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

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 });

图片转代码服务由CSDN问答提供 功能建议

我正在使用Laravel 5.1构建RESTful API。 默认路由为 api 。 允许用户使用她想要的参数创建网址服务,例如 ... / api / p1 / p2 /.../ pn

如何将单个路由指向单个Controller,以便在单个控制器中处理该服务?

注意:首先,应用程序只需要知道是否 通过将 url 与数据库中存储的服务进行比较,服务是否存在。 至于服务本身,可以稍后查询到数据库。

我读到我们可以在Laravel 4中使用 * ,Laravel 5.1怎么样? / p>

我试过了:

Route :: resource('/ api / *','APIServiceController'); 但是 它不适用于无限参数

或者是否可以这样做

Route :: group(['prefix' =>'api'],function(){ //我应该在闭包中放什么,如何将其重定向到单个控制器 });

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    已采纳该答案
    打赏 评论
  • dscdttg4389 2016-05-02 11:44

    You can try this trick

    Route::get('{pageLink}/{otherParams?}', 'IndexController@get')->where('otherParams', '(.*)');
    

    You should put it on the end of routes.php file as it is like a 'catch all' route.

    class IndexController extends BaseController {
    
        public function get($pageLink, $otherParams = null)
        {
            if($otherParams) 
            {
                $otherParams = explode('/', $otherParams);
            }
        }
    
    }
    
    打赏 评论

相关推荐 更多相似问题