FabricioG
FabricioG

Reputation: 3310

Laravel new package not finding correct controller path

Using Laravel 5.6.39 I've installed a package called Mercurius Messenger

This package installs all of it's assets through composer so I can view them in /vendor/launcher/

In their repo they have a routes file.

Route::group([
    'as'         => 'mercurius.',
    'namespace'  => '\Launcher\Mercurius\Http\Controllers',
    'middleware' => [
        // 'Mercurius',
        'web',
        'auth',
    ],
], function () {
    // Mercurius home
    Route::get('/messages', ['as' => 'home', 'uses' => 'MessagesController@index']);
    // User Profile
    Route::get('/profile/refresh', 'ProfileController@refresh');
    Route::get('/profile/notifications', 'ProfileController@notifications');
});

The name space for the controllers is added above:

\Launcher\Mercurius\Http\Controllers

When I try to hit one of these routes I get this error:

"Class App\Http\Controllers\Launcher\Mercurius\Http\Controllers\MessagesController does not exist"

It's obviously adding the namespace to the current name space for my App\Http\Controllers, is there anyway to work around this? Or do I have to copy all the relative files into my project and sort out where they should go?

Upvotes: 0

Views: 52

Answers (1)

matticustard
matticustard

Reputation: 5149

You definitely don't need (or want) to copy the vendor files into your project.

The simplest option is probably to remove the namespace attribute from your route group and use the full namespace when defining the route.

Route::get('/messages', ['as' => 'home', 'uses' => '\Launcher\Mercurius\Http\Controllers\MessagesController@index']);
Route::get('/profile/refresh', '\Launcher\Mercurius\Http\Controllers\ProfileController@refresh');
Route::get('/profile/notifications', '\Launcher\Mercurius\Http\Controllers\ProfileController@notifications');

Alternatively, you could create a new routes file (e.g., mercurius.php) and map it in your RouteServiceProvider.php with the correct namespace.

public function map()
{
    // ... existing route groups

    $this->mapMercuriusRoutes();
}

protected function mapMercuriusRoutes()
{
    Route::middleware(['web','auth','Mercurius'])
         ->namespace('\Launcher\Mercurius\Http\Controllers')
         ->group(base_path('routes/mercurius.php'));
}

Upvotes: 1

Related Questions