这里有一段路由:
<?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');
});
});
问题是路由无法"找到"控制器,我尝试了许多不同的方法来解决这个问题,我找到了很多关于这个问题的信息,但没有一个能帮助我解决这个问题,我要么得到"Function () does not exist"
要么得到"Target class [App\\Http\\Controllers\\App\\Http\\Controllers\\SurveyController] does not exist."
我没有我不想将每条路由上的控制器声明为$route->request('path', [Controller::class, 'function'])
,因为随着路由数量的增加,它将对未来的维护产生影响。
我使用的是Route::controller()方法吗?我应该只在每条路由上写控制器吗?
我使用的是Laravel 8.83.5和php 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给出的解决方案,首先需要删除route文件中的名称空间声明(如果从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');
});
});
1条答案
按热度按时间ycl3bljg1#
如果你得到
App\\Http\\Controllers\\App\\Http\\Controllers\\SurveyController
错误,这意味着你只需要传递SurveyController
:我建议切换到新的路由系统,删除
$namespace
像this file和从任何地方的文件。然后,您可以执行
controller(SurveyController::class)