【问题标题】:Route [product.store] not defined in laravel路线 [product.store] 未在 laravel 中定义
【发布时间】:2019-10-23 15:39:42
【问题描述】:

在此之前,我只是让卖家可以创建新产品并且它运行良好没有错误

所以现在,我添加卖家可以创建新类别它起作用但对于卖家创建新产品不起作用

它显示 Route [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');

当我在 //Route::post('','CategoryController@store')->name('category.store'); 卖家可以创建新产品时,它会起作用,但卖家不能创建新类别

这是产品/create.blade.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.blade.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

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    在您的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');
    

    【讨论】:

      【解决方案2】:

      路由的命名是可以的,但是你必须提供一个端点,因为那是实际使用的。您不能发布到同一个端点并期望两个不同的结果..

      所以这个:

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

      【讨论】:

        【解决方案3】:

        问题在于您在web.php 文件中声明路由名称的方式。您正在使用类别存储路径覆盖您的产品存储路径。

        Route::post('','ProductController@store')->name('product.store');  
        Route::post('','CategoryController@store')->name('category.store'); 
        

        您可能会认为他们的路线名称不同,所以为什么他们不工作。不工作的原因是路线名称提供了一些特殊的优势。假设您想将路线更改为“产品/创建”之类的内容。然后,您必须在整个项目中更改它们。如果用了5次,就得换5次,如果50次以上,我希望你能猜到那会是多么痛苦。为了缓解这种情况,路线名称在这里。您可以在哪里给路由命名,并用他们的名字打电话/推荐他们。因此,将来如果您将路线更改为其他内容,则无需费心更改引用,因为名称仍将相同。但请注意,路由名称路由不能指代或指向相同的http动词强>。在这里,您违反了规则,尽管您的路线名称不同,但您使用相同的 http 动词(POST)指向相同的路线。这就是为什么它用最后一个Route::post('','CategoryController@store')-&gt;name('category.store'); 覆盖前一个Route::post('','ProductController@store')-&gt;name('product.store');。所以对于 Laravel 未知的路径名称 product.store。

        其他一些小的更正: 默认情况下,索引文件/方法会被触发。所以你可能想替换它们

        //Route::get('/', 'ProductController@index')
        Route::get('/index', 'ProductController@index');  
        //Route::get('/category', 'CategoryController@index')
        Route::get('/category/index', 'CategoryController@index');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-15
          • 2023-03-29
          • 1970-01-01
          • 1970-01-01
          • 2016-01-24
          • 2020-05-03
          • 2016-04-02
          • 2014-09-20
          相关资源
          最近更新 更多