4

Alright so i've been trying to change my controller path (so my project is more structured) but sadly everytime i try to route with the controller like so:

Route::get('/', ['as' => 'home', 'uses' => 'PagesController@index']);

Now that returns me a error page:

ReflectionException

Class PagesController does not exist

So I think let me psr-4 autoload it and/or add it in the classmap so this is what my composer.json looks like:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
    "laravel/framework": "4.2.*",
    "cartalyst/sentinel": "dev-master",
    "guzzlehttp/guzzle": "4.*",
    "way/generators": "2.*"
},
"repositories": [
    {
        "type": "composer",
        "url": "http://packages.cartalyst.com"
    }
],
"autoload": {
    "classmap": [
        "app/database/migrations",
        "app/database/seeds",
        "app/strifemods/Controllers",
        "app/strifemods/Models"
    ],
    "psr-4": {
        "Strifemods\\": "app/Strifemods"
    }
},
"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ]
},
"config": {
    "preferred-install": "dist"
},
"minimum-stability": "dev",
"prefer-stable": true

}

After that it's still not working so i decide to look over in my dear /vendor/composer/ directory to see if its actually being loaded.

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'CreateSessionTable' => $baseDir . '/app/database/migrations/2014_07_06_213148_create_session_table.php',
    'DatabaseSeeder' => $baseDir . '/app/database/seeds/DatabaseSeeder.php',
    'IlluminateQueueClosure' => $vendorDir . '/laravel/framework/src/Illuminate/Queue/IlluminateQueueClosure.php',
    'MigrationCartalystSentinelAlterThrottle' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_025024_migration_cartalyst_sentinel_alter_throttle.php',
    'MigrationCartalystSentinelAlterUsers' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_023520_migration_cartalyst_sentinel_alter_users.php',
    'MigrationCartalystSentinelInstallActivations' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_014954_migration_cartalyst_sentinel_install_activations.php',
    'MigrationCartalystSentinelInstallGroups' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225929_migration_cartalyst_sentinel_install_groups.php',
    'MigrationCartalystSentinelInstallReminders' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_022418_migration_cartalyst_sentinel_install_reminders.php',
    'MigrationCartalystSentinelInstallThrottle' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225988_migration_cartalyst_sentinel_install_throttle.php',
    'MigrationCartalystSentinelInstallUsers' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225921_migration_cartalyst_sentinel_install_users.php',
    'MigrationCartalystSentinelInstallUsersGroupsPivot' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225945_migration_cartalyst_sentinel_install_users_groups_pivot.php',
    'MigrationCartalystSentinelRenameAlterGroups' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_023945_migration_cartalyst_sentinel_rename_alter_groups.php',
    'MigrationCartalystSentinelRenameAlterGroupsUsersPivot' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_024557_migration_cartalyst_sentinel_rename_alter_groups_users_pivot.php',
    'SessionHandlerInterface' => $vendorDir . '/symfony/http-foundation/Symfony/Component/HttpFoundation/Resources/stubs/SessionHandlerInterface.php',
    'Strifemods\\Controllers\\BaseController' => $baseDir . '/app/strifemods/Controllers/BaseController.php',
    'Strifemods\\Controllers\\PagesController' => $baseDir . '/app/strifemods/Controllers/PagesController.php',
    'Whoops\\Module' => $vendorDir . '/filp/whoops/src/deprecated/Zend/Module.php',
    'Whoops\\Provider\\Zend\\ExceptionStrategy' => $vendorDir . '/filp/whoops/src/deprecated        /Zend/ExceptionStrategy.php',
    'Whoops\\Provider\\Zend\\RouteNotFoundStrategy' => $vendorDir . '/filp/whoops/src/deprecated/Zend/RouteNotFoundStrategy.php',
);

And it is being loaded but it cant find the class, can anyone shed some light on this and help me out ;) would appriciate it.

Rapthera
  • 43
  • 1
  • 3
  • Possible duplicate of [Laravel Controller Subfolder routing](https://stackoverflow.com/questions/18850542/laravel-controller-subfolder-routing) – Yevgeniy Afanasyev Jan 02 '19 at 22:52

2 Answers2

1

The error is technically correct. Although PagesController exists, it only exists in that somewhere there's a class with that name. Since the class is namespaced however, it should be referred to as Strifemods\Controllers\PagesController except for when in the context of Strifemods\Controllers.

Using your routes as an example, you'd do it like so:

Route::get('/', [
    'as' => 'home', 
    'uses' => 'Strifemods\Controllers\PagesController@index'
]);

Should you have sub-namespaces within your controllers, say API and Admin, you can do something like the following:

Route::group(['namespace' => 'Strifemods\Controllers\API'], function()
{

    Route::get('/', [
        'as' => 'api.home', 
        'uses' => 'PagesController@index'
    ]);

});

Route::group(['namespace' => 'Strifemods\Controllers\Admin'], function()
{

    Route::get('/', [
        'as' => 'admin.home', 
        'uses' => 'PagesController@index'
    ]);

});

That allows you to have Strifemods\Controllers\API\PagesController and Strifemods\Controllers\Admin\PagesController. Hope that helps.

ollieread
  • 6,018
  • 1
  • 20
  • 36
0

That is how you can make your project more structured:

Every route file (web.php, api.php ...) declared in a map() method, in a file

\app\Providers\RouteServiceProvider.php

when you mapping a route file you can set a ->namespace($this->namespace) for it, you will see it there among examples.

It means that you can create more files to make your project more structured!

And set different namespaces for each of them.

But I prefer set empty string for the namespace ""

it gives you set your controllers to rout in a native php way, see the example:

Route::resource('/users', UserController::class);
Route::get('/agents', [AgentController::class, 'list'])->name('agents.list');

Now you can double click your controller names in your IDE to get there quickly and conveniently.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191