laravel中的重定向,没有返回语句 [英] Redirection in laravel without return statement
本文介绍了laravel中的重定向,没有返回语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个blogsController,其创建功能如下.
i have this blogsController, the create function is as follows.
public function create() {
if($this->reqLogin()) return $this->reqLogin();
return View::make('blogs.create');
}
在BaseController中,我具有此功能,用于检查用户是否已登录.
In BaseController, i have this function which checks if user is logged in.
public function reqLogin(){
if(!Auth::check()){
Session::flash('message', 'You need to login');
return Redirect::to("login");
}
}
这段代码可以正常工作,但是这不是我想要的create函数,如下所示.
This code is working fine , but it is not what is need i want my create function as follows.
public function create() {
$this->reqLogin();
return View::make('blogs.create');
}
我可以这样做吗?
除此之外,我是否可以像在 Yii 框架中那样在控制器顶部设置身份验证规则.
Apart from that, can i set authantication rules , like we do in Yii framework, at the top of controller.
推荐答案
您应该将支票放入过滤器中,然后仅当用户首先登录时才让用户进入控制器.
You should put the check into a filter, then only let the user get to the controller if they are logged in in the first place.
过滤器
Route::filter('auth', function($route, $request, $response)
{
if(!Auth::check()) {
Session::flash('message', 'You need to login');
return Redirect::to("login");
}
});
路线
Route::get('blogs/create', array('before' => 'auth', 'uses' => 'BlogsController@create'));
控制器
public function create() {
return View::make('blogs.create');
}
这篇关于laravel中的重定向,没有返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文