具有多个可选参数的Laravel路由 [英] Laravel routes with multiple optional parameters

查看:67
本文介绍了具有多个可选参数的Laravel路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有以下api控制器

class APIController extends BaseController{

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

    }

    public function getMsg($roomid, $timestamp){

    }
}

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

The URL format I want this to be accessible looks like this: 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.

例如
要访问sendMsg()函数,URL应如下所示:
http://example.com/api/sendMsg/sdf879s8/2/hi+there+whats+up

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

要访问getMsg()函数,URL应该类似于 http://example.com/api/getMsg/2/1395796678

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


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


So... how can I write my routes so that it can handle the dynamic number and different parameters need?

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

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

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

,其他功能相同.如果可以,但是是否可以通过一种途径将所有功能组合到APIController?

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

推荐答案

是的,您可以使用

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

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

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

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

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

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

php artisan controller:make APIController

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

This will create a controller with 6 functions (skeleton/structure only) and each function would be mapped to a HTTP verb. 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/api using GET request then the getIndex method will be invoked.

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

您应该检查文档以深入了解正确的知识.这称为RESTful api.

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

这篇关于具有多个可选参数的Laravel路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆