Laravel:Action App\Http\Controllers\NotesController@store 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46931166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:52:00  来源:igfitidea点击:

Laravel: Action App\Http\Controllers\NotesController@store not defined

phplaravellaravel-bladelaravel-5.5

提问by Jethro Hazelhurst

I have a form

我有一个表格

<form action="{{ action('NotesController@store') }}" method="post">
    //
</form>

The action points to the storemethod in the NotesController controller in the App\Http\Controllersdirectory.

该操作指向目录中storeNotesController 控制器中的方法App\Http\Controllers

The NotesControllerlooks like this:

NotesController如下所示:

<?php

namespace App\Http\Controllers;

use App\Note;
use Illuminate\Http\Request;

class NotesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // var_dump($request);
        //
        // $note = new Note();


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

But when I try to render the page that displays my form (so before even submitting it, just trying to render the form itself) I get the following error:

但是当我尝试呈现显示我的表单的页面时(所以在提交之前,只是尝试呈现表单本身)我收到以下错误:

Action App\Http\Controllers\NotesController@store not defined. (View: C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\diary\entry.blade.php)

未定义 Action App\Http\Controllers\NotesController@store。(查看:C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\diary\entry.blade.php)

回答by David D

you need to define web routes for the controller in routes/web.php

您需要为控制器定义网络路由 routes/web.php

Route::resource('notes', 'NotesController');

回答by Hiren Gohel

You can use the action()helper to generate an URL to your route:

您可以使用action()帮助程序生成路由的 URL:

<form method="post" action="{{ action('NotesController@store') }}" accept-charset="UTF-8">

You can also use like:

你也可以使用像:

In Route:

在路线:

Route::post('notes', 
      array('uses'=>'App\Controllers\NotesController@store',
             'as' => 'notes.create'));

In View:

在视图中:

{{ Form::open(array('url'=>route('notes.create'))) }}

{{ Form::open(array('url'=>route('notes.create'))) }}

And if you are using Laravel resourceController, then use:

如果您使用的是 Laravel 资源控制器,请使用:

Route::resource('notes', 'NotesController');

Hope this helps you!

希望这对你有帮助!