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

后端 未结 3 1470
-上瘾入骨i
-上瘾入骨i 2020-12-06 17:22

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(\'/\'         


        
相关标签:
3条回答
  • 2020-12-06 17:25

    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'));
    
    0 讨论(0)
  • 2020-12-06 17:26

    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

    0 讨论(0)
  • 2020-12-06 17:40

    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.

    0 讨论(0)
提交回复
热议问题