aso*_*erg 3 php laravel laravel-5
我了解如何使用redirect()方法重定向用户,但此方法返回302代码,浏览器必须发出第二个HTTP请求。是否可以在内部将请求转发到不同的控制器和操作?
我正在中间件中进行此检查,因此我的句柄函数如下所示:
public function handle($request, Closure $next)
{
if (auth()->user->age <= 20) { //example
//internally forward the user to a different controller@action
}
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将call方法用作:
app()->call('App\Http\Controllers\ControllerName@funName')
Run Code Online (Sandbox Code Playgroud)
或者
app('App\Http\Controllers\ControllerName')->funName();
Run Code Online (Sandbox Code Playgroud)
所以你的中间件将如下所示:
if (auth()->user->age <= 20) {
return app()->call('App\Http\Controllers\ControllerName@action');
}
Run Code Online (Sandbox Code Playgroud)