[英]Laravel5 `RouteServiceProvider` `should be compatible with` Error
[英]Laravel5 RouteServiceProvider not working
你能解決我的問題嗎? 我正在使用laravel5 App/Providers/RouteServiceProvider
。 我正在嘗試根據當前的lang添加路由前綴。 例如:我有routes.php,其中的路線如下:
/blog
/home
我需要這樣做:我的網址是例如:
/en/blog
我從url獲取“lang”,我想添加路由前綴,以便與我的路由匹配。 所以它會做什么。 像這樣:
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
我不知道為什么,但它不起作用...如果url是/en/blog
,它與任何路由都不匹配,無論如何它應該......我也嘗試更改名稱空間並在某些控制器方法中做這個:
die(print_r(Route::getCurrentRoute()));
但所有路由的命名空間是: App\\Controllers
你有一個想法,為什么它不起作用?
在我看來,RouteServiceProvider沒有加載,因為當我嘗試放入方法map
或方法boot
die('smth');
它什么都沒做......
這是我的路線服務提供商:
<?php namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider {
/**
* This namespace is applied to the controller routes in your routes file.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function boot(Router $router)
{
parent::boot($router);
//
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
}
}
嘿,我看到沒有人回答你的問題,我不知道你是否已將此標記為已解決但基於我能說的內容:
php artisan route:cache
這意味着Laravel正在查看路由的存儲緩存,而不是您更改的routes.php或路由服務提供程序文件。
這意味着當我在RouteServiceProvider.php
更改map函數時,什么都不會發生
public function map(Router $router, Request $request)
{
/*
$locale = $request->segment(1);
$this->app->setLocale($locale);
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
*/
}
您可能已經通過重新啟動服務器解決了問題,但是,特定的供應商文件可能會為您緩存路由。
php artisan route:clear
之后,對RouteServiceProvider.php
文件的所有更改都應立即生效。 所以你可以訪問/en
但是為了強制你需要在語言中間件中添加前綴。
搜索我認為你正在laracast博客多個locales帖子工作
php artisan make:middleware Language
創建一個中間件並更改其handle()函數 之后只需將語言中間件添加到Kernel.php
中,以便強制Laravel將您路由到/en
protected $middleware = [
'App\Http\Middleware\Language'
];
希望能解決你的問題,遲到總比沒有好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.