当用户未登录时,Laravel 重定向到特定路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45185590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel redirect to specific route when user is not logged
提问by Lluís Puig Ferrer
If i go to http://www.yourdomain.com/admin/logini see my login page.
如果我访问http://www.yourdomain.com/admin/login,我会看到我的登录页面。
If i go to http://www.yourdomain.com/admin/examplei have the redirect to http://www.yourdomain.com/loginwithout the admin.
如果我去http://www.yourdomain.com/admin/example我有重定向到http://www.yourdomain.com/login没有管理员。
My web routes:
我的网络路线:
Auth::routes();
Route::prefix('admin')->group(function() {
Route::get('/login','Auth\AdminLoginController@showLoginForm')->name('admin.login');
Route::post('/login','Auth\AdminLoginController@login')->name('admin.login.submit');
Route::get('/manifiesto','AdminController@getIndex')->name('admin.dashboard');
Route::get('/logout','Auth\AdminLoginController@logout')->name('admin.logout');
Route::get('/trabajadores','AdminController@showTrabajadores')->name('admin.trabajadores');
Route::get('/clientes','AdminController@showClientes')->name('admin.clientes');
Route::get('/proyectos','AdminController@showProyectos')->name('admin.proyectos');
Route::get('/administradores','AdminController@showAdmins')->name('admin.administradores');
});
When i put some url with the /admin before and user isn't logged, i want to redirect to /admin/login.
当我在 /admin 之前添加一些 url 并且用户未登录时,我想重定向到 /admin/login。
Thanks.
谢谢。
More info:
更多信息:
App/http/Controllers/Auth/AdminLoginController.php
应用程序/http/Controllers/Auth/AdminLoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
class AdminLoginController extends Controller
{
protected $loginPath = 'admin/login';
public function __construct()
{
$this->middleware('guest:admin', ['except' => ['logout']]);
}
public function showLoginForm()
{
return view('backend.public.pages.login');
}
public function login(Request $request)
{
//validate the form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
//attempt to log the user in
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
//if successful, then redirect to their intended location
return redirect()->intended(route('admin.dashboard'));
}
return redirect()->back()->withInput($request->only('email','remember'));
}
public function logout()
{
Auth::guard('admin')->logout();
return redirect('/');
}
}
App\Http\Middleware\AdminAuthenticate.php
App\Http\Middleware\AdminAuthenticate.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class AdminAuthenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->guest())
{
if ($request->ajax())
{
return response('Unauthorized.', 401);
}
else
{
return redirect()->guest('admin/login'); // <--- here
}
}
return $next($request);
}
}
回答by linktoahref
- Create an middleware
- 创建中间件
php artisan make:middleware AuthAdmin
- Check for guest in the
handle
method of the middleware
handle
在中间件的方法中检查guest
public function handle($request, Closure $next) { if (Auth::guest()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('admin/login'); } } return $next($request); }
- Add a key to the middleware in
app/Http/Kernel.php
in$routeMiddleware
array
- 添加一个关键中间件
app/Http/Kernel.php
在$routeMiddleware
阵列
'auth_admin' => \App\Http\Middleware\AuthAdmin::class
- Attach the
auth_admin
middleware to the group
- 将
auth_admin
中间件附加到组
Route::group(['prefix' => 'admin', 'middleware' => 'auth_admin'], function() { // Your admin routes except login });
回答by Margi
write bellow code in your route.php file
在你的 route.php 文件中编写波纹管代码
Route::group(array('prefix' => 'admin'), function() {
Route::controller('login', 'AdminloginController');
});
Route::group(array('before' => 'admin_ajax', 'prefix' => 'admin'), function()
{
//route for pages which are render after login
});
Route::get('/admin', function() {
return View::make('admin.loginform');
});
And Write bellow code in your filter.php file
并在您的 filter.php 文件中编写以下代码
Route::filter('admin_ajax', function() {
if (!Auth::admin()->check()) {
return Redirect::to('admin/login');
} else {
}
});
And if you are using laravel 5.4
如果您使用的是 laravel 5.4
Route::get('/manage', function () {
return redirect('manage/login');
});
Route::group(['prefix' => 'manage'], function() {
//login bypass for the below listed controllers
Route::resource('login', 'AdminLoginController@showLoginForm');
Route::post('dologin', 'AdminLoginController@login');
});
回答by Maraboc
All you can do is add the auth
middleware like this :
您所能做的就是auth
像这样添加中间件:
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
Route::get('/login','Auth\AdminLoginController@showLoginForm')->name('admin.login');
Route::post('/login','Auth\AdminLoginController@login')->name('admin.login.submit');
Route::get('/manifiesto','AdminController@getIndex')->name('admin.dashboard');
Route::get('/logout','Auth\AdminLoginController@logout')->name('admin.logout');
Route::get('/trabajadores','AdminController@showTrabajadores')->name('admin.trabajadores');
Route::get('/clientes','AdminController@showClientes')->name('admin.clientes');
Route::get('/proyectos','AdminController@showProyectos')->name('admin.proyectos');
Route::get('/administradores','AdminController@showAdmins')->name('admin.administradores');
});
But by default this will redirect to /login
, if you want to override this you have two chocies depending on if you have other type of users that uses the /login
route or not !!
但默认情况下,这将重定向到/login
,如果您想覆盖它,您有两种选择,具体取决于您是否有其他类型的用户使用该/login
路由!
- If NO ONE uses
/login
route
- 如果没有人使用
/login
路由
1- You need to modify App\Http\Middleware\Authenticate::handle()
method and change /login
to admin/login
.
1-您需要修改App\Http\Middleware\Authenticate::handle()
方法并更改/login
为admin/login
.
2- Then you need to add $loginPath
property to your \App\Http\Controllers\Auth\AuthController
class.
2- 然后你需要$loginPath
为你的\App\Http\Controllers\Auth\AuthController
班级添加属性。
Authenticate
认证
namespace App\Http\Middleware;
class Authenticate {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->guest())
{
if ($request->ajax())
{
return response('Unauthorized.', 401);
}
else
{
return redirect()->guest('admin/login'); // <--- here
}
}
return $next($request);
}
}
AuthController
验证控制器
namespace App\Http\Controllers\Auth;
class AuthController extends Controller
{
protected $loginPath = 'admin/login'; // <--- here
// ... other properties, constructor, traits, etc
}
- If there is someone using
/login
route
- 如果有人使用
/login
路由
You must create you own middlewareand do what it takes for auth checking in the handle
method with redirecting to your admin/liging
route :)
您必须创建自己的中间件,并在handle
重定向到您的admin/liging
路由的方法中执行身份验证检查:)
In this case :
在这种情况下 :
Add the following line in
$routeMiddleware
property atapp/Http/Kernel.php
file'adminAuth' => \App\Http\Middleware\YourNewMiddleware::class,
Don't forget to add your new middleware in route group as follow
Route::group(['prefix' => 'admin', 'middleware' => 'adminAuth'], function() { // your admin routes });
$routeMiddleware
在app/Http/Kernel.php
文件的属性中添加以下行'adminAuth' => \App\Http\Middleware\YourNewMiddleware::class,
不要忘记在路由组中添加新的中间件,如下所示
Route::group(['prefix' => 'admin', 'middleware' => 'adminAuth'], function() { // your admin routes });
回答by Nazmul Hasan
Make an another middleware for admin. follow the step
为管理员制作另一个中间件。按照步骤
Make a file named
AdminAuthenticate
inapp/Http/Middleware
location and copy the content of Authenticate in New file change the Class name asAdminAuthenticate
Change the content of handle function as show below
public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return redirect()->guest('/admin/login'); } } return $next($request); }
Add the following line in
$routeMiddleware
array atapp/Http/Kernel.php file
'AdminAuth' => \App\Http\Middleware\AdminAuthenticate::class,
Now everything is okay. just add your new middleware in route group as follow
Route::group(['prefix' => 'admin', 'middleware' => 'AdminAuth'], function() { // all admin routes });
Or you can add new middleware to constructor function of every admin controller as like below
$this->middleware('AdminAuth');
制作一档名为
AdminAuthenticate
在app/Http/Middleware
位置和验证的内容复制在新文件中更改类名称AdminAuthenticate
修改handle函数的内容如下图
public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return redirect()->guest('/admin/login'); } } return $next($request); }
在
$routeMiddleware
数组中添加以下行app/Http/Kernel.php file
'AdminAuth' => \App\Http\Middleware\AdminAuthenticate::class,
现在一切正常。只需在路由组中添加您的新中间件,如下所示
Route::group(['prefix' => 'admin', 'middleware' => 'AdminAuth'], function() { // all admin routes });
或者您可以将新的中间件添加到每个管理控制器的构造函数中,如下所示
$this->middleware('AdminAuth');