php - Laravel 构造函数重定向不起作用

标签 php laravel laravel-4

我有一个带有多种方法的 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();

However you should really use a filter for this

关于php - Laravel 构造函数重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568147/

相关文章:

php - $stmt->num_rows 即使在调用 store_result 之后也返回 0

php - 使用 Laravel 迁移添加外键约束

Laravel Blade 模板部分重复/缓存错误

php - Laravel 搜索多对多关系

mysql - 使用 Eloquent 加入 Laravel 4

php - Wordpress-SQL语法错误

php - 要求有金额的条件

php - 使用 PHP 清理 SVG

javascript - 如何为不同的 Vue 布局使用不同的资源?

laravel - laravel 中不支持的操作数类型