Reputation: 5045
I have two namespaces 'Front'
and 'Admin'
. For 'Admin'
namespace it is OK to have all paths prefixed with admin.conrtoller.action
, but for 'Front'
I want to have prefixed route names without prefixed URIs.
Route::group(array('namespace' => 'Front'), function()
{
Route::resource('franchising', 'FranchisingController', array('only' => array('index')));
});
This generates me franchising.index
root name and get 'franchising'
URI. How to make all resources in this group to generate route names like front.franchising.index
, but leave current URIs without change (i.e. not prefixing it with front/
).
I use Laravel 4.2.
Upvotes: 2
Views: 6926
Reputation: 40861
Just to provide an updated answer for newcomers using at least Laravel 5.4, I'm not exactly sure when it was introduced, but you can now pass the array option 'as'
when defining routes to prefix all the route names without affecting the URI or namespace.
For example, I'm starting with a fresh laravel installation and will create the Franchising
model.
php artisan make:model Franchising
This needs to be accessed by both a Front
controller and an Admin
controller.
php artisan make:controller --resource --model='Franchising' 'Front\FranchisingController'
php artisan make:controller --resource --model='Franchising' 'Admin\FranchisingController'
This creates the following files:
app/
├── ...
├── Franchising.php
├── Http
│ ├── Controllers
│ │ ├── Admin
│ │ │ └── FranchisingController.php
│ │ ├── ...
│ │ └── Front
│ │ └── FranchisingController.php
│ └── ...
└── ...
Create the resource routes using the 'as'
option to define route name prefixes.
Route::resource(
'franchising',
'Front\FranchisingController',
['as' => 'front']
);
Route::resource(
'admin/franchising',
'Admin\FranchisingController',
['as' => 'admin']
);
You can view all routes with artisan:
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 | +-----------+--------------------------------------+---------------------------+----------------------------------------------------------+
Now for your specific use case, I noticed that you are only using one route on the front controller, so instead of this general solution, it might actually be better to simply define that one route.
Route::name('front.franchising.index')
->get('franchising', 'Front\FranchisingController@index');
Route::resource(
'admin/franchising',
'Admin\FranchisingController',
['as' => 'admin']
);
Which generates these routes:
+-----------+--------------------------------------+---------------------------+----------------------------------------------------------+ | 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 | +-----------+--------------------------------------+---------------------------+----------------------------------------------------------+
Upvotes: 1
Reputation: 2766
Just set the prefix to none:
Route::group(array('namespace' => 'Front', 'prefix'=>''), function()
update:
You should be able to extend the Route class and override this one function:
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;
}
}
Then use it instead of the normal route:
MyRoute::group(array('namespace' => 'Front'), function()
Update:
The extends might need to be
use Illuminate\Support\Facades\Route;
class MyRoute extends Route {
Upvotes: 3
Reputation: 3374
Route prefix is mainly for prefixing the path. The fact that it also prefixes route names, is just an added behavior when using Route::resource()
.
EDIT
It is definitely not possible with the actual prefixing, using a prefix
route group parameter, in Laravel out of the box.
Route::resource()
and Route::controller()
are only a shortcuts for small amount of real use cases, where they fit. They are definitely not tools that fits for everything. If more control over routes is needed, manually specifying routes with Route::get
, Route::post
and such is advisable.
Upvotes: 1