登录后 Laravel 5.4 重定向到自定义 url [英] Laravel 5.4 redirection to custom url after login

查看:45
本文介绍了登录后 Laravel 5.4 重定向到自定义 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Laravel Framework 5.4.10,我使用的是常规身份验证

I am using Laravel Framework 5.4.10, and I am using the regular authentication that

php artisan make:auth

提供.我想保护整个应用程序,并在登录后将用户重定向到/themes.

provides. I want to protect the entire app, and to redirect users to /themes after login.

我有 4 个控制器:ForgotPasswordController.php、LoginController.php、RegisterController.php 和 ResetPasswordController.php.我已将此行编辑为最后三行:

I have 4 controllers: ForgotPasswordController.php, LoginController.php, RegisterController.php and ResetPasswordController.php. I have edited this line into the last three:

protected $redirectTo = '/themes';

这是我的 routes/web.php 的第一行:

This is the first line in my routes/web.php:

Auth::routes();

我已经在我的 Controller.php 中添加了这个函数:

I have added this function in my Controller.php:

    public function __construct()
    {
        $this->middleware('auth');

    }

我已经编辑了app/Http/Middleware/RedirectIfAuthenticated.php,所以handle函数是这样的:

I have edited app/Http/Middleware/RedirectIfAuthenticated.php, so that the handle function looks like this:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/themes');
    }

    return $next($request);
}

一切都很好,除了当我点击登录按钮时,我被重定向到/",而不是/themes".如果我不需要在控制器中进行身份验证(Controller.php 文件中没有 __contruct 函数),我会在登录时重定向.我究竟做错了什么?

It's all fine, except when I click the Login button, I get redirected to "/", not "/themes". If I don't require authentication in the controllers (no __contruct function in Controller.php file), I get redirected OK at login. What am I doing wrong?

推荐答案

这就是我目前的工作,真是巧合.

That's what i am currrently working, what a coincidence.

您还需要将以下行添加到您的 LoginController

You also need to add the following lines into your LoginController

namespace AppHttpControllersAuth;

use AppHttpControllersController;

use IlluminateFoundationAuthAuthenticatesUsers;

use IlluminateHttpRequest;

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;


protected function authenticated(Request $request, $user)
{
if ( $user->isAdmin() ) {// do your magic here
    return redirect()->route('dashboard');
}

 return redirect('/home');
}
/**
 * Where to redirect users after login.
 *
 * @var string
 */
//protected $redirectTo = '/admin';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}

这篇关于登录后 Laravel 5.4 重定向到自定义 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆