在此之前,我只是有卖家可以创造新的产品,它的功能很好,没有错误。
所以现在,我增加了卖家可以创建新的类别,它的功能,但对于卖方创造新产品,而不是功能。
它显示路由product.store未定义。我怎么才能修好它?
这是在web.php里
Route::get('/index', 'ProductController@index');
Route::get('/create', 'ProductController@create');
Route::post('','ProductController@store')->name('product.store');
Route::get('/category/index', 'CategoryController@index');
Route::get('/category/create', 'CategoryController@create');
Route::post('','CategoryController@store')->name('category.store');
当我在//post::post(‘)上评论时,'CategoryController@store')->name('category.store');seller可以创建它的新产品,但卖方不能创建新的类别
这是product/create.blde.php
<div class="container">
<div class="row">
@include('admin.includes.sidebar_admin')
<div class="col-md-9">
<div class="panel panel-primary">
<div class="panel-heading">Create products</div>
<div class="panel-body">
<form action="{{route('product.store')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
@csrf
这是类别/create.blde.php
<div class="container">
<div class="row">
@include('admin.includes.sidebar_admin')
<div class="col-md-9">
<div class="panel panel-primary">
<div class="panel-heading">Create New Category</div>
<div class="panel-body">
<form action="{{route('category.store')}}" method="post">
{{csrf_field()}}
@csrf
发布于 2019-10-23 15:43:11
路由的命名是可以的,但是您必须提供一个端点,因为这是实际使用的。您不能投递到同一个端点并期望得到两个不同的结果。
所以这个:
Route::post('','ProductController@store')->name('product.store');
取而代之的可能是:
Route::post('/products','ProductController@store')->name('product.store');
这是:
Route::post('','CategoryController@store')->name('category.store');
可以是:
Route::post('/categories','CategoryController@store')->name('category.store');
发布于 2019-10-23 15:44:16
在您的web.php
中,有两个重叠的路由。
Route::get('/index', 'ProductController@index');
Route::get('/create', 'ProductController@create');
Route::post('','ProductController@store')->name('product.store'); //<----first one
Route::get('/category/index', 'CategoryController@index');
Route::get('/category/create', 'CategoryController@create');
Route::post('','CategoryController@store')->name('category.store'); //<----second one
将第二条路径更改为其他方式,如:
Route::post('/category','CategoryController@store')->name('category.store');
发布于 2019-10-23 16:13:53
问题在于如何在web.php
文件中声明路由名称。您正在用类别存储路由覆盖您的产品存储路由。
Route::post('','ProductController@store')->name('product.store');
Route::post('','CategoryController@store')->name('category.store');
有一段时间,您可能会认为他们的路线名称是不同的,所以他们不工作。不工作背后的一点是,路线名称提供了一些特殊的优势。假设您希望将路线更改为“产品/创建”之类的内容。然后你必须在你的项目中全部改变它们。如果他们使用了5次,你必须更换5次,如果50次或更多次,我希望你能猜出有多痛苦。为了从中解脱,路线的名字在这里。在这里,您可以给出一个路由的名称,并调用/引用他们的名字。因此,在将来,如果您将路径更改到其他东西,则无需费心更改引用,因为名称仍将是相同的。但是请注意,路由名称或路由不能引用或指向相同的http谓词。在这里,您违反了规则,尽管您的路由名称不同,但是您使用相同的http谓词(POST)指向相同的路由。这就是为什么它用最后一个Route::post('','ProductController@store')->name('product.store');
覆盖前一个Route::post('','CategoryController@store')->name('category.store');
的原因。因此,对于product.store这个路线名称,Laravel是不知道的。
其他一些小更正:默认情况下,索引文件/方法会被触发。所以你可能要换掉它们
//Route::get('/', 'ProductController@index')
Route::get('/index', 'ProductController@index');
//Route::get('/category', 'CategoryController@index')
Route::get('/category/index', 'CategoryController@index');
https://stackoverflow.com/questions/58526528
复制相似问题