我正在使用hesto / multi-auth package
默认情况下,如果我已将auth中间件分配给路由,那么在登录后它应该将我重定向回目标页面,但它只是第一次执行..
一切正常工作我只想第一次但是一旦我退出并尝试再次访问路线它会进入登录页面而不是重定向到用户/家庭,但第一次它工作完美看到40秒视频 http://neelnetworks.org/video/laravel.mp4 对此有何解决方案?这些是我的网络路线
Route::get('/', 'PagesController@getIndex')->middleware('user');
Route::group(['prefix' => 'user'], function () {
Route::get('/login', 'UserAuth\LoginController@showLoginForm');
Route::post('/login', 'UserAuth\LoginController@login');
Route::post('/logout', 'UserAuth\LoginController@logout');
Route::get('/register', 'UserAuth\RegisterController@showRegistrationForm');
Route::post('/register', 'UserAuth\RegisterController@register');
Route::post('/password/email', 'UserAuth\ForgotPasswordController@sendResetLinkEmail');
Route::post('/password/reset', 'UserAuth\ResetPasswordController@reset');
Route::get('/password/reset', 'UserAuth\ForgotPasswordController@showLinkRequestForm');
Route::get('/password/reset/{token}', 'UserAuth\ResetPasswordController@showResetForm');
});
这是我的页面控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function getIndex()
{
return "hello";
}
}
第一次完美地运作,为什么不在我们登录后呢?
如果我清除所有缓存和cookie,它会再次起作用,这是默认行为还是laravel中的错误?你可以澄清一下这个包
是一个问题吗? 中提出了这个问题答案 0 :(得分:1)
在UserAuth / LoginController.php
中制作这样的showLoginForm方法public function showLoginForm()
{
session()->put('url.intended',url()->previous());
return view('user.auth.login');
}
因为它在将表单发布到/ user / login时更改了以前的URL,如果您已登录,则会被重定向到/ user / home
答案 1 :(得分:0)
登录后的laravel默认重定向是转到LoginController中的/ home设置:
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
并且有默认的中间件RedirectIfAuthenticated
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
并在app / Http / Controllers / Auth / RegisterController.php
中class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
这就是你需要做出改变以便按照自己的方式工作的地方......
答案 2 :(得分:0)
在RedirectIfNot {guard-name}中,例如RedirectIfNotAdmin
我们需要添加此行
session()->put('url.intended', url()->current());
所以中间件看起来像这样
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfNotAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = 'admin')
{
if (!Auth::guard($guard)->check()) {
session()->put('url.intended', url()->current());
return redirect('/admin/login');
}
return $next($request);
}
}