【发布时间】:2015-01-08 21:43:02
【问题描述】:
所以我的一些路由使用了 csrf 过滤器,我已经添加如下:
Route::post('/user/notifications/dismiss', array('before' => 'csrf', 'uses' => 'UsersController@dismiss'));
在我的 start/global.php 中,我设置了错误处理程序以在生产环境中生成视图(此示例的代码已简化)
App::error(function(Exception $exception, $code)
{
Log::error($exception);
if(Config::get('app.debug') === false) {
return Response::view('message', array('title' => $code, 'message' => 'Server error'));
}
});
在我的 BaseController __construct() 中我做了一些基本的初始化,这包括为视图设置一些变量
class BaseController extends Controller {
public function __construct() {
$user = Auth::user();
View::share('user', $user);
$this->setupAssets();
$this->setupOtherStuff();
...
}
// css and js files to include
protected function setupAssets() {
$stylesheets = array(
'/css/styles.css',
);
View::share('stylesheets', $stylesheets);
$javascripts = array(
'/js/script.js',
);
View::share('javascripts', $javascripts);
}
...
问题是当应用程序没有通过 csrf 检查时,它会在过滤器中抛出异常。此时 BaseController __construct() 尚未运行,所以我收到一堆错误,即视图中使用的变量未分配。
我的问题是,解决这个问题的最佳方法是什么?我可以将所有视图分配放在 App::before() 事件中,但我宁愿避免这样做,因为该方法可能会变得非常大。
【问题讨论】:
-
csrf过滤器真的会阻止程序继续运行吗?