Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

automica's avatar

registering namespace for custom controller in laravel 5.8

I'm developing a package that includes a custom route and controller.

I am having issues getting the application to recognise the custom controller even though it is loading the route correctly.

(1) I have added route and controller in the following locations:

- mypackage/src/routes/web.php
- mypackage/src/Http/Controllers/TranslationController.php

(2) controller has the following namespace

 namespace MyPackage\LaravelI18nLoader\Http\Controllers;

(3) within the boot method of my package's ServiceProvider, I have the following:

 protected function registerRoutes()
    {
        Route::namespace('MyPackage\LaravelI18nLoader\Http\Controllers')
            ->middleware('web')
            ->group(function () {
            $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
        });
    }

My test:

    /** @test */
    public function getConfigShouldReturnJsonResponse()
    {
        $route = route('translation.config');

        $this
            ->withExceptionHandling()
            ->get($route)
            ->dump()
            ->assertSuccessful()
            ->assertJson(['default_locale' => 'en']);
    }

if i dump $route, i can see the correct path is defined.

Route::get('translation/config', ['TranslationController', 'get'])->name('translation.config');

Running php artisan route:list i get the following:

 local.ERROR: Class TranslationController does not exist {"exception":"[object] (ReflectionException(code: -1): Class TranslationController does not exist at /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:788)

I know in laravel 6 + its possible to include the controller within the route by adding it via a use statement, but in laravel5.8 i seem just to have to specify controller name in the route file.

Any advise for how i can register this route correctly on this namespace?

0 likes
2 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61

What do you get when you define it like this?

Route::get('translation/config', ['MyPackage\LaravelI18nLoader\Http\Controllers\TranslationController', 'get'])->name('translation.config');

Please or to participate in this conversation.