【问题标题】:Laravel Routing Route::resource inside Route::groupLaravel 路由 Route::resource 内 Route::group
【发布时间】:2017-05-29 12:03:56
【问题描述】:

我是Laravel 的新手。 routes/api.php 这个函数我已经写好了

   Route::group(['namespace' => "Catalogue"],function(){
    Route::resource('product','Product');
});

我已经创建了一个资源控制器:

app/Controllers/Catalogue/Product.php

这是我的索引方法:

public function index()
    {
        $pdo = DB::select('select count(*) from offers');
        return $pdo;
    }

我正在尝试从 url 获取 index 方法的结果:

http://localhost:8000/api/Catalogue/product

但是,这会导致 404 not found。 注意:这部分url没有问题http://localhost:8000/api

【问题讨论】:

  • Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route. -- product 是中间件还是命名空间?

标签: php laravel laravel-5


【解决方案1】:

根据您的路线生成的链接是http://localhost:8000/api/product

如果您需要链接为 http://localhost:8000/api/Catalogue/product ,则将前缀添加到组中。

Route::group(['prefix' => 'Catalogue', 'namespace' => 'Catalogue'], function() {
    Route::resource('product', 'Product');
});

namespace 仅设置控制器的默认命名空间。 prefix 设置组中所有路由的路由前缀。

【讨论】:

    【解决方案2】:

    你打错了 uri。 检查http://localhost:8000/api/product

    组路由中的命名空间意味着您将命名空间分配给一组控制器。正如你在这里看到的https://laravel.com/docs/5.4/routing#route-group-namespaces。它与路线无关。

    当您在控制器中创建其他路由时,您可以在此处看到它们。 https://laravel.com/docs/5.4/controllers#controllers-and-namespaces

    【讨论】:

    猜你喜欢
    • 2014-11-16
    • 2014-06-23
    • 2015-09-01
    • 2018-02-17
    • 2019-06-14
    • 1970-01-01
    • 2014-06-23
    • 2013-10-06
    • 2015-07-22
    相关资源
    最近更新 更多