Asked  7 Months ago    Answers:  5   Viewed   212 times

Here is my controller:

<?php

namespace AppHttpControllersApi;

use AppHttpControllersController;
use IlluminateHttpRequest;

class RegisterController extends Controller
{
    public function register(Request $request)
    {
        dd('aa');
    }
}

As seen in the screenshot, the class exists and is in the correct place:

enter image description here

My api.php route:

Route::get('register', 'ApiRegisterController@register');

When I hit my register route using Postman, it gave me the following error:

Target class [ApiRegisterController] does not exist.


Update:

Thanks to the answer, I was able to fix it. I decided to use the fully qualified class name for this route, but there are other options as described in the answer.

Route::get('register', 'AppHttpControllersApiRegisterController@register');

 Answers

75

You are using Laravel 8. In a fresh install of Laravel 8, there is no namespace prefix being applied to your route groups that your routes are loaded into.

"In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property's value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel." Laravel 8.x Docs - Release Notes

You would have to use the Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing.

use AppHttpControllersUserController;

Route::get('/users', [UserController::class, 'index']);
// or
Route::get('/users', 'AppHttpControllersUserController@index');

If you prefer the old way:

AppProvidersRouteServiceProvider:

public function boot()
{
    ...

    Route::prefix('api')
        ->middleware('api')
        ->namespace('AppHttpControllers') // <---------
        ->group(base_path('routes/api.php'));

    ...
}

Do this for any route groups you want a declared namespace for.

The $namespace property:

Though there is a mention of a $namespace property to be set on your RouteServiceProvider in the Release notes and commented in your RouteServiceProvider this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it by itself won't add these namespace prefixes, you would still have to make sure you would be using this variable when adding the namespace to the route groups.

This information is now in the Upgrade Guide

Laravel 8.x Docs - Upgrade Guide - Routing

With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace variable by itself only helps in generating URLs to actions.

Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace directly in the example.

Update:

If you have installed a fresh copy of Laravel 8 since version 8.0.2 of laravel/laravel you can uncomment the protected $namespace member variable in the RouteServiceProvider to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.

// protected $namespace = 'App\Http\Controllers';

The only reason uncommenting that would add the namespace prefix to the Controllers assigned to the routes is because the route groups are setup to use this variable as the namespace:

...
->namespace($this->namespace)
...
Tuesday, June 1, 2021
 
Fishingfon
answered 7 Months ago
67

For anybody else who has had this issue.

I had prefer stable set in the composer file, which installed PHPUnit 6.

This was "made stable today" - thus it installed during a composer update.

Downgrading to PHPUnit 5 fixes the issue - so was bad timing starting it today.

Wednesday, March 31, 2021
 
dimitarvp
answered 9 Months ago
65

You need to put SongsTableSeeder into file SongsTableSeeder.php in the same directory where you have your DatabaseSeeder.php file.

And you need to run in your console:

composer dump-autoload

to generate new class map and then run:

php artisan db:seed

I've just tested it. It is working without a problem in Laravel 5

Sunday, June 27, 2021
 
mattltm
answered 6 Months ago
47

In laravel 8 the default route namespace was removed.

Try to change:

ArticlesTableSeeder.php:

 factory(AppModelsArticle::class, 30)->create();

to:

AppModelsArticle::factory()->count(30)->create(); 

ArticleFactory.php:

protected $model = AppModelsArticle::class;

to:

protected $model = AppModelsArticle::class;

and you will probably have to change:

 'title' => $faker->text(50),
            'body' => $faker->text(200)

to:

 'title' => $this->faker->text(50),
        'body' => $this->faker->text(200)
Monday, August 2, 2021
 
user729076
answered 5 Months ago
60

The issue is well documented in Github. See comments above that explains it further.

I've modified one of the 'solutions' in Github to automatically reset all model events during the tests. Add the following to your TestCase.php file.

app/tests/TestCase.php

public function setUp()
{
    parent::setUp();
    $this->resetEvents();
}


private function resetEvents()
{
    // Get all models in the Model directory
    $pathToModels = '/app/models';   // <- Change this to your model directory
    $files = File::files($pathToModels);

    // Remove the directory name and the .php from the filename
    $files = str_replace($pathToModels.'/', '', $files);
    $files = str_replace('.php', '', $files);

    // Remove "BaseModel" as we dont want to boot that moodel
    if(($key = array_search('BaseModel', $files)) !== false) {
        unset($files[$key]);
    }

    // Reset each model event listeners.
    foreach ($files as $model) {

        // Flush any existing listeners.
        call_user_func(array($model, 'flushEventListeners'));

        // Reregister them.
        call_user_func(array($model, 'boot'));
    }
}
Thursday, November 4, 2021
 
Camille Laborde
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share