Laravel 5.4登录后重定向到自定义URL

时间:2022-02-17 07:20:30

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

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

php artisan make:auth

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

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

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

我有4个控制器:ForgotPasswordController.php,LoginController.php,RegisterController.php和ResetPasswordController.php。我把这一行编辑成了最后三行:

protected $redirectTo = '/themes';

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

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

Auth::routes();

I have added this function in my Controller.php:

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

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

    }

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

我编辑了app / Http / Middleware / RedirectIfAuthenticated.php,因此handle函数如下所示:

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

    return $next($request);
}

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?

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

5 个解决方案

#1


34  

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

这就是我正在努力工作,这是多么巧合。

You also need to add the following lines into your LoginController

您还需要在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 margic 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']);
}
}

#2


7  

If you look in the AuthenticatesUsers trait you will see that in the sendLoginResponse method that there is a call made to $this->redirectPath(). If you look at this method then you will discover that the redirectTo can either be a method or a variable.

如果查看AuthenticatesUsers特性,您将在sendLoginResponse方法中看到对$ this-> redirectPath()的调用。如果你看一下这个方法,你会发现redirectTo可以是方法,也可以是变量。

This is what I now have in my auth controller.

这就是我现在在我的auth控制器中所拥有的。

public function redirectTo() {
    $user = Auth::user();
    switch(true) {
        case $user->isInstructor():
            return '/instructor';
            break;
        case $user->isAdmin():
        case $user->isSuperAdmin():
            return '/admin';
            break;
        default:
            return '/account';
    }
}

#3


5  

The way I've done it by using AuthenticatesUsers trait.

我通过使用AuthenticatesUsers特性完成它的方式。

\App\Http\Controllers\Auth\LoginController.php

Add this method to that controller:

将此方法添加到该控制器:

/**
 * Check user's role and redirect user based on their role
 * @return 
 */
public function authenticated()
{
    if(auth()->user()->hasRole('admin'))
    {
        return redirect('/admin/dashboard');
    } 

    return redirect('/user/dashboard');
}

#4


1  

You should set $redirectTo value to route that you want redirect

您应该将$ redirectTo值设置为要重定向的路由

$this->redirectTo = route('dashboard');

inside AuthController constructor.

在AuthController构造函数中。

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/';

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

#5


0  

you can add method in LoginController add line use App\User; on Top, after this add method, it is work for me wkwkwkwkw , but you must add {{ csrf_field() }} on view admin and user

你可以在LoginController中添加方法添加行使用App \ User;在Top上,在这个add方法之后,它对我来说是wkwkwkwkw,但你必须在视图管理员和用户上添加{{csrf_field()}}

protected function authenticated(Request $request, $user){

$user=User::where('email',$request->input('email'))->pluck('jabatan');
$c=" ".$user." ";
$a=strcmp($c,' ["admin"] ');

if ($a==0) {
    return redirect('admin');

}else{
    return redirect('user');

}}

#1


34  

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

这就是我正在努力工作,这是多么巧合。

You also need to add the following lines into your LoginController

您还需要在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 margic 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']);
}
}

#2


7  

If you look in the AuthenticatesUsers trait you will see that in the sendLoginResponse method that there is a call made to $this->redirectPath(). If you look at this method then you will discover that the redirectTo can either be a method or a variable.

如果查看AuthenticatesUsers特性,您将在sendLoginResponse方法中看到对$ this-> redirectPath()的调用。如果你看一下这个方法,你会发现redirectTo可以是方法,也可以是变量。

This is what I now have in my auth controller.

这就是我现在在我的auth控制器中所拥有的。

public function redirectTo() {
    $user = Auth::user();
    switch(true) {
        case $user->isInstructor():
            return '/instructor';
            break;
        case $user->isAdmin():
        case $user->isSuperAdmin():
            return '/admin';
            break;
        default:
            return '/account';
    }
}

#3


5  

The way I've done it by using AuthenticatesUsers trait.

我通过使用AuthenticatesUsers特性完成它的方式。

\App\Http\Controllers\Auth\LoginController.php

Add this method to that controller:

将此方法添加到该控制器:

/**
 * Check user's role and redirect user based on their role
 * @return 
 */
public function authenticated()
{
    if(auth()->user()->hasRole('admin'))
    {
        return redirect('/admin/dashboard');
    } 

    return redirect('/user/dashboard');
}

#4


1  

You should set $redirectTo value to route that you want redirect

您应该将$ redirectTo值设置为要重定向的路由

$this->redirectTo = route('dashboard');

inside AuthController constructor.

在AuthController构造函数中。

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/';

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

#5


0  

you can add method in LoginController add line use App\User; on Top, after this add method, it is work for me wkwkwkwkw , but you must add {{ csrf_field() }} on view admin and user

你可以在LoginController中添加方法添加行使用App \ User;在Top上,在这个add方法之后,它对我来说是wkwkwkwkw,但你必须在视图管理员和用户上添加{{csrf_field()}}

protected function authenticated(Request $request, $user){

$user=User::where('email',$request->input('email'))->pluck('jabatan');
$c=" ".$user." ";
$a=strcmp($c,' ["admin"] ');

if ($a==0) {
    return redirect('admin');

}else{
    return redirect('user');

}}