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

查看:41
本文介绍了登录后将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';

这是我的路线/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

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

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天全站免登陆