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 ?
Related
It is a very strange situation. Some of my routes doesn't want to be translated. I am using the Laravel mcamara/laravel-localization package.
The routes are translated as described in the documentation.
On my website I have a flag for each translation to change the language. By hover on the flags, it changes only the language, but does not translate the route. The strange thing, is that for some routes it works, but for some not and I cannot find out what is the logic.
For example I have one controller for displaying articles. Here are the routes:
Route::get(LaravelLocalization::transRoute('routes.artciles').'/{id}/{slug}.html', ['as' => 'strategy.show', 'uses' => 'ArticlesController#show']);
Route::get(LaravelLocalization::transRoute('routes.artciles').'/{category}.html', ['as' => 'strategy.category', 'uses' => 'ArticlesController#category']);
Route::get(LaravelLocalization::transRoute('routes.artciles').'.html', ['as' => 'strategy.index', 'uses' => 'ArticlesController#index']);
Here it fails to translate the routes?
By hovering I see just:
en/articles.html, de/articles.html instead of en/articles.html de/artikel.html.
I have found the solution. Here it is for somebody who has similar problem:
in Http/routes.php the code must be without the parameters:
Route::get(LaravelLocalization::transRoute('routes.article'), ['as' => 'strategy.show', 'uses' => 'ArticlesController#show'])->where(['id' => '[0-9]+']);
Route::get(LaravelLocalization::transRoute('routes.article_cat'), ['as' => 'strategy.category', 'uses' => 'ArticlesController#category']);
Route::get(LaravelLocalization::transRoute('routes.articles'), ['as' => 'strategy.index', 'uses' => 'ArticlesController#index']);
And in the lang/routes.php you can include the parameters like this:
'articles' => 'help.html',
'article' => 'help/{id}/{slug}.html',
'article_cat' => 'help/{category}.html',
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){
}
Consider the following:
Route::get('/', ['as' => 'home.index', 'uses' => 'HomeController#index']);
Route::group(['domain' => 'thechildandthepoet' . env('CONNECTION')], function() {
Route::get('/', ['as' => 'thechildandthepoet.home', 'uses' => 'GameController#index']);
});
When I go to thechildandthepoet.example.local It shows me the contents of
Route::get('/', ['as' => 'home.index', 'uses' => 'HomeController#index']);
completely by passing the fact that I told it which controller to use.
The link looks like: <li>The Child And The Poet</li>
any idea why this doesn't work?
Laravel's router executes the first route that matches given URL.
You don't specify the domain for your first route, therefore it matches all domains. The second route, even though it matches the URL as well, is ignored.
Reorganize your routes.php file, put the routes that specify the domain at the beginning and keep the most generic routes at the end.
I'm doing a simple project. I want it to be as minimal as possible so I tried to create system where I can create pages and they're placed at localhost/{page?}
But, here's the problem. I also want some routes to be defined (e.g. route /blog) like below.
Route::get('/{page?}', ['as' => 'root', 'uses' => 'SiteController#getRoot']);
Route::get('blog/{slug?}', ['as' => 'blog.post', 'uses' => 'BlogController#getPost']);
Route::get('blog/page/{page}', ['as' => 'blog.page', 'uses' => 'BlogController#getPage'])->where('page', '[0-9]+');
With this setup, it only uses the first route.
My question is. Is there a way to do this? Or, is this beyond capabilities of Laravel?
Thank you for any help.
Yeah, place your first route as the last route. That way it will get picked up last. You may also need to place the blog/{slug?} before that one as well so blog/slug/{page} is first.
Route::get('blog/page/{page}', ['as' => 'blog.page', 'uses' => 'BlogController#getPage'])->where('page', '[0-9]+');
Route::get('blog/{slug?}', ['as' => 'blog.post', 'uses' => 'BlogController#getPost']);
Route::get('/{page?}', ['as' => 'root', 'uses' => 'SiteController#getRoot']);
Basically what happens is the most basic route is picking up the other routes because there is no reason for it not to and it technically matches the route even though it's not the route you want. Putting the most specific routes first usually handles this issue.
Try reordering them:
Route::get('blog/page/{page}', ['as' => 'blog.page', 'uses' => 'BlogController#getPage'])->where('page', '[0-9]+');
Route::get('blog/{slug?}', ['as' => 'blog.post', 'uses' => 'BlogController#getPost']);
Route::get('/{page?}', ['as' => 'root', 'uses' => 'SiteController#getRoot']);
otherwise they get "overwritten"
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'));