Laravel 4 route with unlimited number of parameters

眉间皱痕 提交于 2019-11-30 12:59:36

You can try something like this:

//routes.php
Route::get('{pageLink}/{otherLinks?}', 'SiteController@getPage')->where('otherLinks', '(.*)');

Remember to put the above on the very end (bottom) of routes.php file as it is like a 'catch all' route, so you have to have all the 'more specific' routes defined first.

//controller 
class SiteController extends BaseController {

    public function getPage($pageLink, $otherLinks = null)
    {
        if($otherLinks) 
        {
            $otherLinks = explode('/', $otherLinks);
            //do stuff 
        }
    }

}

This approach should let you use unlimited amount of params, so this is what you seem to need.

Aleksei Odegov

@Fusion https://laravel.com/docs/5.4/routing

You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained. so {id} is a route parameter , and ->where('id', '[0-9]+') is a regex expression for this parameter. If u need to use more than parameters you can do something like this :

Route::get('user/{id}/{id2}', function ($id) { })->where('id', '[0-9]+')->where('id2', '[[0-9]+]');


    Route::get('user/{id}', function ($id) {

    })->where('id', '[0-9]+');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!