Php RESTful API参数不限的Laravel路由

Php RESTful API参数不限的Laravel路由,php,api,rest,laravel-5.1,Php,Api,Rest,Laravel 5.1,我正在使用Laravel5.1构建一个RESTful API。 默认路由为api。用户可以使用任意多的参数创建url服务,比如说../api/p1/p2/../pn 如何创建指向单个控制器的单个路由,以便在单个控制器中处理服务 注意:首先,应用程序只需要通过比较url与数据库中存储的服务来知道服务是否存在。至于服务本身,可以稍后查询到数据库中 我读到我们可以在Laravel4中使用*,那么Laravel5.1呢 我试过: Route::resource('/api/*','APIServiceC

我正在使用Laravel5.1构建一个RESTful API。 默认路由为
api
。用户可以使用任意多的参数创建url服务,比如说
../api/p1/p2/../pn

如何创建指向单个控制器的单个路由,以便在单个控制器中处理服务

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

我读到我们可以在Laravel4中使用
*
,那么Laravel5.1呢

我试过:

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

还是可以这样做

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

你可以试试这个把戏

Route::get('{pageLink}/{otherParams?}', 'IndexController@get')->where('otherParams', '(.*)');
您应该将它放在routes.php文件的末尾,因为它就像一条“一网打尽”的路线

class IndexController extends BaseController {

    public function get($pageLink, $otherParams = null)
    {
        if($otherParams) 
        {
            $otherParams = explode('/', $otherParams);
        }
    }

}
你可以试试这个把戏

Route::get('{pageLink}/{otherParams?}', 'IndexController@get')->where('otherParams', '(.*)');
您应该将它放在routes.php文件的末尾,因为它就像一条“一网打尽”的路线

class IndexController extends BaseController {

    public function get($pageLink, $otherParams = null)
    {
        if($otherParams) 
        {
            $otherParams = explode('/', $otherParams);
        }
    }

}

按以下方式填写您的路线:-

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', '(.*)');
});
Route::get('{routeName}/{params?}', 'YourController@action')->where('params', '(.*)');
重定向到控制器

Route::group(['prefix' => 'api'], function () {
    Route::get('routeName/{params?}', 'YourController@action')->where('params', '(.*)');
});
如果您想使routeName成为动态的,只需将其写在花括号中,如下所示:-

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', '(.*)');
});
Route::get('{routeName}/{params?}', 'YourController@action')->where('params', '(.*)');

希望它能帮助您:-)

将您的路线写在下面:-

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', '(.*)');
});
Route::get('{routeName}/{params?}', 'YourController@action')->where('params', '(.*)');
重定向到控制器

Route::group(['prefix' => 'api'], function () {
    Route::get('routeName/{params?}', 'YourController@action')->where('params', '(.*)');
});
如果您想使routeName成为动态的,只需将其写在花括号中,如下所示:-

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', '(.*)');
});
Route::get('{routeName}/{params?}', 'YourController@action')->where('params', '(.*)');

希望对您有所帮助:-)

谢谢您的回答。“routeName”也是动态创建的,因此我不想通过在路由中对其进行硬编码来逐个定义路由,因为它可能非常多。在测试您的解决方案后,我将
routeName
更改为变量
{slug}
,因此我不必逐个定义路由名称是。如果您将routeName定义为{slug},那么它的名称将变为动态名称。在最佳实践中,您应该在制作API时修复routeName。因此,在大型应用程序中,您可以轻松地调试它。谢谢您的回答。“routeName”也是动态创建的,因此我不想通过在路由中对其进行硬编码来逐个定义路由,因为它可能非常多。在测试您的解决方案后,我将
routeName
更改为变量
{slug}
,因此我不必逐个定义路由名称是。如果您将routeName定义为{slug},那么它的名称将变为动态名称。在最佳实践中,您应该在制作API时修复routeName。因此,在较大的应用程序中,您可以轻松地调试它。谢谢。这是我想要的。处理我从get改为any的任何方法。谢谢。这是我想要的。来处理我从get改为any的任何方法。