skip to Main Content

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


  1. Chosen as BEST ANSWER

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


  2. I don’t know about api-platform.com, but the way you would specify a controller + method for Laravel route is: [JobController::class, 'test']

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search