doudaiyao0934 2016-05-02 11:28
浏览 312
已采纳

Laravel路由具有RESTful API的无限参数

I am building a RESTful API using Laravel 5.1. The default route would be api. The user is allowed to create a url service using as many as parameters as she wants let's say .../api/p1/p2/.../pn.

How do I make a single route to point to a single Controller, so the service will be handled in a single controller?

Note : At first the application just needs to know whether the service exists or not by comparing the url with stored service in the database. As for the service itself, it can be queried into the database later.

I read that we can use * in Laravel 4, how about Laravel 5.1 ?

I have tried :

Route::resource('/api/*', 'APIServiceController'); but it does not work for unlimited parameters

or is it possible to do it like this

Route::group(['prefix' => 'api'], function () { //what should I put in the closure, how can I redirect it to a single controller });

  • 写回答

2条回答 默认 最新

  • dtvgo28624 2016-05-02 11:44
    关注

    Write your route as below:-

    Route::group(['prefix' => 'api'], function () {
        // this route will basically catch everything that starts with api/routeName 
        Route::get('routeName/{params?}', function($params= null){
            return $params;
        })->where('params', '(.*)');
    });
    

    Redirect to controller,

    Route::group(['prefix' => 'api'], function () {
        Route::get('routeName/{params?}', 'YourController@action')->where('params', '(.*)');
    });
    

    If you want to make routeName dynamic then just write it in curly bracket as below:-

    Route::get('{routeName}/{params?}', 'YourController@action')->where('params', '(.*)');
    

    Hope it will help you :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 代码实现状态连接包过滤防火墙的设计与实现
  • ¥15 vscode的红色箭头爆红和has no default export报错
  • ¥15 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)
  • ¥15 eNSP中基于默认路由及浮动路由的公司与分部互联和校园网综合项目
  • ¥15 主要进行描述泥浆在管路不同区段泥浆的密度不相同,泥浆的密度有高有低,此时管路的摩阻分布需要怎么计算,(标签-matlab)
  • ¥40 通过编制程序计算圆管内层流充分发展对流换热,参数如图5-4,以及公式5-16所示,要求用表5-6对程序计算准确性进行验证
  • ¥20 该学习哪个编程语言? AI会取代程序员吗?
  • ¥15 ensp如何拼通IP地址
  • ¥15 saber软件导入Ibis模型报错
  • ¥15 商城生产日期批次库存问题