David
David

Reputation: 965

Route Not Hitting Controller

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 href="{{ route('product.view', $product->slug) }}">

Route

Route::get('/product/{$slug}', 'ProductsController@view')->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('/', 'ProductsController@index')->name('product.index');
Route::get('/products/create', 'ProductsController@create')->name('product.create');
Route::post('/products', 'ProductsController@store')->name('product.store');
Route::get('/product/{$slug}', 'ProductsController@view')->name('product.view');
/*Route::get('/users', 'UsersController');*/

Route::get('/contact', 'PagesController@contact');
Route::get('/about', 'PagesController@about');

Upvotes: 1

Views: 333

Answers (1)

Iftikhar uddin
Iftikhar uddin

Reputation: 3182

Try changing

Route::get('/product/{$slug}', 'ProductsController@view')->name('product.view');

to

Route::get('/product/{slug}', 'ProductsController@view')->name('product.view');

Ref: Laravel Routing

Upvotes: 3

Related Questions