[英]How to redirect a route prefix to another route in laravel?
I am using laravel 5.7. 我正在使用laravel 5.7。 Please see my code:
请查看我的代码:
web.php web.php
Route::prefix('admin')->group(function () {
Route::get('/', function () {
return redirect()->route('admin.check');
});
Route::get('/check-auth', 'Admin\AdminController@checkAuth')->name('admin.check');
});
AdminController::checkAuth()
public function checkAuth()
{
if(true == Auth::guard('admin')->user()){
return redirect()->route('cs.dashboard');
} else{
return redirect()->route('admin.login');
}
}
I have created a group of routes with prefix admin
. 我创建了一组前缀为
admin
的路由。
What I need is, when I use url localhost:8000/admin
then it should check Auth of admin user, if user is logged in then it should show admin dashboard otherwise redirect him to admin login page : localhost:8000/admin/login
but the issue over here is I am getting apache error which reads: 我需要的是,当我使用url
localhost:8000/admin
它应检查admin用户的身份验证,如果用户已登录,则应显示admin仪表板,否则将其重定向到admin登录页面: localhost:8000/admin/login
但这里的问题是我收到apache错误,内容为:
Not Found
The requested resource /admin was not found on this server.
I also tried using htaccess file placing it in root/public/admin
location, it somewhat solved my issue but I have to use this url. 我还尝试使用htaccess文件将其放置在
root/public/admin
位置,它某种程度上解决了我的问题,但是我必须使用此url。
localhost:8000/index.php/admin
and I dont need index.php in my url. 而且我的网址中不需要index.php。
This is what my .htaccess file looks: 这是我的.htaccess文件的外观:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Please help me to resolve it. 请帮我解决。
Normal way to check if user is authenticated is Middleware 检查用户是否通过身份验证的正常方法是中间件
In your case, check-auth runs when you go to: localhost:8000/admin/
or localhost:8000/admin/check-auth
othwewise laravel will not check authentication. 在您的情况下,当您转到以下位置时,将运行check-auth:
localhost:8000/admin/
或localhost:8000/admin/check-auth
否则laravel将不会检查身份验证。
Please look at Laravel Middlewares. 请查看Laravel中间件。
I think that the best way is using Middleware to check if the logged in user is admin or not.. and it will make your code more elegant than now.. You only need to make the middleware to check if it's admin or not.. if it's admin then return $next($request)
, if not redirect to other page.. Then change your code.. 我认为最好的方法是使用中间件来检查登录用户是否为admin,这将使您的代码比现在更美观。您只需要使中间件检查它是否为admin。如果是管理员,则返回
$next($request)
,如果不重定向至其他页面,则更改代码。
Route::prefix('admin')->group(function () {
Route::get('/', 'AdminController@index');
});
Then create the admin controller and add index function like 然后创建管理控制器并添加索引功能,例如
public function __construct()
{
$this->middleware('admin');
}
public function index()
{
return view('adminpage');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.