使用异常变量重定向

时间:2017-02-23 10:28:43

标签: php session laravel-5.3

我有以下(测试)设置:

web.php

Route::get("test/test","TestController@test");
Route::get("test/numeric","TestController@numeric");
Route::get("forbidden", "TestController@exception")

TestController.php

use \Symfony\Component\HttpKernel\Exception\HTTPException; 
public class TestController {

public function test() {
    return redirect()->to("/forbidden")->with("exception",new HttpException(403));            
}

public function numeric() {
    return redirect()->to("/forbidden")->with("exception",403);            
}

public function exception() {
    if (\Session::get("exception") instanceof \Throwable) {
        throw \Session::get("exception"); //Let the default handler handle it.
    } else if (is_numeric(\Session::get("exception"))) {
        throw new HttpException(\Session::get("exception"));
    } else {
        return "Empty exception";
    }
}
}

当我导航到/test/test时,我总是会出现“空例外”。 但是/test/numeric通常会显示异常。此外,我在两种情况下都检查了会话的内容,在第一种情况下,根本没有传递异常对象。

我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

经过更多挖掘后,我意识到由于异常堆栈跟踪,这是不可能做到的。有许多函数将闭包作为参数,因此异常堆栈跟踪不可序列化。 Laravel似乎悄悄地从会话中删除任何不可序列化的变量。