Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple routes in single Route::get() call Laravel 4

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'));
like image 885
AndrewMcLagan Avatar asked Jul 05 '13 12:07

AndrewMcLagan


People also ask

How can I create multiple route in Laravel?

Just add another fileGo to your App/Providers/RouteServiceProvider and find the map() method. Here the Service Provider will map your Routes. A quick glance on the file and you will note that API and Web routes are mapped using other methods. * Define the routes for the application.

Can a request have two same routes?

As for having two routes concurrently matching parameters at the root level, that's just impossible as the first route defined will receive all incoming requests, while the second will receive none, simply because there is no way to tell a :session parameter from a :name parameter.

How do you use GET and POST for the same route in Laravel?

Route::get('login', 'AuthController@getLogin'); Route::post('login', 'AuthController@postLogin'); So instead of having to write both you only have to write one since their both using the 'same' method but also the URL remains as site.com/login instead of a redirect to site.com/auth/login ?


3 Answers

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.

like image 119
graemec Avatar answered Nov 11 '22 01:11

graemec


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

like image 25
Oluwatobi Samuel Omisakin Avatar answered Nov 11 '22 00:11

Oluwatobi Samuel Omisakin


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'));
like image 5
Markus Hofmann Avatar answered Nov 11 '22 00:11

Markus Hofmann