繁体   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