[英]Laravel 5.1 page authentication using routes
I'm working on a site that needs an admin panel. 我正在一个需要管理面板的网站上工作。 I am currently trying to set up the authentication of that panel, though I can not find a way to deny access from any guest users (non-admins).
我目前正在尝试设置该面板的身份验证,但我找不到拒绝任何访客用户(非管理员)访问的方法。 I have a login page, of course, and after login, it routes to the admin page, though you can also go to /admin when you're not logged in.
当然,我有一个登录页面,登录后,它会路由到管理页面,但是当你没有登录时你也可以去/ admin。
routes.php : routes.php :
Route::get('home', function(){
if (Auth::guest()) {
return Redirect::to('/');
} else {
return Redirect::to('admin');
}
});
Route::get('admin', function () {
return view('pages.admin.start');
});
MainController.php : MainController.php :
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MainController extends Controller {
public function getIndex() {
return view('pages.index');
}
public function getAbout() {
return view('pages.about');
}
public function getPortfolio() {
return view('pages.portfolio');
}
public function getShop() {
return view('pages.shop');
}
public function getContact() {
return view('pages.contact');
}
/*public function getAdmin() {
return view('pages.admin.start');
}*/
}
I could really use some help here, because I'm totaly stuck, and yes, I have read the documentation, though maybe I'm just missing something. 我真的可以在这里使用一些帮助,因为我完全卡住了,是的,我已经阅读了文档,尽管我可能只是遗漏了一些东西。
You should use a Middleware to handle authentication of your users 您应该使用中间件来处理用户的身份验证
1) First you have to create a middleware that will check if the user requiring the page is an admin, and if not you have to redirect; 1)首先,你必须创建一个中间件来检查需要该页面的用户是否是管理员,如果不是,你必须重定向; something like this:
这样的事情:
class AdminMiddleware
{
public function handle(Request $request, Closure $next )
{
//if User is not admin
//redirect to no permess
return $next($request);
}
}
2) Then you have to bind the middleware to the routes you want to be accessible only from an admin user: 2)然后,您必须将中间件绑定到您只能从管理员用户访问的路由:
//bind the middleware to all the routes inside this group
Route::group( ['middleware' => 'adminmiddleware' ], function()
{
Route::get('admin', function () {
return view('pages.admin.start');
});
//other routes
});
Assuming you have a line like this: 假设你有这样的一行:
'auth' => 'App\Http\Middleware\Authenticate',
in your app/Http/Kernel.php
file: 在你的
app/Http/Kernel.php
文件中:
put all the routes you need "authenticated" inside the grouping, but keep the "guest" routes outside of them : 将您需要的所有路由“经过身份验证”放在分组中,但将“访客”路由保留在其中:
Route::get('home', function(){
if (Auth::guest()) {
return Redirect::to('/');
} else {
return Redirect::to('admin');
}
});
Route::group( ['middleware' => 'auth' ], function(){
Route::get('admin', function () {
return view('pages.admin.start');
});
Route::just-another-route()...;
Route::just-another-route()...;
});
Documentation: http://laravel.com/docs/5.1/routing#route-groups 文档: http : //laravel.com/docs/5.1/routing#route-groups
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.