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.
Related
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 a route with parameter
Route::get('forum/{ques}', "ForumQuestionsController#show");
Now I want a route something like
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
well when I hit localhost:800/forum/add I get routed to ForumQuestionsController#show instead of ForumQuestionsController#add
Well I know I can handle this in show method of ForumQuestionsController and return a different view based on the paramter. But I want it in this way.
First give this one
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
Then the following
Route::get('forum/{ques}', "ForumQuestionsController#show");
Another Method (using Regular Expression Constraints)
Route::pattern('ques', '[0-9]+');
Route::get('forum/{ques}', "ForumQuestionsController#show");
If ques is a number it will automatically go to the show method, otherwise add method
You can adjust the order of routes to solve the problem.
Place add before show , and then laravel will use the first match as route .
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
Route::get('forum/{ques}', "ForumQuestionsController#show");
I think your {ques} parameter do not get properly. You can try this:
Route::get('forum/show/{ques}', "ForumQuestionsController#show");
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
If you use any parameters in show method add parameters:
public function show($ques){
}
I have the following in my routes.php
Route::resource('g', 'GameController');
I link to these generated routes via HTML::linkRoute('g.index', 'Title', $slug) which produces a link to http://domain/g/demo-slug
What I am looking to do is verify if it is possible to have the prefix be declared in one place so I'm not hunting for links if a URL structure were to change.
For example, I would want to change http://domain/g/demo-slug to http://domain/video-games/demo-slug
I was hoping to use the similar functionality with the standard routes, but that does not seem to be available to resource routes.
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#getUpdated'));
Route::group() takes a 'prefix'. If you put the Route::resource() inside, that should work.
Tangent, I find this reads better:
Route::get('/', array('uses' => 'HomeController#getUpdated', 'as' => 'home'));
As far as I know it's true you can't have named routes for a resource controllers (sitation needed) but you can contain them in a common space using Route::group() with a prefix. You can even supply a namespace, meaning you can swap out an entire api with another quickly.
Route::group(array(
'prefix' => 'video-games',
'before' => 'auth|otherFilters',
'namespace' => '' // Namespace of all classes used in closure
), function() {
Route::resource('g', 'GameController');
});
Update
It looks like resource controllers are given names internally, which would make sense as they are referred to internally by names not urls. (php artisan routes and you'll see the names given to resource routes).
This would explain why you can't name or as it turns out is actually the case, rename resource routes.
I guess you're probably not looking for this but Route:group is your best bet to keep collections of resources together with a common shared prefix, however your urls will need to remain hard coded.
You can give custom names to resource routes using the following syntax
Resource::route('g', 'GameController', ['names' => [
'index' => 'games.index',
'create' => 'games.create',
...
]])
This means you can use {!! route('games.index') !!} in your views even if you decided to change the URL pattern to something else.
Documented here under Named resource routes
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'));