此问题已在此处有答案:
Route [login] not defined(20个回答)
3天前关闭。
我想为数据库操作(创建、编辑、删除)启用访问路由,只对经过身份验证的用户,但我一直在Postman中收到登录名未定义的消息。我希望删除路由在中间件中。
这就是我的API:
<?php
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\ExamController;
use App\Http\Controllers\StudentExamController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// Ova ruta nije zaštićena i dostupna je svim korisnicima
Route::post('register', [AuthController::class, 'register']);
Route::post('login', [AuthController::class, 'login']);
// Grupa ruta zaštićenih middleware-om auth:sanctum
Route::group(['middleware' => ['auth']], function() {
// Rute za manipulaciju bazom
Route::delete('exams/{id}/delete', [ExamController::class, 'destroy']);
Route::post('exams/create', [ExamController::class, 'create']);
Route::put('exams/{id}/edit', [ExamController::class, 'edit']);
// Rute za logout
Route::post('logout', [AuthController::class, 'logout']);
});
// Rute koje ne zahtevaju autentifikaciju
Route::get('users', [UserController::class, 'index']);
Route::get('users/{id}', [UserController::class, 'show']);
Route::get('students', [StudentController::class, 'index']);
Route::get('students/{id}', [StudentController::class, 'show']);
Route::get('exams', [ExamController::class, 'index']);
Route::get('studentexams', [StudentExamController::class, 'index']);
Route::get('studentexams/{id}', [StudentExamController::class, 'showExams']);
这是我的ExamController.php:
<?php
namespace App\Http\Controllers;
use App\Models\Exam;
use Illuminate\Http\Request;
use App\Http\Resources\ExamResource;
class ExamController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return response()->json(Exam::all());
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Exam $exam
* @return \Illuminate\Http\Response
*/
public function destroy($exam_id)
{
$examIds = array_values((array) Exam::pluck('id'))[0];
if(!in_array($exam_id, $examIds)){
return response()->json([
'message' => 'Exam ID does not exist',
]);
}
$Exam = Exam::find($exam_id);
if (!$Exam->delete()) {
return response()->json([
'error' => 'Unable to delete the Exam'
]);
}
$this->middleware('auth');
}
}
我尝试添加$this-〉middleware('auth'),但没有成功。我也尝试了所有已经存在的Stack overflow,什么都没有
1条答案
按热度按时间u59ebvdq1#
从ExamController中删除
$this->middleware('auth');
,并尝试将您在web.php中的中间件更改为:为此: