Search code examples
phplaravellaravel-9

Target class LogoutController does not exist yet it does


I'm using Laravel 9

When I click on a link with the route /logout I get the following error: Target class [App\Http\Controllers\LogoutController] does not exist. Yet it does exist and the file is there. The file path to LogoutController.php is app/Http/Controllers/Auth/LogoutController.php

If I do a refresh I get a different error: The GET method is not supported for this route. Supported methods: POST. But the method is POST not GET. Here is my web.php line for the route and the class is imported using use App\Http\Controllers\Auth\LogoutController; :

Route::post('/logout', [LogoutController::class, 'logout_user'])->name('logout');

Here is my LogoutController in the Auth directory

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LogoutController extends Controller
{
    public function logout_user()
    {
        auth()->logout();
    }
}

Here is the link for the route:

        <li>
          <form action="{{route('logout')}}" method="POST">
          @csrf
          <button>Logout</button>
          </form>
        </li>

All the other routes that are the same work why is this one not working? Help to fix is appreciated.


Solution

  • Your namespace is not correct:

    namespace App\Http\Controllers;
    

    needs to be

    namespace App\Http\Controllers\Auth;
    

    Make sure it is located in the correct folder and the use statements are correct.

    Also add the use statement for the extended controller

    use App\Http\Controllers\Controller