Php 如何捕捉Laravel 5中的ReflectionException?
$module-是我的一个类,基于接口,必须具有公共函数getController。我可能忘记在类中添加getController函数,运行后出现以下错误:Php 如何捕捉Laravel 5中的ReflectionException?,php,exception,laravel,Php,Exception,Laravel,$module-是我的一个类,基于接口,必须具有公共函数getController。我可能忘记在类中添加getController函数,运行后出现以下错误: ReflectionException in Container.php line 776: Class App\Http\Controllers\ does not exist 并希望捕获此异常,但此代码无效: try { \Route::get($module->getUrl(), $module
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;
}
}
}
在L5中,您可以全局处理此异常:
// Exceptions/Handler.php
use ReflectionException;
public function render($request, \Exception $e)
{
if ($e instanceof ReflectionException) {
// …
}
return parent::render($request, $e);
}
谢谢在这种情况下,当任何异常反射Exception时都会触发它,但我只需要捕获导致我的代码的异常。真的吗?当然是:)请在你的问题中添加
ReflectionException
的代码。它确实被添加了-异常导致这个命令:$module->getController()或者我不懂的东西。刚刚在我的沙盒中复制了您的代码,似乎无法使用try catch onRoute::get()
处理ReflectionException
。所以对我来说,全球处理是实现你想要的唯一途径。