Psr-4 Laravel Namespace

1

I am working with Laravel 5.1 in an app that is already running, but I am changing from modules to the repository pattern.

"psr-4": {
    "app\": "app/",
    "Cms\": "app/cms"
}

The first one is the default namespace and the second is what I'm changing and going to that folder. When I call a controller inside app / cms I get the following error:

  

Class app \ Http \ Controllers \ CalendarController does not exist

It's like you never look in app \ cms.

Route::group(['namespace' => 'Calendario'], function() {
       Route::resource('calendario', 'CalendarioController');
});

and the namespace of the controller as:

namespace Cms\Calendario;

Thanks for your help!

    
asked by Juan Pablo B 13.09.2016 в 15:13
source

1 answer

2

Although I do not know how your RouteServiceProvider is, it is most likely that with the code that shows the group of routes you are adding a second namespace to the one that already exists in RouteServiceProvider.

The correct way according to the Laravel documentation would be to add another namespace in RouteServiceProvider and preferably another file of routes (as it is done in Laravel 5.3):

<?php

namespace app\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;

class RouteServiceProvider extends ServiceProvider
{

    protected $namespace = 'app\Http\Controllers';

    protected $cmsNamespace = 'app\cms\Http\Controllers';


    public function boot(Router $router)
    {
        //

        parent::boot($router);
    }

    public function map(Router $router)
    {

        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });


        $router->group(['namespace' => $this->cmsNamespace], function ($router) {
            require app_path('Http/routes.cms.php');
        });

    }
}
    
answered by 13.09.2016 / 15:43
source