我想创建我的应用程序(我不想使用laravel默认登录系统)
我想使用一个中间件在我的应用程序中的每个HTTP请求中运行,除了一个
在Laravel5.1文档系统中,我可以使用全局中间件,但我不想只使用中间件登录页面。我该怎么办?这是我的中间件:
<?php
namespace App\Http\Middleware;
use Closure;
class Admin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if( ! session()->has('Login' ) )
{
return redirect('login');
}
return $next($request);
}
}发布于 2016-02-26 21:18:17
您可以使用路由组并将中间件分配给它:
Route::group(['middleware' => 'Admin'], function () {
// All of your routes goes here
});
// Special routes which you dont want going thorugh the this middleware goes here发布于 2016-02-26 21:11:08
不要对您的中间件做任何事情。你可以在路线小组之外自由地走那条路。所以它变成了一条独立的路线。或者,您可以创建一个新的路由组,并且只将该一条路由放入其中,而不使用该中间件。例如:
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");
});这样,中间件只影响第二个路由组。
发布于 2016-02-26 21:25:23
有几种方法可以解决这个问题,一种是在中间件中解决这个问题,并在那里排除路由,另一种是将您希望在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
});https://stackoverflow.com/questions/35661659
复制相似问题