【问题标题】:Difference Between index.php?id=1 and index.php/id/1index.php?id=1 和 index.php/id/1 之间的区别
【发布时间】:2019-04-27 16:20:37
【问题描述】:
  1. 如果我想创建 RESTful API,我应该选择哪一个?
  2. 作为 index.php/id/1 的 URL 是如何工作的?我认为这是一个文件路径,而不是 URL。
  3. 如果我想获取 abc.com/img/1.png 格式的图像,它可能与 abc.com/img/{param} 有冲突。我该如何解决?

顺便说一句,我现在使用 Laravel。

非常感谢。

【问题讨论】:

标签: laravel rest url nginx laravel-routing


【解决方案1】:

区别在于路由模型绑定

https://laravel.com/docs/5.7/routing#route-model-binding

这使您可以获取带有传递到路由中的 id 的模型

例如这样的路线:

Route::get('users/{user}', 'UsersController@getUser');

将允许您在您的方法中执行此操作:

use App\User

public function getUser(User $user) {

    return $user;

}

这意味着您可以获得路径中 id 的完整记录。

所以你的问题:

1:我会用它来发送模型 ID 2:路由中的变量按顺序传递给允许您访问它们的方法。 3:您需要小心您的路线,因为您可能会遇到冲突。话虽如此,Laravel 并没有使用传统的目录结构进行存储。我相信,如果您有一个 /public/img 文件夹结构并且该文件夹包含一个名为 1.png 的 img ,它将获取图像,但我尚未对此进行测试。

【讨论】:

    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2010-10-01
    • 1970-01-01
    相关资源
    最近更新 更多