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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?