2015-05-17 61 views
2

$ module - 是我的一個基於Interface的類,必須具有公共函數getController。我忘了在類中添加getController功能,並運行後,我有這樣的錯誤:如何在Laravel 5中捕捉ReflectionException?

ReflectionException in Container.php line 776: 
Class App\Http\Controllers\ does not exist 

,並想抓住這個異常,但是這個代碼不工作:

try 
    { 
     \Route::get($module->getUrl(), $module->getController() . '@index'); 
    } 
    catch (\ReflectionException $e) 
    { 

     echo 123123; 
    } 

代碼示例:

namespace App\MyModules; 

MyModuleManager::bindRoute(); 


interface MyModuleInterface 
{ 
    public function getUrl(); 

    public function getController(); 
} 


class MyClass implements MyModuleInterface 
{ 
    public function getUrl() 
    { 
     return '/url'; 
    } 
/* 
* for example: this method i forgdet to add 
* and in ModuleManager::bindRoute i want to cath exception 
* 
    public function getController() 
    { 

    } 
*/ 
} 


class MyModuleManager 
{ 

    static public function bindRoute() 
    { 
     $module = new MyClass(); 

     try 
     { 
      \Route::get($module->getUrl(), $module->getController() . '@index'); 
     } 
     catch (\ReflectionException $e) 
     { 

      echo 123123; 
     } 

    } 
} 

回答

1

在L5可以處理全局此異常:

// Exceptions/Handler.php 

use ReflectionException; 

public function render($request, \Exception $e) 
{ 
    if ($e instanceof ReflectionException) { 
     // … 
    } 

    return parent::render($request, $e); 
} 
+0

謝謝。在這種情況下,當任何異常ReflectionException時它將被觸發,但是我只需要捕獲那些導致我的代碼的東西。是真的? – Stanislav

+0

確定它是:)請在您的問題中添加'ReflectionException'代碼。 –

+0

這是alreay添加 - 異常導致此命令:$ module-> getController() – Stanislav