我想要做的是用一个中间件对多个前缀进行分组:
Route::group(['middleware' => ['centralize']], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setLocale()], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setCountry()], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setCity()], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setArea()], function () {
//routes
});
});
});
});
});
我已将centralize
中间件添加到kernel
:
protected $routeMiddleware = [
...
'centralize'=> \App\Http\Middleware\Centralize::class,
...
];
但是根本没有调用中间件,这里缺少什么吗?
编辑:它直接进入房屋的视野,我正在dd
内做middleware
,但它从未到达那里!
编辑:中间件代码:
<?php
namespace App\Http\Middleware;
use App\Utilities\Centralization;
use Closure;
use Illuminate\Http\RedirectResponse;
class Centralize
{
/**
* @var \Illuminate\Http\Request
*/
private $request;
/**
* @var array
*/
private $params;
private $location;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(\Illuminate\Http\Request $request, Closure $next)
{
$this->request = $request;
dd('hit');
$this->params = explode('/', $request->getPathInfo());
$this->figureLocale();
$this->figureCountry();
$this->figureCity();
$this->figureArea();
$this->figureLocation();
$redirection = implode('/', $this->params);
if ($request->getPathInfo() !== $redirection) {
return new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);
}
\View::share([
'countries' => Centralization::getCountries(),
'cities' => Centralization::getCities(),
'areas' => Centralization::getAreas(),
'location' => $this->location
]);
return $next($request);
}
private function figureLocale()
{
//...
}
private function figureCountry()
{
//..
}
private function figureCity()
{
//...
}
private function figureArea()
{
//...
}
private function figureLocation()
{
//...
}
}
编辑:我也尝试过:
Route::middleware(['centralize'])->group(function () {
Route::group(['prefix' => \App\Utilities\Centralization::setLocale()], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setCountry()], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setCity()], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setArea()], function () {
//..
});
});
});
});
});
但是结果相同,它永远不会打入中间件!
编辑:使用artisan
和手动清除缓存也不起作用!
答案 0 :(得分:3)
组路由中间件这样调用:
在routes / web.php中
foreach(DateTime dt in list)
{
if(myDate>dt)
continue;
else
//You've got the answer here
}
在app / Http / kernal.php中
Route::group(['middleware' => ['centralize']], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setLocale()], function () {
Route::group(['prefix' => \App\Utilities\Centralization::setCountry()], function () {
//routes
});
});
});
在app / Http / Middleware / CentralizeMiddleware.php
protected $routeMiddleware = [
'centralize' => \App\Http\Middleware\CentralizeMiddleware::class,
];
答案 1 :(得分:0)
您在一次编辑中提到:
直接进入房屋的视图
因此,您正在访问的URL实际上是/
。并且正如注释中所阐明的,“ home”路由是嵌套在前缀内的。但这意味着它实际上是/fr/france/paris/somewhere/
之类的路线,而不是 /
之类的路线。
路由中的prefix
表示“此路由适用于具有此前缀的URI”。如果URI不包含该前缀,则不会应用该路由。
因此,这意味着/
没有路线,要访问它,将会:
不触发centralize
中间件;
找不到匹配的路由,因此抛出404;
如果您想将centralize
中间件应用到本地路由,则需要将其放置在该组中,但在前缀之外:
Route::middleware(['centralize'])->group(function () {
Route::get('/', 'HomeController@index')->name('home');
Route::group(['prefix' => \App\Utilities\Centralization::setLocale()], function () {
// ...
});
});