我有一个带有多种方法的 Controller ,我需要在每个方法的开始时添加一个特定的授权检查。所以我想把这个检查放在构造函数中,
class AdminController extends BaseController {
public function __construct() {
$this->isAuthorized();
}
protected $layout = "layouts.main";
private function isAuthorized() {
if (!Session::get('userId')) {
echo "inside check"; // checking for debug purpose
return Redirect::to('login');
}
}
/**
* Admin dashboard view after authentication.
*/
public function getDashboard() {
$this->layout->content = View::make('admin.dashboard');
}
}
它不起作用,它只是在 session 检查中打印消息并加载仪表板页面,而不是重定向回登录页面。
我也试过这样的,
public function getDashboard() {
$this->isAuthorized();
$this->layout->content = View::make('admin.dashboard');
}
当我尝试用这个奇怪的返回语句调用这个方法时,它会起作用
public function getDashboard() {
return $this->isAuthorized();
$this->layout->content = View::make('admin.dashboard');
}
我从 here 得到这个想法.如何使用构造方法实现这一点?
最佳答案
返回一个 Redirect
来执行它只能通过路由、 Controller Action 和过滤器来执行。否则你必须调用 send()
Redirect::to('login')->send();
关于php - Laravel 构造函数重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568147/