php 如果登录,laravel 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32129874/
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 redirect if logged in
提问by smartrahat
i am using laravel 5.1.8. i am making a login/registration system. i made a controller named AdminController and protect it with middleware.
我正在使用 Laravel 5.1.8。我正在制作一个登录/注册系统。我制作了一个名为 AdminController 的控制器并用中间件保护它。
but i am using laravel's default AuthController which methods and classes are located in different locations. where routes are:
但我使用的是 laravel 的默认 AuthController,其中的方法和类位于不同的位置。其中路线是:
Route::Controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);
get('admin', 'AdminController@index');
get('profile', 'AdminController@profile');
get('article', 'AdminController@article');
users cannot access AdminController without logging in. its redirected to login page. but i want, if a logged in user typed the address of login page or registration on the address bar of browser, the page will redirected to AdminController.
用户不能在没有登录的情况下访问 AdminController。它被重定向到登录页面。但我想,如果登录用户在浏览器的地址栏上输入登录页面或注册的地址,页面将重定向到 AdminController。
when i try to do this, it looking for '/home' and gives errors. i want to make it '/admin'.
当我尝试这样做时,它会寻找“/home”并给出错误。我想让它'/admin'。
回答by mdamia
go to
App\Http\Middleware\RedirectIfAuthenticated
then change it from
去
App\Http\Middleware\RedirectIfAuthenticated
然后改变它
public function handle($request, Closure $next)
{
if ($this->auth->check()) {
return redirect('/home');
}
return $next($request);
}
to
到
public function handle($request, Closure $next)
{
if ($this->auth->check()) {
return redirect('/admin');
}
return $next($request);
}
回答by BrokenBinary
Add this to your AuthController
:
将此添加到您的AuthController
:
protected $redirectTo = '/admin';
This tells all the redirect methods in the various traits to redirect there instead of to /home
.
这告诉各种特征中的所有重定向方法重定向到那里而不是 to /home
。
回答by santoshvijaypawar
when a user is successfully authenticated, they will be redirected to the /home URI, which you will need to register a route to handle. You can customize the post-authentication redirect location by defining a redirectPath property on the AuthController:
当用户成功通过身份验证时,他们将被重定向到 /home URI,您需要注册一个路由来处理。您可以通过在 AuthController 上定义 redirectPath 属性来自定义身份验证后重定向位置:
protected $redirectPath = '/dashboard';
受保护的 $redirectPath = '/dashboard';
回答by Bharat Parmar
Include \App\Http\Middleware\RedirectIfAuthenticated::class
middleware in $middlewareGroups "web" array after \Illuminate\Session\Middleware\StartSession::class
middleware
\App\Http\Middleware\RedirectIfAuthenticated::class
在中间件之后的 $middlewareGroups "web" 数组中包含\Illuminate\Session\Middleware\StartSession::class
中间件
then modify your redirect path in handle()
method of RedirectIfAuthenticated
然后在handle()
RedirectIfAuthenticated 方法中修改您的重定向路径
public function handle($request, Closure $next, $guard = null)
{
//check if authenticate && second is condition when we need to redirect i.e,
if(Auth::guard($guard)->check() && $request->route()->named('login') ) {
return redirect()->route('dashboard');
}
return $next($request);
}
回答by rashedcs
You can check using auth function.
您可以使用 auth 功能进行检查。
public function checkLogin()
{
if (auth()->user())
{
return redirect(route('home'));
}
else
{
return redirect(route('login'));
}
}