【问题标题】:Laravel resource route doesn't display show in controllerLaravel 资源路由不显示在控制器中
【发布时间】:2020-09-04 22:29:18
【问题描述】:

根据 Laravel 文档:

这个单一的路由声明创建了多个路由来处理一个 对资源的各种操作。生成的控制器将 已经为这些操作中的每一个设置了存根方法,包括 通知您它们处理的 HTTP 动词和 URI 的注释。

在我的路线中,我有这个:

Route::resource('admin/companies', 'CompaniesController');

在我的控制器中,我有索引、创建、存储、显示等。 特别是在节目中我有这个:

public function show(Company $company)
{
    //
    dd('hi');
}

当我到达这条路线时,我会期待:

http://127.0.0.1:8000/admin/companies/onecompany

让它添加我的回复。相反,我收到 404 错误。我做错了什么?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    这条路由http://127.0.0.1:8000/admin/companies/onecompany不是根据Laravel documentaion触发资源功能的有效路由。

    触发show(Company $company)函数的正确URL是:

    //This route will extract the `id` column from the model and show the required record.
    http://127.0.0.1:8000/admin/companies/{company}
    or
    http://127.0.0.1:8000/admin/companies/{id}
    

    尝试数据库中的现有记录;

    //Assuming there is a record with an id of 1
    http://127.0.0.1:8000/admin/companies/1
    

    【讨论】:

      【解决方案2】:

      您的问题的原因似乎与Route model binding 有关 ,用 uri 中的 id 试试。

      查看应用程序的路由列表:php artisan route:list

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-04
        • 2014-10-08
        • 2013-08-20
        • 2014-07-08
        • 2018-01-16
        • 2014-02-18
        • 2015-07-20
        • 2018-02-18
        相关资源
        最近更新 更多