Let me start saying i'm new to Api Platform and atm i find it really hard to find all the documentation needed to make something work.
That said, my example problem is quite simple, i want a public endpoint where i can fetch the current mobile app version number (which is simply a string saved somewhere).
What i did until now (based on what i've found online/documentation/demo/symfonycast) is that i created a new PHP object inside my /Entity folder and named it "PublicApi".
use Ramsey\Uuid\Uuid;
#[ApiResource(
collectionOperations: [],
itemOperations: [
'version' => [
'method' => 'GET',
'read' => false,
'write' => false,
'path' => '/version',
'controller' => ApiPublicVersionAction::class,
"openapi_context" => [
"summary" => "Returns current app version.",
"description" => "Public endpoint, no need to authenticate",
'parameters' => [],
'requestBody' => [],
],
],
],
routePrefix: '/public'
)]
class PublicApi {
#[ApiProperty( identifier: true )]
public $code;
#[ApiProperty]
private string $exposedData;
public function __construct() {
$this->code = Uuid::uuid4();
}
doing that i can see the route being recognized by api platform, it shows up in the doc and i can test it. The controller is quite simple, it just has an __invoke method
<?php
namespace App\Controller\Api;
use App\Entity\PublicApi;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ApiPublicVersionAction extends AbstractController {
public function __invoke() {
return ( new PublicApi() )->setExposedData( '1.0.0' );
}
}
While this actually "works" i find it really confusing and i miss some pieces of information. I'd like to know the following:
- Is there another way of creating a custom public endpoint or i am forced to create a new controller class with a single method (__invoke) for EVERY endpoint?
- Can't i just have a single controller for all the endpoint and make api-platform call the corrisponding method? that would be so much more clear and easy to maintain
- What are the actual options that i can pass to: collectionOperations, itemOperations or openapi_context? I can't really find a good list with some good documentation of what i can do
- Why am i forced to have an identifier even in this case where i don't really want to fetch anything and i just want to return some non-structured data? The code i've made requires me to pass a random string (which is the "code" property value that api platform uses as identifier) when i don't really need it
- I've read about DataProvider but in this case i don't really need one, in fact i tested creating a very simple one that simply supports PublicApi::class and it gives me no real advantage. Only one more class/file to maintain
My Answer:
AppVersion. This object has a single property called versionNumber which is a string.
Next, I created a new API resource for this object by creating a new PHP class inside my /Api folder and named it AppVersionResource. This class extends the AbstractResource class provided by Api Platform and specifies the AppVersion object as the data class.
I also added a custom operation to this resource called getVersionNumber which returns the current version number as a string.
Finally, I configured the API platform to expose this resource as a public endpoint by adding it to the api_platform.yaml file.
With these steps, I now have a public endpoint where I can fetch the current mobile app version number.
Rate this post
3 of 5 based on 4619 votesComments