phplaravelapi-platform.com

Unable to use custom controller method as route for API Platform in Laravel


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 ?


Solution

  • The solution I was looking for is to use api platform state provider instead of a controller.