Laravel 8中未定义动作Controller @ store的问题 [英] Problem with action Controller@store not defined in Laravel 8

查看:46
本文介绍了Laravel 8中未定义动作Controller @ store的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在探索Laravel 8,但是我的控制器的store()方法未定义"存在问题.

I'm still exploring Laravel 8 but I have a problem with my controller's store() method 'not defined.'

InvalidArgumentException操作PostsController @ store未定义.(视图:D:\ Server \ htdocs \ app \ resources \ views \ posts \ create.blade.php)

InvalidArgumentException Action PostsController@store not defined. (View: D:\Server\htdocs\app\resources\views\posts\create.blade.php)

我相信我已经定义了它,并且使用了资源路由.

I believe I have actually defined it and I used a resources route.

路线

Route::resource('posts', PostsController::class); 

刀片

@extends('layouts.app')

@section('content')
    <h1>Create Post</h1>

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

    {{Form::submit('Submit',['class' =>'btn btn-primary'])}}
    {!! Form::close() !!}

@endsection

控制器

class PostsController extends Controller
{
    public function create()
    {
        return view('posts.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'body' => 'required'
        ]);

        return 143;
    }
}

推荐答案

进入您的 RouteServiceProvider 并将 $ namespace 属性设置为 App \ Http \ Controllers,如果您希望在为操作生成URL时添加名称空间前缀.

Go into your RouteServiceProvider and set the $namespace property to App\Http\Controllers if you want there to be a namespace prefix added when generating URLs for actions.

否则,您应该使用其完全合格的类名(FQCN)来引用您的控制器.

Otherwise you should be referring to your Controllers by their Fully Qualified Class Name (FQCN).

['action' => 'App\Http\Controllers\PostsController@store', ...]

这篇关于Laravel 8中未定义动作Controller @ store的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆