H
H
Hatchet7872019-08-28 14:11:53
Laravel
Hatchet787, 2019-08-28 14:11:53

Laravel. How to fix error when using controller "Class does not exist"?

You need to build a site on Laravel with a modular system. I found a guide, made a "base" as written, in order to calmly study the material. It didn't work out.
In general, I created a folder at the root with the following structure:

Modules folder
5d6659d5d6a86267917322.png

Here are the changes I made in the standard folders:
Created a provider for connecting modules App\Providers\ModulesServiceProvider.php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ModulesServiceProvider extends ServiceProvider {
  public function boot() {
    $modules = config("modules.modules");

    if ($modules) {
      foreach($modules as $module) {
        // Routes
        if (file_exists(__DIR__.'/../../Modules/'.$module.'/Routes/routes.php')) {
          $this->loadRoutesFrom(__DIR__.'/../../Modules/'.$module.'/Routes/routes.php');
        }
        // Views
        if (is_dir(__DIR__.'/../../Modules/'.$module.'/Views')) {
          $this->loadViewsFrom(__DIR__.'/../../Modules/'.$module.'/Views', $module);
        }
        // Migration
        if (is_dir(__DIR__.'/../../Modules/'.$module.'/Migration')) {
          $this->loadMigrationsFrom(__DIR__.'/../../Modules/'.$module.'/Migration');
        }
        // Lang
        if (is_dir(__DIR__.'/../../Modules/'.$module.'/Lang')) {
          $this->loadTranslationsFrom(__DIR__.'/../../Modules/'.$module.'/Lang', $module);
        }
      }
    }
  }
}

Connected it in config\app.php

'providers' => [
  ...,
  App\Providers\ModulesServiceProvider::class,
  ...
];

In the new settings file, added a list of modules config/modules.php
return [
  'modules' => [
    'Test',
  ]
];

Created a Controller Modules\Test\Controllers\TestController.php
namespace Modules\Test\Controllers;

use Illuminate\Routing\Controller;

class TestController extends Controller {
  public function index() {
    
    return view('Test::index');
  }
}

I tried to specify the route in two ways
First
Route::namespace('Modules\Test\Controllers')->get('/', '[email protected]');
Second
Route::group(['namespace' => 'Modules\Test\Controllers'], function(){
  Route::get('/', '[email protected]');
});
But none worked.
The routing works, but when it comes to the controller, Laravel can't find the TestController class. Hope someone can tell me what the problem is.
UPD:
Full text of the error

5d666911969c6034233512.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Wells, 2019-08-28
@Hatchet787

composer.json:

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Modules\\": "Modules/"
        },
    },

A
ajaxtelamonid, 2019-08-28
@ajaxtelamonid

Route::namespace('\Modules\Test\Controllers') ? Is he at the root?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question