我正在使用Laravel 5.0,并且得到了一个问题。我的网站是正常工作时,没有错误,如404,500等。但当我的网站上有错误,然后它呈现错误页面,但一些中间件不加载。
我的网站有两部分前端和后端。两个部分都有我正在使用的diff-diff中间件。我正在使用一些自定义中间件,其中一些我在前端使用,另一些在后端使用。但是当现场出现任何错误时,中间件就无法正常工作,这些中间件加载到Kernel.php文件中的$routeMiddleware数组中。
我研究了许多文章,但都给了我相同的解决方案,以便在$middleware中加载所有必需的中间件,这对我来说似乎不太好。因为在$middleware数组中,所有的中间件都加载了前端和后端部分。但我只需要加载空间中间件的一个部分。我怎么才能实现这一点。
这是我的kernel.php文件
class Kernel extends HttpKernel {
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
// 'App\Http\Middleware\VerifyCsrfToken',
];
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'auth.frontend' => 'App\Http\Middleware\FrontendAuthenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
];
}这是我的route.php文件代码
Route::group(['middleware' => 'auth.frontend'], function() {
Route::get('/home', ['as' => 'home', 'uses' => 'HomeController@index']);
Route::get('/inner_page', ['as' => 'inner_page', 'uses' => 'HomeController@inner_page']);
});这是怎么可能的,请在这个问题上帮助我。
发布于 2015-07-25 17:24:35
正如documentation中所解释的,您必须定义一个terminate($request, $response)方法,以便在准备将响应发送到浏览器之后运行中间件。terminate($request, $response)方法应该同时接收请求和响应。
然后,您可以将中间件添加到global或route中间件阵列。
示例:
<?php
namespace App\Http\Middleware;
use Closure;
class FrontendAuthenticate
{
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
// Check if the $response is an error and then do stuff
}
}https://stackoverflow.com/questions/31623713
复制相似问题