php Laravel 构造函数重定向不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27568147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel constructor redirect is not working
提问by Irfan Ahmed
I have a controller with several methods and I need to add a specific authorization check on start of each method. So I thought to put this check in constructor as,
我有一个带有多种方法的控制器,我需要在每个方法开始时添加特定的授权检查。所以我想把这个检查放在构造函数中,
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');
}
}
}
It does not work, it just prints the message inside Session check and load the dashboard page rather than redirecting back to login page.
它不起作用,它只是在会话检查中打印消息并加载仪表板页面,而不是重定向回登录页面。
I have also tried something like this,
我也尝试过这样的事情,
public function getDashboard() {
$this->isAuthorized();
$this->layout->content = View::make('admin.dashboard');
}
When I try to call this method with this weird return statement, then it works
当我尝试用这个奇怪的返回语句调用这个方法时,它就起作用了
public function getDashboard() {
return $this->isAuthorized();
$this->layout->content = View::make('admin.dashboard');
}
I get that idea from here. How do I achieve this using constructor method. Any help is greatly appreciated.
我从这里得到这个想法。我如何使用构造函数方法实现这一点。任何帮助是极大的赞赏。
回答by lukasgeiter
Returning a Redirect
to execute it is only possible from routes, controller actions and filters. Otherwise you have to call send()
返回 a Redirect
以执行它只能从路由、控制器操作和过滤器中返回。否则你必须打电话send()
Redirect::to('login')->send();
回答by Sampath Perera
Make Sure to use Illuminate\Routing\Redirector; and pass it to the constructor. (Laravel 5.2)
确保使用 Illuminate\Routing\Redirector; 并将其传递给构造函数。(Laravel 5.2)
use Illuminate\Routing\Redirector;
class ServiceController extends Controller {
public function __construct(Request $request, Redirector $redirect) {
$this->service = Auth::user()->Service()->find($request->id);
if (!$this->service) {
$redirect->to('/')->send();
}
}
回答by Omar J Fry
Event with Illuminate\Routing\Redirector, laravel set redirect in http header, but also continue to execute request like there are no redirect. So the solution is use die() after the redirection.
带有 Illuminate\Routing\Redirector 的事件,laravel 在 http 标头中设置重定向,但也像没有重定向一样继续执行请求。所以解决方案是在重定向后使用 die()。
public function __construct(Request $request, \Illuminate\Routing\Redirector $redirecor)
{
//$bool = ...
if (false == $bool) {
$redirecor->action('MyController@myAction')->send() ;
die();
}
}
回答by Chandan Mistry
Use ->send()
用 ->send()
redirect()->route('named-route')->send();插入的
return
return
return redirect()->route('named-route');在 Laravel 构造函数中。