如何在Laravel 5中捕获ReflectionException?

时间:2015-05-17 15:25:16

标签: 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->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 个答案:

答案 0 :(得分:1)

在L5中,您可以全局处理此异常:

// Exceptions/Handler.php

use ReflectionException;

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

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