I’m starting to play around with Laravel’s implementation of API Platform.
What I need to do is to have an create a custom route that points to a specific controller’s method. So far the documentation does not show any example on this use case.
I tried soemthing like this :
#[ApiResource]
#[GetCollection]
#[Get(
uriTemplate: '/jobs/test',
controller: JobController::class . '::test'
)]
class Job extends Model
{ ... }
// JobController.php
public function test()
{
return response()->json([
'message' => 'This is my test',
]);
}
But all i get is the first result of my jobs in the database. but not the response inside test().
Does anyone know how to achieve this ?
2
Answers
The solution I was looking for is to use api platform state provider instead of a controller.
I don’t know about
api-platform.com
, but the way you would specify a controller + method for Laravel route is:[JobController::class, 'test']