php - 如何从 url 获取大量参数但没有 (&)?
问题描述
现在在 Laravel 我正在测试 url 并且在路由中我有
Route::group(['prefix' => 'game'], function (){
Route::get('/location/{location?}/action/{action?}/screen/{screen?}','GameController@index')->name('game.index');
});
在控制器中,当我想传递参数时,我必须输入
example.com/game/location/1/action/update/screen/main
如果我只想传递位置和屏幕,我在 url 第二个参数中有一个错误原因应该是行动。
我可以创建像
example.com/game/?location=1&screen=main
和控制器 $request->screen 和 location 工作正常。但是有什么方法可以不使用 & 吗?并这样做:
example.com/game/location/1/screen/main
解决方案
对于这条路线
Route::get('/locations/{location?}/actions/{action?}/screens/{screen?}','GameController@index')->name('locations.actions.screens.show');
在 GameController 索引方法中,您可以将这些参数获取为
public function index(Location $location, Action $action, Screen $screen) {
// here you can use those models
}
如果您使用的是路由模型绑定,
如果不使用
public function index($location, $action, $screen) {
// here you can use these variables
}
如果路线名称locations.actions.screens.show
在视图中,它将是
<a href="{{ route('locations.actions.screens.show', ['location' => $location, 'action' => $action, 'screen' => $screen ]) }}">Test</a>
现在,如果您有一些查询参数
那么它就像 $ http://example.com/?test="some test data"&another_test="another test"
您可以访问这些参数,例如
public function myfunction(Request $request) {
dd($request->all());
}
假设您要检索属于特定屏幕的所有游戏,该特定屏幕属于特定操作并且属于特定位置,您的网址在您的问题中似乎是什么,在这种情况下,网址将是
Route::group(['prefix' => 'game'], function (){
Route::get('locations/{location?}/actions/{action?}/screens/{screen?}','GameController@index')->name('game.index');
});
url 似乎是game/locations/1/actions/1/screens/1
action 和 screen 参数可以选择的地方
现在在您的控制器 index() 方法中
public function index(Location $location, Action $action=null, Screen $screen=null) {
//using the model instance you received, you can retrieve your games here
}
推荐阅读
- go - 无法使用 Go 的“文本/模板”库导入变量
- outlook - 需要帮助以修复响应式电子邮件中的 webkit 错误
- django - 当前用户以外的其他用户的 PassWordChangeForm
- java - 我可以在自动化测试期间从 HttpRequest 捕获 JSON 有效负载(JUnit 或带有 Selenium 的 TestNG)吗?
- vba - 我可以在 vba 中使用哪个函数,类似于我在 sql 中使用的函数
- c# - .NET 字典插入的奇怪性能行为
- swift - 带有复选框的 SwiftUI ToDoList?
- javascript - html-webpack-plugin - 脚本注入问题
- android - 条目名称“classes.dex”发生冲突
- javascript - 按顺序异步每一行,等待它返回