带有多个可选参数的 Laravel 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22649764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:15:21  来源:igfitidea点击:

Laravel routes with multiple optional parameters

phplaravel

提问by Krimson

I am building a RESTful api using Laravel. I am confused on how to do the routing.

我正在使用 Laravel 构建一个 RESTful api。我对如何进行路由感到困惑。

I have the following api controller

我有以下 api 控制器

class APIController extends BaseController{

    public function sendMsg($authid, $roomid, $msg){

    }

    public function getMsg($roomid, $timestamp){

    }
}

The URL format I want this to be accessible looks like this: http://example.com/api/{functionName}/{parameter1}/{parameter2}/.../

我希望可以访问的 URL 格式如下所示: http://example.com/api/{functionName}/{parameter1}/{parameter2}/.../

Here, in the first parameter, I will have the function name which should map to the function in the controller class and following that the parameters the controller needs.

在这里,在第一个参数中,我将拥有应该映射到控制器类中的函数的函数名称,然后是控制器需要的参数。

For example
To access the sendMsg()function, the url should look like this:
http://example.com/api/sendMsg/sdf879s8/2/hi+there+whats+up

例如,
要访问该sendMsg()功能,网址应如下所示:
http://example.com/api/sendMsg/sdf879s8/2/hi+there+whats+up

To access the getMsg()function, the url should look like http://example.com/api/getMsg/2/1395796678

要访问该getMsg()功能,网址应如下所示 http://example.com/api/getMsg/2/1395796678



那么......我如何编写我的路由,以便它可以处理动态数量和不同的参数需要?

I can write one route for each function name like so:

我可以为每个函数名称编写一个路由,如下所示:

Route::get('/api/sendmsg/{authid}/{msg}', function($authid, $msg){
    //call function...
});

and same for the other function. This if fine but is there a way to combine all function to the APIController in one route?

和其他功能相同。如果没问题,但是有没有办法将所有功能组合到一个路由中的 APIController 中?

回答by The Alpha

Yes, you can combine all the function to your APIControllerin one route by using a resourceful controllerwhich is best suited for building an API:

是的,您可以APIController通过使用最适合构建以下功能的资源丰富的控制器将所有功能组合到您的一条路线中API

Route::resource('api' ,'APIController');

But, technically, it's not one route at all, instead Laravelgenerates multiple routesfor each function, to check routes, you may run php artisan routescommand from your command prompt/terminal.

但是,从技术上讲,它根本不是一条路线,而是为每个功能Laravel生成多个routes路线,要检查路线,您可以php artisan routes从命令提示符/终端运行命令。

To, create a resourceful controlleryou may run the following command from your command line:

要创建一个resourceful controller您可以从命令行运行以下命令:

php artisan controller:make APIController

This will create a controller with 6 functions (skeleton/structure only) and each function would be mapped to a HTTPverb. It means, depending on the request type (GET/POST etc) the function will be invoked. For example, if a request is made using http://domain.com/apiusing GETrequest then the getIndexmethod will be invoked.

这将创建一个具有 6 个功能(仅限骨架/结构)的控制器,并且每个功能都将映射到一个HTTP动词。这意味着,将根据请求类型(GET/POST 等)调用该函数。例如,如果使用http://domain.com/apiusing GETrequest 发出请求,getIndex则将调用该方法。

public function getIndex()
{ 
    // ...
}

You should check the documentation for proper understanding in depth. This is known as RESTfulapi.

您应该检查文档以获得正确的深入理解。这称为RESTfulapi。