【问题标题】:Laravel - How to assign relative path for subfolder controller?Laravel - 如何为子文件夹控制器分配相对路径?
【发布时间】:2013-12-16 21:49:47
【问题描述】:

我将 laravel 4.0 用于我的 Web 服务项目。我尝试为控制器子文件夹分配相对路径,但仍然收到错误消息。

这是我的路由器的样子

Route::group(array('prefix' => 'merchant'), function()
{
    Route::resource('index', 'ProductController@showIndex');
    Route::resource('product', 'CategoryController@showIndex');
    Route::resource('general', 'GeneralController@showIndex');
});

当前路径

/app/controllers/ProductController.php

我想成为这样的人

/app/controllers/merchant/ProductController.php

非常感谢。

【问题讨论】:

    标签: rest laravel-4


    【解决方案1】:

    您需要namespace 来实现这一点。

    在您的控制器文件夹中创建一个名为 merchant 的目录并将您的 ProductController.php 放在 Merchant 目录中。

    然后打开您的ProductController.php 并在文件顶部使用以下命名空间。

    <?php namespace Merchant;
    
    class ProductController extends /BaseController 
    {
    

    然后编辑你的路由文件:

     Route::get('index', 'Merchant\ProductController@showIndex');
    

    删除Route::group(array('prefix' =&gt; 'merchant'), function()。当您有多个路由的公共 url 时使用的前缀。

    例如:

    http:://laravel.com/xyz/products
    http:://laravel.com/xyz/category
    http:://laravel.com/xyz/posts
    

    这里xyz 在每个网址中都很常见。所以,在这种情况下,您可以使用前缀为xyz的组路由

    还有一件事,我可以看到,您使用了资源控制器。

        Route::resource('index', 'ProductController@showIndex');
        Route::resource('product', 'CategoryController@showIndex');
        Route::resource('general', 'GeneralController@showIndex');
    

    你知道吗,默认情况下,对于资源控制器,Laravel 会生成 7 个路由。所以,在使用资源控制器的时候不需要创建@showIndex函数。

    Route::resource('index', 'ProductController');
    Route::resource('product', 'CategoryController');
    Route::resource('general', 'GeneralController');
    

    更多关于资源控制器:

    http://laravel.com/docs/controllers#resource-controllers

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 1970-01-01
      • 2018-11-20
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多