【发布时间】:2016-01-14 00:13:58
【问题描述】:
你能帮我解决我的问题吗?我正在使用 laravel5 App/Providers/RouteServiceProvider。我正在尝试根据当前语言添加到路由前缀。例如。:
我有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 routes laravel-5 laravel-routing