'Laravel API REST for index() returns a json html string to the home page
I'm using Laravel api with a resource controller. My api route
Route::apiResource('todos', TodoController::class)->middleware('auth:api');
Controller
public function index(TodoRequest $request): JsonResponse
{
$response = new Response();
// $withDeleted = $request->has('withDeleted') ? true : false;
$todos = $this->todoService->getAllTodosWithLists($request->query('withDeleted'));
$response->setMessage(ServerMessages::TODOS_RETRIEVE_SUCCESS);
return response()->json(
$response->build($todos),
$response->getCode($todos)
);
}
class TodoRequest extends FormRequest {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
* Must match with FrontEnd fields
*
* @return array
*/
public function rules(): array
{
return [
'content' => 'required'
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages(): array
{
return ['required' => 'The :attribute must not be left blanked.'];
}
}
My problem is everytime I add the TodoRequest in my parameter's it always return a response redirecting back to the default Laravel homepage.
Note: I am passing a query parameter inside my $request i.e withDeleted and probably will add more filters. This app was originally made in Lumen which perfectly well, and I did some migration to the latest Laravel 9 framework.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
