【问题标题】:Action App\Http\Controllers\KategoriController@store not definedAction App\Http\Controllers\KategoriController@store 未定义
【发布时间】:2021-07-08 02:13:58
【问题描述】:

我正在使用 Laravel 8,我想尝试使用 Modals 制作输入表单。 这是我的第一次尝试,我不知道我做错了什么,而我认为我已经复制了与教程相同的内容。

错误信息

Action App\Http\Controllers\KategoriController@store 未定义。 (查看:C:\xampp\htdocs\LARAVEL\Workshop\resources\views\back\kategori-produk\tampil.blade.php)

类别控制器

<?php

namespace App\Http\Controllers;

use App\Models\Kategori;
use Illuminate\Http\Request;

class KategoriController extends Controller
{
    // sesi FRONTEND
    public function index()
    {
        $kategoris = \App\Models\Kategori::all();
        return view('front.product',['kategoris' => $kategoris]);
    }

    // sesi BACKEND
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function indexBack()
    {
        $kategoris = \App\Models\Kategori::all();
        return view('back/kategori-produk/tampil',['kategoris' => $kategoris]);
    }

    /**
     * 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)
    {
        $this->validate($request, [
            'name' => 'required',
        ]);

        $kategoris = new Kategori;
        $kategoris->nama = $request->input('nama');
        $kategoris->save();
        return back()->with('Success', 'Data Saved');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Kategori  $kategori
     * @return \Illuminate\Http\Response
     */
    public function show(Kategori $kategori)
    {
        //
    }

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

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

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

类别视图刀片

另外,这里也编码“模态形式”。

@extends('back.index')

@section('content')
<main>
    <div class="container-fluid">
        <h1 class="mt-4">Tables</h1>
        <ol class="breadcrumb mb-4">
            <li class="breadcrumb-item"><a href="index.html">Dashboard</a></li>
            <li class="breadcrumb-item active">Tables</li>
        </ol>
        <div class="card mb-4">
            <div class="card-header">
                <!-- Button trigger modal FORM -->
                @auth
                <button type="button" class="btn btn-info float-right" data-toggle="modal" data-target="#exampleModal">
                    Add
                </button>
                @endauth


                <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            </div>
                            <form action={{ action('App\Http\Controllers\KategoriController@store') }} method="GET">
                                @csrf
                                <div class="modal-body">
                                    <div class="form-group">
                                        <input id="nama" type="text" class="form-control col-md-8 @error('nama') is-invalid @enderror" name="nama" value="{{ old('nama') ?? $kategori->nama ?? '' }}" autofocus>
                                        @error('nama')
                                            <div class="text-danger">{{ $message }}</div>
                                        @enderror
                                    </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                        <button type="button" class="btn btn-primary">Save</button>
                                    </div>
                                </div>
                            </form>
                    </div>
                </div>
            </div>


            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Nama</th>
                                <th>Act</th>
                            </tr>
                        </thead>
                        @forelse ($kategoris as $kategori)
                        <tbody>
                            <tr>
                                <td>{{$kategori->id}}</td>
                                <td>{{$kategori->nama}}</td>
                                @auth
                                <td>
                                    {{-- <a href="{{ route('dosens.edit',['dosen' => $dosen->id])}}"
                                        class="btn btn-secondary" title="Edit Dosen">Edit</a>
                                    <form action="{{route('dosens.destroy',['dosen' => $dosen->id])}}"
                                    method="POST" class="d-inline">
                                    @csrf  @method('DELETE')
                                    <button type="submit" class="btn btn-danger shadow-none btn-hapus"
                                    title="Hapus Dosen" data-name="{{$dosen->nama}}"
                                    data-table="dosen">Hapus</button>
                                    </form> --}}
                                </td>
                                @endauth
                            </tr>
                        </tbody>
                        @empty
                        <strong>Tidak Ada Data ..</strong>
                        @endforelse
                    </table>

                </div>
            </div>

        </div>
    </div>

</main>
@endsection

web.php 中的路由

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\KategoriController;
use App\Http\Controllers\ProdukController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('front.welcome');
});

Route::get('/product', [KategoriController::class,'index']);

Route::get('/product-details', [ProdukController::class,'index']);

Route::get('/service', function () {
    return view('front.service');
});
Route::get('/rent', function () {
    return view('front.rent');
});
Route::get('/training', function () {
    return view('front.training');
});
Route::get('/facility', function () {
    return view('front.facility');
});
Route::get('/aboutUs', function () {
    return view('front.aboutUs');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::prefix('/kategori')->group(function () {
    Route::get('/', [App\Http\Controllers\KategoriController::class, 'indexBack']);
});

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您已使用 GET 提交数据,GET 用于检索数据。您应该使用 POST 代替 GET。查看我的代码并在您的表单和路由中使用它。

    类别控制器

    <?php
    namespace App\Http\Controllers;
    
    use App\Models\Kategori;
    use Illuminate\Http\Request;
    class KategoriController extends Controller {
        // sesi BACKEND
        public function __construct() {
            $this->middleware('auth');
        }
    
        // sesi FRONTEND
        public function index() {
            $kategoris = Kategori::all();
            return view('front.product',['kategoris' => $kategoris]);
        }
    
        public function indexBack() {
            $kategoris = Kategori::all();
            return view('back/kategori-produk/tampil',['kategoris' => $kategoris]);
        }
        
        public function create() {
            //
        }
    
        public function store(Request $request) {
            $this->validate($request, [
                'name' => 'required',
            ]);
    
            $kategoris = new Kategori;
            $kategoris->nama = $request->input('nama');
            $kategoris->save();
            return back()->with('Success', 'Data Saved');
        }
    
        public function show($category) {
            //
        }
    
        public function edit($category) {
            //
        }
    
        public function update(Request $request, $category) {
            //
        }
    
        public function destroy($category) {
            //
        }
    }
    

    类别视图刀片

    <form method="post" action="{{ route('category.store') }}">
        @csrf
    
    </form>
    

    web.php

    Route::get('', 'KategoriController@index')->name('category.index');
    Route::get('', 'KategoriController@indexBack')->name('category.indexBack');
    Route::get('create', 'KategoriController@Create')->name('category.create');
    Route::post('create', 'KategoriController@Store')->name('category.store');
    Route::get('show/{category}', 'KategoriController@Show')->name('category.show');
    Route::get('update/{category}', 'KategoriController@Edit')->name('category.edit');
    Route::patch('update/{category}', 'KategoriController@Update')->name('category.update');
    Route::delete('delete/{category}', 'Users\UserController@Destroy')->name('user.destroy');
    

    【讨论】:

      【解决方案2】:

      首先,将此表单属性操作从 &lt;form action={{ action('App\Http\Controllers\KategoriController@store') }} method="GET"&gt; 更改为&lt;form action={{url('getKategorie') }} method="GET"&gt;,然后在路由文件中命名url。我确定这就是你的问题所在。就目前而言,laravel 正在 web.php 文件中查找该操作,但找不到它。因此添加类似Route::get('getKategorie',[KategoriController,'store']); 的内容。您需要注意的另一件事是,您正在使用资源控制器,因此GET http verb 只读取索引方法而不是存储方法。存储方法适用于POST http verb。希望这能回答您的问题。

      【讨论】:

        猜你喜欢
        • 2021-02-18
        • 2019-05-13
        • 2017-06-30
        • 2018-04-06
        • 2019-04-13
        • 2020-04-13
        • 2017-08-13
        • 2021-02-07
        • 1970-01-01
        相关资源
        最近更新 更多