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.
Related
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!
I have made two routes:
Single tour route:
Route::get('{category}/{slug}',
['as' => 'single.tour',
'uses' => 'PublicController#singleTour'])
->where('category', '[A-Za-z\d\-\_]+')
->where('slug', '[A-Za-z\d\-\_]+');
Travel guide route:
Route::get('{pcategory}/{slug}',
['as' => 'travel-guide',
'uses' => 'PublicController#getTravelguide'])
->where('pcategory', '[A-Za-z\d\-\_]+')
->where('slug', '[A-Za-z\d\-\_]+');
both with Regular Expression Constraints but still the second route travel-guide is being redirected to view of first route single.tour. I tried replacing {travel-guide} with travel-guide [static] but still getting same problem. Though I found solution [worst idea] the solution by attaching any extensions like (php,html,jsp,aspx,css....etc) to the url example below:
Route::get('{pcategory}/{slug}.php',
['as' => 'travel-guide',
'uses' => 'PublicController#getTravelguide'])
->where('pcategory', '[A-Za-z\d\-\_]+')
->where('slug', '[A-Za-z\d\-\_]+');
It works well returning its own view. But this is not the best solution. Can anyone suggest me solution to this problem ?
I have the following route in my laravel route file:
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
The problem with the above route is , its overriding the below route:
Route::resource('admin', 'adminController');
I want to keep my resource route, but how do i keep my resource ? is there a way around this ??
Modify your route file like this.
Route::resource('admin', 'adminController');
Route::get('/{id}' , [ 'uses' => 'PagesController#show', 'as' => 'getArticle' ]);
Route files executed in the order they are defined.
If you define Route::get('/{id}',.... in the beginning and set your url like http://your-site/admin, the admin section will be considers as the id for the Route::get('/{id}',.... route. So you need to keep it in your mind when you define your route.
just move this route in the end of the web.php file.
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
There are two options:
move Route::get('/{id}', ...) after Route::resource(...)
or add a pattern to Route::get() if id is numeric Route::get('/{id}', ...)->where('id', '[0-9]+');
I'm creating a CMS that allows the user to define categories. Categories can either have additional categories under it or pages. How can I create a route in Laravel that will support a potentially unlimited number of URI segments?
I've tried the following....
Route::get('/resources/{section}', ['as' => 'show', 'uses' => 'MasterController#show']);
I also tried making the route optional...
Route::get('/resources/{section?}', ['as' => 'show', 'uses' => 'MasterController#show']);
Keep in mind, section could be multiple sections or a page.
First, you need to provide a regular expression to be used to match parameter values. Laravel router treats / as parameter separator and you must change that behaviour. You can do it like that:
Route::get('/resources/{section}',
[
'as' => 'show',
'uses' => 'MasterController#show'
])
->where(['section' => '.*']);
This way, whatever comes after /resources/ and matches the regular expression will be passed to $section variable in your controller.
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'));