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条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd