当我在 Laravel 中启动该项目时,我有一个 ReturnController。但由于命名约定,我不得不将其重命名为 OrderController。我重命名了控制器和模型,我运行所有 php artisan cache:clear、php artisan route:cache、php artisan config:cache ...
等,但是当我尝试在 中创建订单时>views\orders\add.blade.php
(表单的操作是action="{{route('orders.store') }}"
)
我收到错误目标类 [OrderController] 不存在。
这是web.php
内容:
Route::get('/', function () { return view('welcome'); }); Route::resource('customers', 'CustomerController'); Route::resource('orders', 'OrderController'); Auth::routes(); Route::get('/home', [App\Http\Controllers\OrderController::class, 'showOrders'])->name('orders'); Route::get('/orders/create', [App\Http\Controllers\OrderController::class, 'create'])->name('orders.create'); Route::get('/orders', [App\Http\Controllers\OrderController::class, 'index']);
我错过了什么?在使用正确的控制器名称从头开始重写整个应用程序之前,如何解决此问题?
另一个线索是,为了导航到 http://127.0.0.1:8000/home
,奇怪的是我必须添加行 use App\Models\Order
在 OrderController.php 中以便工作...所以这里是 OrderController.php 以帮助...
<?php namespace App\Http\Controllers; use App\Models\Order; use App\Models\Customer; use App\Models\Product; use Illuminate\Http\Request; class OrderController extends Controller { /** * Display a listing of the resource. */ public function index() { // } /** * Show the form for creating a new resource. */ public function create() { $customers = Customer::all(); $products = Product::all(); return view('orders.add', compact('customers', 'products')); } /** * Store a newly created resource in storage. */ public function store(Request $request) { // } /** * Display the specified resource. */ public function show(string $id) { // } /** * Show the form for editing the specified resource. */ public function edit(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } public function showOrders() { $orders = Order::with(['customer', 'products'])->get(); return view('home', compact('orders')); } }
P粉5459565972024-01-17 14:58:06
在 web.php 中,而不是:
Route::resource('customers', 'CustomerController'); Route::resource('orders', 'OrderController');
用途:
Route::resource('customers', App\Http\Controllers\CustomerController::class); Route::resource('orders', App\Http\Controllers\OrderController::class);