【问题标题】:Laravel 8 with laravelcollective/html 6.2, Action Controller@function not defined带有 laravelcollective/html 6.2 的 Laravel 8,未定义动作控制器@函数
【发布时间】:2023-03-08 02:34:01
【问题描述】:

我是 Laravel 和 Stackoverflow 的新手

当 Laravel7 发布后,我开始学习 Laravel。 不久前推出了新的8.0版本,我开始尝试。

我无法确定问题是由较新版本的 Laravelor 任何错误配置引起的。

当我尝试以下(edit.blade.php)

{!! Form::open(['action' => ['ProductController@update', $product->id], 'method' => 'POST']) !!}

{!! Form::open(['action' => [[ProductController::class, 'update'], $product->id], 'method' => 'POST']) !!}

发生错误

Action ProductController@update 未定义

然后我尝试用类似的路径替换控制器名称

{!! Form::open(['action' => ['App\Http\Controllers\ProductController@update', $product->id], 'method' => 'POST']) !!}

{!! Form::open(['action' => [[App\Http\Controllers\ProductController::class, 'update'], $product->id], 'method' => 'POST']) !!}

有效!

所以我认为这是关于命名空间

但我有一个命名空间标题 namespace App\Http\Controllers; 在我的 App\Http\ProductController.php 中

虽然我可以通过在集合表单中输入控制器的完整路径来解决问题, 我担心我的代码有配置错误或语法错误等。

【问题讨论】:

    标签: laravel laravelcollective laravel-8


    【解决方案1】:

    Laravel 8 : Collective form - 用于创建 Laravel 视图页面

                {!! Form::open(['route' => '', 'id'=>'form','class' => 'needs-validation',]) !!}           
               
                    <div class="form-group row">
                        {!! Form::label('employee_id', 'Employee ID:', ['class'=>'col-md-1 col-form-label  custom_required']) !!}
                             
                    <!-- Employee ID -->
                    <div class="form-group row">
                        {!! Form::label('employee_name', 'Employee Name:', ['class'=>'col-md-1 col-form-label  custom_required']) !!}
                        <div class="col-lg-8">
                            {!! Form::text('employee_name', @$employee_id, ['class' => 'form-control', 'required', 'placeholder' => 'Employee Name', 'pattern'=> '^[a-z A-Z0-9_.-]*$']) !!}
                                       
                    </div>     
                     <!-- Employee number -->
                     <div class="form-group row">
                        {!! Form::label('phone_number', 'Number:', ['class'=>'col-md-1 col-form-label  custom_required']) !!}
                        <div class="col-lg-8">
                            {!! Form::text('phone_number', @$phone_number, ['class' => 'form-control', 'required','placeholder' => 'number']) !!}
                         
                    </div>    
                          
                    {!! Form::close()  !!}          
                 
    

    【讨论】:

    • 嗨。欢迎来到 SO。如果附上关于试图传达的答案的简短描述/解释会更有帮助。
    • 肯定ranka47下次明确添加简短说明
    • 这是我第一次发帖所以不知道,下次我会明确添加
    • How to answer 这会很有帮助。
    【解决方案2】:

    在 Laravel 8 中:使用此代码我认为您的问题将得到解决

    web.php

    Route::middleware(['auth:sanctum', 'verified'])->group(function () {
        Route::get('create', 'Product\ProductController@Create')->name('product.create')->middleware('can:Add Product');
        Route::post('create', 'Product\ProductController@Store')->name('product.store')->middleware('can:Add Product');
    });
    

    资源/视图/产品:

    <form method="post" action="{{ route('product.store') }}"  enctype="multipart/form-data">
        @csrf
        {{-- START - PRODUCT INFORMATION --}}
    
        {{-- END- PRODUCT INFORMATION --}}
    </form>
    

    应用程序/Http/控制器/产品:

    <?php
    namespace App\Http\Controllers\Products;
    
    use App\Http\Requests\Product\ProductRequest;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    
    class ProductController extends Controller {
        //---Constructor Function
        public function __construct() {
            $this->middleware('auth:sanctum');
        }//---End of Function Constructor
    
        //---Create Product
        public function create() {
            return view('product.product_add');
        }//---End of Function create
    
        //---Store Product in Database
        public function store(ProductRequest $request) {
            //---Add Product Details in DB
        }
    ?>
    

    【讨论】:

      【解决方案3】:

      这是对 Laravel 8 的更改。默认情况下,您的路由没有应用命名空间前缀,并且在生成“动作”的 URL 时没有使用命名空间前缀。

      要在生成操作 URL 时尝试引用的控制器的命名空间前缀,您需要在 App\Providers\RouteServiceProvider 上定义 $namespace 属性:

      protected $namespace = 'App\Http\Controllers';
      

      现在您可以使用该命名空间前缀引用您的操作:

      Form::open(['action' => ['ProductController@update', $product->id], 'method' => 'POST'])
      

      【讨论】:

        猜你喜欢
        • 2016-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-11
        相关资源
        最近更新 更多