我有两个名称空间'Front'
和'Admin'
。对于'Admin'
命名空间,可以使所有路径都以admin.conrtoller.action
为前缀,但对于'Front'
,我希望使用不带前缀URI的带前缀的路由名称。
Route::group(array('namespace' => 'Front'), function()
{
Route::resource('franchising', 'FranchisingController', array('only' => array('index')));
});
这会生成franchising.index
根名称和get 'franchising'
URI。如何使该组中的所有资源生成路径名称,如front.franchising.index
,但保留当前URI不做更改(即不用front/
作为前缀)。
我使用的是Laravel 4.2。
答案 0 :(得分:2)
只需将前缀设置为none:
Route::group(array('namespace' => 'Front', 'prefix'=>''), function()
更新
你应该能够扩展Route类并覆盖这一个函数:
class MyRoute extends \Laravel\Routing\Route {
/**
* Add a prefix to the route URI.
*
* @param string $prefix
* @return \Illuminate\Routing\Route
*/
public function prefix($prefix)
{
$this->uri = trim($this->uri, '/'); // removed the prefix from this line
return $this;
}
}
然后使用它代替正常路线:
MyRoute::group(array('namespace' => 'Front'), function()
更新
扩展可能需要
use Illuminate\Support\Facades\Route;
class MyRoute extends Route {
答案 1 :(得分:1)
路由前缀主要用于为路径添加前缀。它也是路由名称的前缀,这只是使用Route::resource()
时的一个附加行为。
修改强>
使用prefix
路由组参数的实际前缀绝对不可能在Laravel开箱即用。
Route::resource()
和Route::controller()
只是适用于少量实际用例的快捷方式。它们绝对不是适合一切的工具。如果需要对路径进行更多控制,建议使用Route::get
,Route::post
等手动指定路线。
答案 2 :(得分:0)
只是为至少使用Laravel 5.4的新手提供更新的答案,我不确定它何时推出,但您现在可以在定义路由时传递数组选项'as'
以便为所有路由名称添加前缀不影响URI或命名空间。
例如,我从一个新的laravel安装开始,将创建Franchising
模型。
php artisan make:model Franchising
这需要Front
控制器和Admin
控制器访问。
php artisan make:controller --resource --model='Franchising' 'Front\FranchisingController'
php artisan make:controller --resource --model='Franchising' 'Admin\FranchisingController'
这将创建以下文件:
app/
├── ...
├── Franchising.php
├── Http
│ ├── Controllers
│ │ ├── Admin
│ │ │ └── FranchisingController.php
│ │ ├── ...
│ │ └── Front
│ │ └── FranchisingController.php
│ └── ...
└── ...
使用'as'
选项创建资源路由以定义路由名称前缀。
Route::resource(
'franchising',
'Front\FranchisingController',
['as' => 'front']
);
Route::resource(
'admin/franchising',
'Admin\FranchisingController',
['as' => 'admin']
);
您可以使用工匠查看所有路线:
php artisan route:list
+-----------+--------------------------------------+---------------------------+----------------------------------------------------------+ | Method | URI | Name | Action | +-----------+--------------------------------------+---------------------------+----------------------------------------------------------+ | GET|HEAD | admin/franchising | admin.franchising.index | App\Http\Controllers\Admin\FranchisingController@index | | POST | admin/franchising | admin.franchising.store | App\Http\Controllers\Admin\FranchisingController@store | | GET|HEAD | admin/franchising/create | admin.franchising.create | App\Http\Controllers\Admin\FranchisingController@create | | GET|HEAD | admin/franchising/{franchising} | admin.franchising.show | App\Http\Controllers\Admin\FranchisingController@show | | PUT|PATCH | admin/franchising/{franchising} | admin.franchising.update | App\Http\Controllers\Admin\FranchisingController@update | | DELETE | admin/franchising/{franchising} | admin.franchising.destroy | App\Http\Controllers\Admin\FranchisingController@destroy | | GET|HEAD | admin/franchising/{franchising}/edit | admin.franchising.edit | App\Http\Controllers\Admin\FranchisingController@edit | | GET|HEAD | franchising | front.franchising.index | App\Http\Controllers\Front\FranchisingController@index | | POST | franchising | front.franchising.store | App\Http\Controllers\Front\FranchisingController@store | | GET|HEAD | franchising/create | front.franchising.create | App\Http\Controllers\Front\FranchisingController@create | | GET|HEAD | franchising/{franchising} | front.franchising.show | App\Http\Controllers\Front\FranchisingController@show | | PUT|PATCH | franchising/{franchising} | front.franchising.update | App\Http\Controllers\Front\FranchisingController@update | | DELETE | franchising/{franchising} | front.franchising.destroy | App\Http\Controllers\Front\FranchisingController@destroy | | GET|HEAD | franchising/{franchising}/edit | front.franchising.edit | App\Http\Controllers\Front\FranchisingController@edit | +-----------+--------------------------------------+---------------------------+----------------------------------------------------------+
现在针对您的具体用例,我注意到您只在前端控制器上使用了一条路由,因此,除了这种通用解决方案之外,简单地定义一条路线实际上可能更好。
Route::name('front.franchising.index')
->get('franchising', 'Front\FranchisingController@index');
Route::resource(
'admin/franchising',
'Admin\FranchisingController',
['as' => 'admin']
);
生成这些路线:
+-----------+--------------------------------------+---------------------------+----------------------------------------------------------+ | Method | URI | Name | Action | +-----------+--------------------------------------+---------------------------+----------------------------------------------------------+ | GET|HEAD | admin/franchising | admin.franchising.index | App\Http\Controllers\Admin\FranchisingController@index | | POST | admin/franchising | admin.franchising.store | App\Http\Controllers\Admin\FranchisingController@store | | GET|HEAD | admin/franchising/create | admin.franchising.create | App\Http\Controllers\Admin\FranchisingController@create | | GET|HEAD | admin/franchising/{franchising} | admin.franchising.show | App\Http\Controllers\Admin\FranchisingController@show | | PUT|PATCH | admin/franchising/{franchising} | admin.franchising.update | App\Http\Controllers\Admin\FranchisingController@update | | DELETE | admin/franchising/{franchising} | admin.franchising.destroy | App\Http\Controllers\Admin\FranchisingController@destroy | | GET|HEAD | admin/franchising/{franchising}/edit | admin.franchising.edit | App\Http\Controllers\Admin\FranchisingController@edit | | GET|HEAD | franchising | front.franchising.index | App\Http\Controllers\Front\FranchisingController@index | +-----------+--------------------------------------+---------------------------+----------------------------------------------------------+