Ad

Route Not Hitting Controller

- 1 answer

When I try to create a view for a product, the URL gets built correctly.

http://localhost:8000/product/my-slug

However, I get a 404 page not found and I have no idea why. It's like the controller is not getting called.

Initiation

<a target="_blank" rel="nofollow noreferrer" href="{{ route('product.view', $product->slug) }}">

Route

Route::get('/product/{$slug}', '[email protected]')->name('product.view');

Controller

public function view($slug)
{
    $product = Product::find($slug);

    return view('products.view', compact('product'));
}

View

<h1>{{ $product->name }}</h1>

EDIT

web.php

Route::get('/', '[email protected]')->name('product.index');
Route::get('/products/create', '[email protected]')->name('product.create');
Route::post('/products', '[email protected]')->name('product.store');
Route::get('/product/{$slug}', '[email protected]')->name('product.view');
/*Route::get('/users', 'UsersController');*/

Route::get('/contact', '[email protected]');
Route::get('/about', '[email protected]');
Ad

Answer

Try changing

Route::get('/product/{$slug}', '[email protected]')->name('product.view');

to

Route::get('/product/{slug}', '[email protected]')->name('product.view');

Ref: Laravel Routing

Ad
source: stackoverflow.com
Ad