命名空间路由在 Laravel 5 中不起作用
Namespaced routes not working in Laravel 5
我正在建立一个国际网站,因此我设法让 URL 看起来像 /{language}/{other_stuff}
感谢 RouteServiceProvider
中的一些操作
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
/**
* Internationalization routing system
*/
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) use ($locale) {
if ($locale == 'en') require app_path('Http/routes_en.php');
elseif ($locale == 'el') require app_path('Http/routes_el.php');
});
}
很有魅力。每种语言都会有自己的路由文件,这是一个选择。
假设我们去 /en/
而你是管理员,我在 Http/route_en.php
中创建了另一个名称空间以专注于管理部分:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::controller('', 'DashboardController');
Route::controller('brands', 'BrandsController');
Route::controller('contents', 'ContentsController');
Route::controller('downloads', 'DownloadsController');
Route::controller('news', 'NewsController');
Route::controller('products', 'ProductsController');
Route::controller('settings', 'SettingsController');
Route::controller('users', 'UsersController');
});
所以现在我应该可以轻松访问 /en/admin/brands
等部分,但它失败了。由于 HTML class
,我动态生成了所有链接
{!! HTML::linkAction('Admin\BrandsController@getIndex', 'Brands') !!}
当我转到 /en/admin
时生成工作正常,这意味着此包检测到 Admin\BrandsController@getIndex
,但是当您单击它时
Sorry, the page you are looking for could not be found.
我测试了一些东西,当我只是简单地将路线设置在 group()
之外时,它工作正常。
Route::controller('admin/brands', 'Admin\BrandsController');
我在这里错过了什么? HTML class 和路由系统不应该彼此一致吗?我犯了什么错误吗?也许有问题?
所以没有人试图帮助我。
几天后,一个问题和多次测试我自己明白了问题:你必须把DashboardController
路由放在最后,否则路由系统会先走它而忽略其他的。
/**
* Admin
*/
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'is.admin'], function() {
Route::controller('news', 'NewsController');
Route::controller('brands', 'BrandsController');
Route::controller('products', 'ProductsController');
Route::controller('users', 'UsersController');
Route::controller('downloads', 'DownloadsController');
Route::controller('settings', 'SettingsController');
Route::controller('contents', 'ContentsController');
Route::controller('', 'DashboardController');
});
注意:路由列表中的一切看起来都很好,甚至在 HTML/Form 包中,但事实并非如此。
我把它留给任何有类似问题的人。
我正在建立一个国际网站,因此我设法让 URL 看起来像 /{language}/{other_stuff}
感谢 RouteServiceProvider
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
/**
* Internationalization routing system
*/
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) use ($locale) {
if ($locale == 'en') require app_path('Http/routes_en.php');
elseif ($locale == 'el') require app_path('Http/routes_el.php');
});
}
很有魅力。每种语言都会有自己的路由文件,这是一个选择。
假设我们去 /en/
而你是管理员,我在 Http/route_en.php
中创建了另一个名称空间以专注于管理部分:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::controller('', 'DashboardController');
Route::controller('brands', 'BrandsController');
Route::controller('contents', 'ContentsController');
Route::controller('downloads', 'DownloadsController');
Route::controller('news', 'NewsController');
Route::controller('products', 'ProductsController');
Route::controller('settings', 'SettingsController');
Route::controller('users', 'UsersController');
});
所以现在我应该可以轻松访问 /en/admin/brands
等部分,但它失败了。由于 HTML class
{!! HTML::linkAction('Admin\BrandsController@getIndex', 'Brands') !!}
当我转到 /en/admin
时生成工作正常,这意味着此包检测到 Admin\BrandsController@getIndex
,但是当您单击它时
Sorry, the page you are looking for could not be found.
我测试了一些东西,当我只是简单地将路线设置在 group()
之外时,它工作正常。
Route::controller('admin/brands', 'Admin\BrandsController');
我在这里错过了什么? HTML class 和路由系统不应该彼此一致吗?我犯了什么错误吗?也许有问题?
所以没有人试图帮助我。
几天后,一个问题和多次测试我自己明白了问题:你必须把DashboardController
路由放在最后,否则路由系统会先走它而忽略其他的。
/**
* Admin
*/
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'is.admin'], function() {
Route::controller('news', 'NewsController');
Route::controller('brands', 'BrandsController');
Route::controller('products', 'ProductsController');
Route::controller('users', 'UsersController');
Route::controller('downloads', 'DownloadsController');
Route::controller('settings', 'SettingsController');
Route::controller('contents', 'ContentsController');
Route::controller('', 'DashboardController');
});
注意:路由列表中的一切看起来都很好,甚至在 HTML/Form 包中,但事实并非如此。
我把它留给任何有类似问题的人。