简体   繁体   中英

Namespacing issue after upgrading to Laravel 5

I am trying to update our application to Laravel 5.1 from 4.2. We are going for a full upgrade from the start. We were using psr-4 namespaces in 4.2 so this wasn't expected to fail nor we were able to find its origin either, all seems fine. All our endpoints (domains, we have 4 in total) are facing the same issue on accessing any route.

App Directory Structure

Console
    Commands
        BackTools
        Consumer
        [Other Endpoints]
Events
Exceptions
Http
    Controllers
        BackTools
        Consumer
        [Other Endpoints]
    Middleware
        BackTools
        Consumer
        [Other Endpoints]
    Requests
    Routes
        BackTools
        Consumer
        [Other Endpoints]
    ViewComposers
        BackTools
        Consumer
        [Other Endpoints]
Jobs
    BackTools
    Consumer
    [Other Endpoints]
Libraries
Listeners
Providers

Routes

Route::group(array(
    'domain' => Config::get('settings.domains.back'),
    'namespace' => 'back\\controllers'
), function () {
    Route::get('login', array('as' => 'auth.back.login', 'uses' => 'AuthController@getBackLogin'));
    Route::get('logout', array('as' => 'auth.back.logout', 'uses' => 'AuthController@getBackLogout'));
    Route::post('login', array('as' => 'auth.back.login', 'uses' => 'AuthController@postBackLogin'));
});

Composer

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "companyX\\": "app/",
        "back\\composers\\": "app/Http/ViewComposers/BackTools",
        "back\\controllers\\": "app/Http/Controllers/BackTools",
        "back\\jobs\\": "app/Jobs/BackTools",
        "back\\middleware\\": "app/Http/Middleware/BackTools",
        "consumer\\composers\\": "app/Http/ViewComposers/Consumer",
        "consumer\\controllers\\": "app/Http/Controllers/Consumer",
        "consumer\\jobs\\": "app/Jobs/Consumer",
        "consumer\\middleware\\": "app/Http/Middleware/Consumer",          
        "companyX\\commands\\": "app/Console/Commands",
        "companyX\\composers\\": "app/ViewComposers",
        "companyX\\controllers\\": "app/Http/Controllers",
        "companyX\\jobs\\": "app/Jobs",
        "companyX\\libraries\\": "app/Libraries",
        "companyX\\providers\\": "app/Providers"
    }
},

Autoload Classmap After Optimize

'back\\controllers\\AuthController' => $baseDir . '/app/Http/Controllers/BackTools/AuthController.php',
'back\\controllers\\BackToolsController' => $baseDir . '/app/Http/Controllers/BackTools/BackToolsController.php',

Example Error

ReflectionException in Container.php line 736: 
Class swaggable\Http\Controllers\back\controllers\BackToolsController does not exist
  • Open RouteServiceProvider under Providers
  • Change $namespace to ''

If someone else can give me an explanation, I'll mark his answer as best. The concept of having a default namespace may appeal to someone but for me it shreds the most beautiful aspect of psr-4 namespacing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM