路由未命中控制器

时间:2019-02-02 17:07:22

标签: laravel controller routing laravel-routing

当我尝试为产品创建视图时,URL会正确构建。

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

但是,我没有找到404页面,也不知道为什么。就像控制器没有被调用。

启动

<a href="{{ route('product.view', $product->slug) }}">

路线

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

控制器

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

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

查看

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

编辑

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');

1 个答案:

答案 0 :(得分:2)

尝试更改

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

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

参考:Laravel Routing