LARAVER-如何导航到子目录向左的参数到一个URL
我必须测试这条路线,但我不确定如何导航。
Route::get('/{type}/Foo/{page}/{date}', 'FooController@index');
我知道URL通常在URL中的参数之前定义了子目录。
我曾与此示例这样的URL工作
Route::get('/Foo/{type}', 'FooController@index');
它的终点看起来像
/foo?type = bar
是否有人知道如何测试以上一条路由?
I have to test this Route, but I am not sure how to navigate to it.
Route::get('/{type}/Foo/{page}/{date}', 'FooController@index');
I understand that URLs usually have subdirectories defined before the parameters in the URL.
I have worked with URLs like this example
Route::get('/Foo/{type}', 'FooController@index');
which would have an endpoint that looks like
/Foo?type=bar
Does anybody know how to test a route such as the one above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我认为您需要清除路由和查询参数之间的区别。
在您的情况下,您正在尝试使用路由参数,实际上看起来像:
/{type}/foo/{page}/{date}/{date}
=>/myType/foo/15/12-11-2021
laravel将单词视为
{}
作为变量,您可以通过请求检索,以便您可以做类似的事情。将您将字符串
myType
作为输出返回。在您过去尝试过的第二种情况下,您是指查询参数,这也是
$ request
的一部分。将其视为GET请求的“正文”,但我并不是要以任何方式转换您的帖子路线以获取:)第二个URL的事情是:
/foo/{type}
与/foo?type = bar
不相似,它应该像:
/foo/bar
在一般查询参数中是您要发送可选字段的大部分时您的获取端点中的时间(用于过滤,分页等)和路由参数是针对导致子目录的强制性字段
的要记住的是,您必须谨慎对路线,因为变量可能与其他路线冲突。
例如,看起来像这样的路线:
会发生冲突,因为Laravel会将
bar
作为fooid
的变量,因此您的第二个路由永远无法访问。解决方案的方法是正确订购路线:
因此,当您将作为路线参数作为其他任何东西时,您将进入第二个路线并按预期工作。
Well i think that you need to clear out a bit the difference between route and query parameters.
In your case you are trying to use route parameters which will actually look something like:
/{type}/Foo/{page}/{date}
=>/myType/Foo/15/12-11-2021
Laravel considers the words inside
{}
as variables that you can retrieve via request so you can do something like:and laravel will return you the string
myType
as output.In your second case that you have tried in the past you are referring to query parameters which are also a part of the
$request
. Consider it as the "body" of a GET request but i don't mean in any way to convert your post routes to GET :)The thing with your second url is that:
/Foo/{type}
is not similar to/Foo?type=bar
instead it should be like:
/Foo/bar
In general query parameters are when you want to send an optional field most of the times in your GET endpoint (for filtering, pagination etc etc) and the route parameters are for mandatory fields that lead to sub-directories for example
/Foo/{FooId}/Bar/{BarId}
The thing to remember is that you must be careful about your routes because variables can conflict with other routes.
For example a route looking like this:
will conflict because laravel will consider
bar
as the variable of thefooId
so your second route can never be accessed.The solution to this is to order your routes properly like:
So when you give as a route parameter anything else than bar your will go to your second route and have it working as expected.