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

编程入门 行业动态 更新时间:2024-10-12 20:22:17
本文介绍了具有多个可选参数的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格式如下: example/api/{functionName}/{parameter1}/{parameter2}/.../

The URL format I want this to be accessible looks like this: example/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应如下所示: example/api/sendMsg/sdf879s8/2/hi+there+whats+up

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

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

To access the getMsg() function, the url should look like example/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?

推荐答案

是的,您可以使用资源控制器,它最适合构建API:

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请求使用domain/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 domain/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路由

本文发布于:2023-11-25 23:17:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   可选   路由   参数   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!