【问题标题】:Laravel APIs changing the link of the apiLaravel API 更改 api 的链接
【发布时间】:2019-10-14 06:19:59
【问题描述】:

我的任务是创建 API,但问题是他们需要链接看起来像:

www.domain.com/api/example=1?ex2=2

我知道如何删除 api 前缀,但如果他们需要搜索某些内容,如何创建链接

如果你注意到,它类似于谷歌链接。

【问题讨论】:

  • 你试过什么?您当前的设置是什么?我没有看到任何实际解决问题的尝试。
  • 我是 laravel 新手,为什么需要我的设置?因为我需要他们

标签: php laravel api


【解决方案1】:

只传递路由中未定义的参数。

// We define a route with the param 'param1'
Route::get('something/{param1}/asdf', 'Controller@action')->name('something');
// This is what happens when we pass the defined parameter
route('something', ['param1' => 1])                              // yields: 'something/1/asdf
route('something', ['param1' => 'a_string'])                     // yields: 'something/a_string/asdf
// This is what happens when we pass other parameters we didn't define
route('something', ['param1' => 1, 'param2' => 2])               // yields: 'something/1/asdf?param2=2
route('something', ['param1' => 'a_string', 'param2' => 'qwer']) // yields: 'something/a_string/asdf?param2=qwer

所以对于你的例子:

Route::get('api')->name('api.example');
# and then
route('api.example', ['example' => 1, 'ex2' => 2]); // yields '/api?example=1&ex2=2'

【讨论】:

  • 所以如果我给出例如 route('api.example', ['example => {param}]) 应该解决我的问题?
  • 是的。只需声明路线(不要忘记给它命名),您就可以开始了。 route($route_name, $param_array) 你也可以试试php artisan tinker
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 2017-11-28
  • 2020-01-21
  • 1970-01-01
相关资源
最近更新 更多