簡體   English   中英

Laravel 在路由上使用方法 ->controller() 拋出“Function () does not exist”

[英]Laravel throwing "Function () does not exist" with method ->controller() on routes

我這里有這條路由:

<?php

use App\Http\Controllers\SurveyController;

Route::middleware(['auth:api'])->controller(SurveyController::class)->group(function ($route) {
    $route->prefix('survey')->group(function ($route) {
        $route->post('show', ['show'])->name('survey_show');
        $route->post('answer', ['answer'])->name('survey_answer');
    });
    $route->prefix('member/survey')->group(function ($route) {
        $route->post('list', ['list'])->name('member_survey_list');
    });
});

問題是路由無法“找到”controller,我嘗試了很多不同的方法來解決這個問題,我找到了很多關於這個問題的信息,但沒有一個能幫助我解決問題,要么我得到"Function () does not exist""Target class [App\\Http\\Controllers\\App\\Http\\Controllers\\SurveyController] does not exist." . 我不想在每條路線上將 controller 聲明為$route->request('path', [Controller::class, 'function'])因為隨着路線數量的增加,它將對未來的維護產生影響。

我在使用方法 Route::controller() 嗎? 我應該在每條路線上寫 controller 嗎?

我正在使用Laravel 8.83.5php 8.1.13

更新

我的調查控制器

<?php

namespace App\Http\Controllers;

use App\Http\Resources\SurveyResource;
use App\Services\SurveyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Exception;

class SurveyController extends Controller
{
    private SurveyService $service;

    public function __construct(SurveyService $surveyService)
    {
        $this->service = $surveyService;
    }

    /**
     * php doc here
     */
    public function list(Request $request): array
    {
        $data = $request->only(['keys']);

        $validator = Validator::make(
            $data,
            [
                'keys' => 'string'
            ],
            $this->customMessages
        );

        if ($validator->fails()) {
            throw new Exception($validator->errors(), 1);
        }

        return SurveyResource::method(
            $this->service->method(
                $data['keys']
            )
        );
    }

    /**
     * php doc here
     */
    public function show(Request $request): array
    {
        $data = $request->only(['keys']);

        $validator = Validator::make(
            $data,
            [
                'keys' => 'string'
            ],
            $this->customMessages
        );

        if ($validator->fails()) {
            throw new Exception($validator->errors(), 2);
        }

        return SurveyResource::show(
            $this->service->show(
                $data['keys']
            )
        );
    }

    /**
     * php doc here
     */
    public function answer(Request $request): array
    {
        $data = $request->only(['keys']);

        $validator = Validator::make(
            $data,
            [
                'keys' => 'string'
            ],
            $this->customMessages
        );

        if ($validator->fails()) {
            throw new Exception($validator->errors(), 3);
        }

        return SurveyResource::answer(
            $this->service->answer(
                $data['keys']
            )
        );
    }
}

在路線上,我嘗試首先使用方法 controller() 調用路線,這樣我得到一個"Function () does not exist"錯誤,當我在 group() 之前使用它時會彈出同樣的錯誤方法

Route::controller(SurveyController::class)->middleware(['auth:api'])->group(function ($route) {    
    $route->prefix('member/survey')->group(function ($route) {
        $route->post('list', ['list'])->name('member_survey_list');
    });
});

還嘗試使用沒有數組的 SurveyController 方法調用路由,這樣我得到一個"Target class [App\\Http\\Controllers\\App\\Http\\Controllers\\SurveyController] does not exist." 錯誤

Route::controller(SurveyController::class)->middleware(['auth:api'])->group(function ($route) {    
    $route->prefix('member/survey')->group(function ($route) {
        $route->post('list', 'list')->name('member_survey_list');
    });
});

還看到一些舊線程說在 group() 方法中使用帶有命名空間鍵和命名空間值的數組可能會有所幫助,所以我嘗試了,但出現了"Array to string conversion"錯誤

Route::middleware(['auth:api'])->group(['namespace' => 'App\Http\Controllers\SurveyController'],function ($route) {    
    $route->prefix('member/survey')->group(function ($route) {
        $route->post('list', 'list')->name('member_survey_list');
    });
});

解決方案

按照@matiaslauriti 給出的解決方案,首先需要刪除路由文件中命名空間的聲明(如果將其從 RouteServiceProvider.php 中刪除,則可以保留該聲明),然后您可以將其稱為Route::controller(ControllerName::class)或字面意思為Route::controller('ControllerName') 這是我的工作代碼:

<?php

Route::controller(SurveyController::class)->middleware(['auth:api'])->group(function ($route) {
    $route->prefix('survey')->group(function ($route) {
        $route->post('show', 'show')->name('survey_show');
        $route->post('answer', 'answer')->name('survey_answer');
    });
    $route->prefix('member/survey')->group(function ($route) {
        $route->post('list', 'list')->name('member_survey_list');
    });
});

如果您收到App\\Http\\Controllers\\App\\Http\\Controllers\\SurveyController錯誤,這意味着您只需按字面意思傳遞SurveyController

Route::controller('SurveyController')->middleware(['auth:api'])->group(function ($route) {    
    $route->prefix('member/survey')->group(function ($route) {
        $route->post('list', ['list'])->name('member_survey_list');
    });
});

我建議切換到新的路由系統,像這個文件一樣刪除$namespace ,並從那個文件的任何地方刪除。

然后,你可以做controller(SurveyController::class)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM