LARAVEL - >为什么尝试在Laravel中不起作用?

时间:2016-02-23 08:52:01

标签: php laravel laravel-4 laravel-5

我在RouteServiceProvider中有代码:

$router->bind('user', function ($value) {
    try{
        throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
    }catch(Exception $e){
        exit('nott');
    }
});

我不知道输出

nott

我正在

Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteServiceProvider.php line 75:
...

EDITED: 这有效:

$router->bind('user', function ($value) {
    try{
        throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
    }catch(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
        exit('addd');
    }
});

但这不起作用:

$router->bind('user', function ($value) {
    try{
        return (new User)->findOrFail(122);
    }catch(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
        exit('addd');
    }
});

1 个答案:

答案 0 :(得分:8)

$router->bind('user', function ($value) {
    try{
        throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
    }catch(\Exception $e){
        exit('nott');
    }
});

OR

use Exception; //on top

    $router->bind('user', function ($value) {
        try{
            throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
        }catch(Exception $e){
            exit('nott');
        }
    });

我想现在你明白你错过了什么。