PHP code example of polidog / simple-api-bundle

1. Go to this page and download the library: Download polidog/simple-api-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

polidog / simple-api-bundle example snippets




return [
...



    Polidog\SimpleApiBundle\PolidogSimpleApiBundle::class => ['all' => true]
];




declare(strict_types=1);

namespace App\Controller\Api;

use App\Entity\LoginUser;
use Polidog\SimpleApiBundle\Annotations\Api;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class UserController
{
    private $userRepository;

    /**
     * for php7
     * @Route("/user/{id}")
     * @Api()
     */
    #[Route('/user/{$id}', name: 'app_user')]
    #[Api]
    public function me($id): array
    {
        $user = $this->userRepository->find($id);
        return [
            'id' => $user->getId(),
            'name' => $user->getUsername(),
            'avatar' => $user->getAvatar(),
        ];
    }

    /**
     * for php7
     * @Route("/user/post", methods={"POST"})
     * @Api(statusCode=201)
     */
    #[Route('/user/post', name: 'POST')]
    #[Api(201)]
    public function post(Request $request): array
    {
        // TODO save logic.
        return [
            'status' => 'ok',
        ];
    }
}