Laravel 5.4 redirection to custom url after login(登录后 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:登录后 Laravel 5.4 重定向到自定义 url
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01