controller @ store未定义laravel中的错误

时间:2017-10-30 09:01:31

标签: php laravel

我安装了laravelcollective并尝试运行以下代码

{!! Form::open(['action' => 'ProductController@store','method' => 'POST']) !!}
    <div class="form-group">
        {{Form::label('title','Title')}}
        {{Form::text('title',['class' => 'form-control', 'placeholder' => 'Title'])}}
    </div>
{!! Form::close() !!}

我收到此错误

App\Http\Controllers\ProductController@store not defined. (View: /Applications/MAMP/htdocs/lsapp/resources/views/product/create.blade.php)

当我运行php artisan route:list时,我看到:

    |        | GET|HEAD  | api/user               |                  | Closure              | api,auth:api |
|        | POST      | events                 | events.store     | App\Http\Controllers\eventcontroller@store              | web          |
|        | GET|HEAD  | events                 | events.index     | App\Http\Controllers\eventcontroller@index              | web          |
|        | GET|HEAD  | events/create          | events.create    | App\Http\Controllers\eventcontroller@create              | web          |
|        | PUT|PATCH | events/{event}         | events.update    | App\Http\Controllers\eventcontroller@update              | web          |
|        | GET|HEAD  | events/{event}         | events.show      | App\Http\Controllers\eventcontroller@show              | web          |
|        | DELETE    | events/{event}         | events.destroy   | App\Http\Controllers\eventcontroller@destroy              | web          |
|        | GET|HEAD  | events/{event}/edit    | events.edit      | App\Http\Controllers\eventcontroller@edit              | web          |
|        | GET|HEAD  | home                   | home             | App\Http\Controllers\HomeController@index              | web,auth     |
|        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login              | web,guest    |
|        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm              | web,guest    |
|        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout              | web          |
|        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | POST      | password/reset         |                  | App\Http\Controllers\Auth\ResetPasswordController@reset              | web,guest    |
|        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register              | web,guest    |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistra

3 个答案:

答案 0 :(得分:2)

为了获得最佳实践并保持代码我希望遵循我的建议:

1)转到routes / web.php并为您的ProductController@store方法添加名称,如下所示:

Route::post('','ProductController@store')->name('products.store');

2)转到create.blade.php文件并将其更改为:

{!! Form::open(array( 'route'=>'products.store')) !!} 
    <div class="form-group">    
    {{Form::label('title','Title')}}  
    {{Form::text('title',['class' => 'form-control', 'placeholder' => 'Title'])}} 
    </div>

答案 1 :(得分:0)

当您使用Laravel Collective表单时,不要将该方法声明为第二个参数,仅当您声明了一个url时,才将其用作路由参数,通常用于添加ID网址中需要一个。首先,尝试删除'method' => 'POST'部分,看看是否有效。

答案 2 :(得分:0)

您是否在routes / web.php文件中创建了资源控制器或单个路由?

你有几个选择:

对于您的Form :: open,您可以使用 Action,Route或URL

行动:

   {!! Form::open(['action' => 'ProductController@store']) !!}
    // By default, a POST method will be assumed 
        <div class="form-group">
            {{Form::label('title','Title')}}
            {{Form::text('title',['class' => 'form-control', 'placeholder' => 'Title'])}}
        </div>
    // If you use the Form::open method with POST, PUT or DELETE the CSRF token will be added to your forms as a hidden field automatically.
    {!! Form::close() !!}

<强>路线:

首先在routes / web.php文件中命名您的路线:

Route::post('/productURL', 'ProductController@store')->name('productRouteName');

然后打开表单:

{!! Form::open(['route' => 'productRouteName'] !!}

<强> URL:

只需从上面创建的路线调用网址即可。

{!! Form::open(['url' => 'productURL'] !!}