I'm using Laravel 5.5 and wonder if I can not only group routes by name prefixes and route prefixes but also controllers since they all use the same controller. The documentation didn't provide anything the like. In Laravel 4 there were implicit controllers available by using Route::controller() but that's not what I'm looking for since this won't be implicit. To cut a long story short, here's what I currently have:
Route::group([ 'prefix' => 'my-route', 'as' => 'myRoute.' ] , function () {
Route::get('/{viewMode?}', 'MyRouteController#index')->name('index')->where('viewMode', '[a-z]+');
Route::get('/ajax', 'MyRouteController#ajax')->name('ajax');
});
And it should look something like that:
Route::group([ 'prefix' => 'my-route', 'as' => 'myRoute.', 'controller' => 'MyRouteController' ] , function () {
Route::get('/{viewMode?}', 'index')->name('index')->where('viewMode', '[a-z]+');
Route::get('/ajax', 'ajax')->name('ajax');
});
Is there a way to achieve this kind of behavior?
Thanks in advance!
Related
I need to have routes group with prefix and middlewares all in one but not sure how to do so? In laravel documents there is no such complex sample.
Here is what I want:
Route::group({Prefix}, {middleware}, function(){...});
I am aware that I can add middlewares at the end of my route groups like:
Route::group({Prefix}, function(){...})->middleware('xxxx');
But I like the shape of first sample (all in one at the top of group).
So anyone can help to figure that out?
Try something like this,
Route::group(['middleware' => 'cors', 'prefix' => '/v1/test'], function () {
Route::post('/', 'Admin\testController#create');
Route::post('/list', 'Admin\testController#list');
Route::post('/view', 'Admin\testController#view');
Route::post('/update', 'Admin\testController#update');
});
Try below code.
if you use multiple middleware.
Route::group(['prefix' => 'admin', 'middleware' => ['auth','admin']], function() {
});
I want to pretty-up my routes, ie, I have such entries:
// DataTable
Route::get('dt/reservations/{room_id]', 'DataTablesController#reservations')->where(['room_id', '[0-9]+']);
Route::get('dt/rooms/{area_id]', 'DataTablesController#rooms')->where(['area_id', '[0-9]+']);
Route::get('dt/departments', 'DataTablesController#departments');
Route::get('dt/addresses', 'DataTablesController#areas');
Route::get('dt/areas', 'DataTablesController#areas');
I would like to make it more understandable. I can add prefix what would give me:
// DataTable
Route::group(['prefix' => 'dt'], function () {
Route::get('reservations/{room_id]', 'DataTablesController#reservations')->where(['room_id', '[0-9]+']);
Route::get('rooms/{area_id]', 'DataTablesController#rooms')->where(['area_id', '[0-9]+']);
Route::get('departments', 'DataTablesController#departments');
Route::get('addresses', 'DataTablesController#areas');
Route::get('areas', 'DataTablesController#areas');
});
But can I somehow make the rest too? The route name and method name will always be the same.
Is it possible to make something like:
// DataTable
Route::group(['prefix' => 'dt'], function () {
Controller => DataTablesController,
Methods => [
'reservations',
'rooms',
'departments',
'addresses',
'areas'
];
});
Although a very good feature. But it can't be done in Laravel
All your routes must be explicit, Laravel won't/can't assume that you
are using same controller for all the routes.
So you will have to define all the routes explicitly.
Only Resource Controllers can have implicit routing in Laravel
Take a look here....
Route use the same controller
I have a Laravel 5.2 application where I want to display the same page on 2 different domains / routes. I have it working using the following route structure:
The routes to my primary domain:
Route::group(['domain' => 'www.primarydomain.com',
'prefix' => 'demo-page']), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The routes to my secundary domain (note: no prefix!):
Route::group(['domain' => 'www.secundarydomain.com',]), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The idea is that both routes will work, and they do. Both www.secundarydomain.com/my-page and www.primarydomain.com/demo-page/my-page work.
The issue is when I now want to generate a link to my second page. For building my URL's in my views, I'm using the following function to generate a link to my-second-page:
url('/my-page')
This function always generates a link to www.primarydomain.com/my-page, while I need a link to www.primarydomain.com/demo-page/my-page.
Is there any easy solution to resolve this? Can this be resolved using middleware, or will a custom URL function be needed?
Expected results:
url('my-page') on www.primarydomain.com should generate a link to www.primarydomain.com/demo-page/my-page
url('my-page') on www.secondarydomain.com should generate a link to www.secondarydomain.com/my-page
Easiest way to do that is to create your own helper, like custom_url() and use it instead of url().
You can look how original url() helper works and create similar one. Look here:
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/helpers.php#L806
You can assign aliases to your routes.
Route::group(['domain' => 'www.primarydomain.com', 'prefix' => 'demo-page']), function(){
Route::get('/my-page', [
'as' => 'my_page_prefixed',
'uses' => 'MyController#index'
]);
Route::get('/my-second-page', [
'as' => 'my_second_page_prefixed'
'uses' => 'MyController#getPageTwo'
]);
}
And then you can call your aliased route on your blade templates by using {{ route('my_page_prefixed') }} or any other alias.
How do you add an regexp on a Route::group? On a normal Route::verb you add ->where('segment', 'regex') to the end, but how do you do it on a group?
I would like something like this (not working as ->where() on Route::group is invalid):
Route::group(['prefix' => '{profileId}'], function(){
Route::get('/', [
'as' => 'profileShow',
'uses' => 'ProfileController#getShow'
]);
Route::post('ny-anvandare', [
'as' => 'profileAccessNew',
'uses' => 'ProfileController#getAccess'
]);
})->where('profileId', '[0-9]+');
You'll need to use the Enhanced Router package by Jason Lewis: https://github.com/jasonlewis/enhanced-router
It introduces much requested enhancements to Laravel's Routing components, including regex filters on groups.
I am not 100% sure if it is Laravel 4.1-ready, but if you're using 4.0, you should be good to go.
When defining a route in Laravel 4 is it possible to define multiple URI paths within the same route?
presently i do the following:
Route::get('/', 'DashboardController#index');
Route::get('/dashboard', array('as' => 'dashboard', 'uses' => 'v1\DashboardController#index'));
but this defeats my purpose, i would like to do something like
Route::get('/, /dashboard', array('as' => 'dashboard', 'uses' => 'DashboardController#index'));
I believe you need to use an optional parameter with a regular expression:
Route::get('/{name}', array(
'as' => 'dashboard',
'uses' => 'DashboardController#index')
)->where('name', '(dashboard)?');
* Assuming you want to route to the same controller which is not entirely clear from the question.
* The current accepted answer matches everything not just / OR /dashboard.
I find it interesting for curiosity sake to attempt to solve this question posted by #Alex as a comment under #graemec's answer to post a solution that works:
Route::get('/{name}', [
'as' => 'dashboard',
'uses' => 'DashboardController#index'
]
)->where('name', 'home|dashboard|'); //add as many as possible separated by |
Because the second argument of where() expects regular expressions so we can assign it to match exactly any of those separated by | so my initial thought of proposing a whereIn() into Laravel route is resolved by this solution.
PS:This example is tested on Laravel 5.4.30
Hope someone finds it useful
If I understand your question right I'd say:
Use Route Prefixing: http://laravel.com/docs/routing#route-prefixing
Or (Optional) Route Parameters: http://laravel.com/docs/routing#route-parameters
So for example:
Route::group(array('prefix' => '/'), function() { Route::get('dashboard', 'DashboardController#index'); });
OR
Route::get('/{dashboard?}', array('as' => 'dashboard', 'uses' => 'DashboardController#index'));