我是laravel的新手,我想创建我的应用程序(我不想使用laravel默认登录系统)
我想在我的应用程序中的每个HTTP请求期间使用中间件运行,除了一个
在laravel 5.1文档中我可以使用全局中间件,但我不想在登录页面中使用中间件。 我该怎么办 ? 这是我的中间件:
$scope.cars = [
{url: 'Volvo.png', label: 'Volvo'},
{url: 'Benz.png', label: 'Benz'},
{url: 'JohnDeer.png', label: 'John Deer'},
{url: 'BMW.png', label: 'BMW'},
];
答案 0 :(得分:1)
不要对你的中间件做任何事情。你可以自由地在路线组外面走那条路。所以它成为一个独立的路线。或者,您可以创建一个新的路由组,并在没有该中间件的情况下仅放入该路由。例如
Route::group(['prefix' => 'v1'], function () {
Route::post('login','AuthenticationController');
});
Route::group(['prefix' => 'v1', 'middleware' => 'web'], function () {
Route::resource('deparments','AuthenticationController');
Route::resource("permission_roles","PermissionRolesController");
});
使用此中间件仅影响第二个路由组
答案 1 :(得分:1)
您可以使用路由组并将中间件分配给它:
Route::group(['middleware' => 'Admin'], function () {
// All of your routes goes here
});
// Special routes which you dont want going thorugh the this middleware goes here
答案 2 :(得分:1)
有几种方法可以解决这个问题,一种方法是在中间件中解决这个问题,并在那里排除路由,另外两种方法是将routes.php
中间件所涵盖的所有路由分组。然后将你想要的那些排除在分组之外。
在中间件中处理此问题
只需修改handle
函数,以包含检查所请求的URI的if
语句
public function handle($request, Closure $next)
{
if ($request->is("route/you/want/to/exclude"))
{
return $next($request);
}
if( ! session()->has('Login' ) )
{
return redirect('login');
}
else
{
return redirect('login');
}
}
此方法允许您将中间件设置为全局中间件,并且可以通过使用or $request->is()
扩展if语句来进行多次排除。
在路线中解决此问题
//Place all the routes you don't want protected here
Route::group(['middleware' => 'admin'], function () {
//Place all the routes you want protected in here
});