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

Spiral's avatar

Getting same page on every URL/Route in Laravel8

Hi! I'm using default laravel auth , I have multiple routes with prefix group but when I use other routes/urls but giving the same page on every route which is View page the product

you can see my route

Auth::routes();

Route::prefix('products')->group(function () {
    Route::get('/', [ProductsController::class, 'index']);
    Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
});

On this below route giving the product_view page which I can't understand why is giving

Route::get('/test_route', , function () {
    return 'Hello World';
});
0 likes
20 replies
automica's avatar

reverse the order of your routes

Route::prefix('products')->group(function () {
    Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
    Route::get('/', [ProductsController::class, 'index']);
});
1 like
Spiral's avatar

@automica Thanks dear for replying to me.

I have reversed the order of the routes but still the same issue

automica's avatar

@Spiral what url are you using to access your view_product method?

if you are using route model binding in controller then you can do:

 Route::get('/{product:code}', [ProductsController::class,'viewProduct'])->name('products.view_product');

and then you can do:

public function viewProduct(Product $product)
{
    return view('your.view', compact('product'))
}
1 like
Spiral's avatar

@automica this one which I'm using for view product

Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');

and this is the list of my routes

+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| Domain | Method   | URI                      | Name                        | Action
    | Middleware                               |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
|        | GET|HEAD | api/user                 | generated::suiIeX6oHZWSnSP1 | Closure
    | api                                      |
|        |          |                          |                             |
    | App\Http\Middleware\Authenticate:sanctum |
|        | GET|HEAD | sanctum/csrf-cookie      | generated::WKOquUKjM8nfHvKl | Laravel\Sanctum\Http\Controllers\CsrfCookieController@s
how | web                                      |
|        | GET|HEAD | products              | generated::IucNuoXqqYtZPi70 | App\Http\Controllers\ProductsController@index
    | web                                      |                                     |
|        | GET|HEAD | {code}                   | products.view_product         | App\Http\Controllers\ProductsController@view_product
    | web                                      |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
automica's avatar

@Spiral if you are using:

Route::prefix('products')->group(function () {
    Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
    Route::get('/', [ProductsController::class, 'index']);
});

which uses a route prefix, then your route would be

/products/product-name

if you dont want the prefix you need to do

Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
Route::prefix('products')->group(function () {
    Route::get('/', [ProductsController::class, 'index']);
});
1 like
Spiral's avatar

@automica

Auth::routes();
Route::prefix('products')->group(function () {
    Route::get('{code}', [ProductsController::class,'view_product'])->name('products.view_product');
    Route::get('/', [ProductsController::class, 'index']);
    Route::post('/upload_product', [ProductsController::class,'upload_product'])->name('products.upload_product');
    Route::get('/fetch_product', [ProductsController::class,'fetch_product'])->name('products.fetch_product');    
    Route::get('/delete_product', [ProductsController::class,'delete_product'])->name('products.delete_product');
});
MichalOravec's avatar

@Spiral That route {code} has to be at the end of your group with prefix products. Otherwise route with /fetch_product will never run.

For delete you are using get request?!

1 like
MichalOravec's avatar
Level 75

Previously you had this route outside of products prefix and your routes are cached.

Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');

So clear your route cache

php artisan route:clear
1 like
Spiral's avatar

@MichalOravec Dear giving same issue These are my routes which I have updated. and run php artisan route:clear but still same issue giving other route

Route::prefix('products')->group(function () {    
    Route::get('/', [ProductsController::class, 'index']);
    Route::post('/upload_image', [ProductsController::class,'upload_product'])->name('proudct.upload_product');
    Route::get('/fetch_product', [ProductsController::class,'fetch_product'])->name('proudct.fetch_product');    
    Route::get('/delete_product', [ProductsController::class,'delete_product'])->name('proudct.delete_product');
});

Route::get('{code}', [ProductsController::class,'view_product'])->name('proudct.view_product');


Auth::routes();

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

MichalOravec's avatar

@Spiral Man this route has to be at the end of web.php

// other routes

Route::get('{code}', [ProductsController::class,'view_product'])->name('proudct.view_product');

or if you need a prefix there then

Route::prefix('products')->group(function () {    
    // other routes

    Route::get('{code}', [ProductsController::class,'view_product'])->name('product.view_product');
});

Auth::routes();

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

@MichalOravec Dear now is working when i have auth routes on the top

Auth::routes();

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


Route::prefix('screenshots')->group(function () {    
    Route::get('/', [ImageUploadController::class, 'index']);
    Route::post('/upload_image', [ImageUploadController::class,'upload_image'])->name('dropzone.upload_image');
    Route::get('/fetch_image', [ImageUploadController::class,'fetch_image'])->name('dropzone.fetch_image');    
    Route::delete('/delete_image', [ImageUploadController::class,'delete_image'])->name('dropzone.delete_image');
});


Route::get('{code}', [ImageUploadController::class,'view_image'])->name('dropzone.view_image');
automica's avatar

also, don't PLEASE dont use a GET for a delete:

    Route::get('/delete_product', [ProductsController::class,'delete_product'])->name('products.delete_product');

CRUD route should be:

    Route::delete('/delete_product/{productId}', [ProductsController::class,'delete_product'])->name('products.delete_product');
1 like
Spiral's avatar

@automica Dear I'm deleting the products with AJAX. But you are right I will change that

automica's avatar

BTW if you follow REST guidelines you can do:

  • GET /products - get all products
  • GET /products/{productId} - get a specific product
  • POST /products - create new product
  • PATCH /products/{productId} - update existing product
  • DELETE /products/{productId} - delete a product

checkout https://jsonapi.org

1 like
Spiral's avatar

@MichalOravec Dear First of all when I was run php artisan route:clear then working fine. but after that when adding default auth then coming to the same problem. After that, I have paste Auth::routes(); on the top then giving no issue. I can't understand what is problem. Please can you explain to me why is that? So that I could understand. Thanks

Please or to participate in this conversation.