New Laravel Install - Cant find Controllers

67 Views Asked by At

I have a new Laravel project running Valet + (laravel 8.12).

Controller:

app->Http->Controllers->PageController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class PageController extends Controller
{
    public function dashboard()
    {
        return view('show');
    }
}

Web.php

Route::get('/', function () {
    return view('home');
});

Route::get(
    '/dashboard',
    [PageController::class, 'show']
)->name('dashboard');

Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

I have a link on my homepage href="dashboard" however when this link is clicked I get an error:

lluminate\Contracts\Container\BindingResolutionException
Target class [PageController] does not exist.
http://myportal.test/dashboard

I have tried running: composer dump-autoload php artisan route:cache php artisan optimize php artisan clear-compiled

I dont fully understand why when I added php artisan ui vue --auth why the routing had [App\Http\Controllers\HomeController::class added for the route?

I have usually used: route::get('/dashboard', 'PageController@show'); which usually works.

None of these have worked to solve the issue. I have another Laravel project running in the same Valet+ environment that is working fine (although using a different version of Laravel).

I haven't been able to find a solution to my problem online that has helped me solve this problem. Can anyone point me in the right direction please?

3

There are 3 best solutions below

0
On

Change

Route::get(
    '/dashboard',
    [PageController::class, 'show']
)->name('dashboard');

to

Route::get(
    '/dashboard',
    [App\Http\Controllers\PageController::class, 'show']
)->name('dashboard');
0
On

You could simply include the class, add the following at the top of the web.php file

use App\Http\Controllers\PageController;
0
On

Include first your Controler in web.php useing use App\Http\Controllers\HomeController; then create route just like this

Route::get('/home', [HomeController::class, 'index'])->name('home');

full Code Try this one

use App\Http\Controllers\HomeController;

Route::get('/home', [HomeController::class, 'index'])->name('home');