Search code examples
phplaravellaravel-9

error 404 in laravel 9.2 even though the route and the view exists


I'm trying to create a new view for my project so I've created the corresponding route, function and view but even with that the browser throws a 404 error.

Here is my web.php file:

<?php

use Illuminate\Database\PostgresConnection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CartController;
use App\Http\Controllers\PageController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// INDEX 
Route::get('/', function () {
    return view('welcome');
})->name('/');

// AUTH ROUTES
Auth::routes();

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

// CART ROUTES
Route::get('/shop', [CartController::class, 'shop'])->name('shop');
Route::get('/cart', [CartController::class, 'cart'])->name('cart.index');
Route::post('/add', [CartController::class, 'add'])->name('cart.store');
Route::post('/update', [CartController::class, 'update'])->name('cart.update');
Route::post('/remove', [CartController::class, 'remove'])->name('cart.remove');
Route::post('/clear', [CartController::class, 'clear'])->name('cart.clear');

// PROFILE
Route::get('/{username}', [PageController::class, 'profile'])->name('profile');

//PRODUCTS
Route::get('/product/{id}', [PageController::class, 'product'])->name('product');
Route::get('/product/create', [PageController::class, 'create'])->name('product.create');

My PageController.php file:

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PageController extends Controller
{
    public function profile() {
        if ( isset(Auth::user()->name )) {
            $user = Auth::user();
            return view('profile', compact('user'));
        } else {
            return redirect()->route('login');
        }
    }

    public function product($id) {
        $product = Product::findOrFail($id);
        return view('product.product', compact('product'));
    }

    public function create() {
        return view('product.create');
    }
}

And finally, my create.blade.php file:

@extends('template')

@section('title')
    Create product
@endsection

@section('favicon')
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
@endsection

@section('css')
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/dropdown-cart.css">
@endsection

@section('content')
    <h1>Create product</h1>
@endsection

note that the create.blade.php file is in a separate folder called product.

I've checked for othergraphic errors and maybe I'm blind but I don't see anything. I was hoping the page would work and my view would render but nothing.


Solution

  • You need to put this parameterized route end of the file

    // PROFILE
    Route::get('/{username}', [PageController::class, 'profile'])->name('profile');
    
    Route::get('/product/{id}', [PageController::class, 'product'])->name('product');
    

    because all the other routes below this will match with the variable parameter. you can read more here

    Solution:

    //PRODUCTS
    Route::get('/product/create', [PageController::class, 'create'])->name('product.create');
    Route::get('/product/{id}', [PageController::class, 'product'])->name('product');
    
    // PROFILE
    Route::get('/{username}', [PageController::class, 'profile'])->name('profile');