PHP code example of peedro07 / symfony-api-rest-bundle

1. Go to this page and download the library: Download peedro07/symfony-api-rest-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/ */

    

peedro07 / symfony-api-rest-bundle example snippets


class UserController extends AbstractPGTRest

parent::__construct($responseOptionsService);

//UserEntity
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[Groups(["user:read"])] // Initialize the group for serialization
#[ORM\Column(length: 255)]
private ?string $name = null;

//UserController
use App\Repository\UserRepository;
use PGTRest\Attribute\ResponseOptions;
use PGTRest\Controller\AbstractPGTRest;
use PGTRest\Service\ResponseOptionsService;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

    public function __construct(ResponseOptionsService $responseOptionsService)
    {
        parent::__construct($responseOptionsService);  
    }

    #[Route('/users', name: 'app_users_get', methods: ["GET"])]
    #[ResponseOptions(statusCode: 200, groups: ["user:read"], formatDate: 'Y-m-d']) // Set the response options with the desired status code and serialization group
    public function index(UserRepository $userRepository): Response
    {
        $users = $userRepository->findAll();
    
        return $this->view($users); //option 1
        //return $this->view(['all_users' => $users]); option 2
    }

#[ResponseOptions(statusCode: 200, groups: ["user:read"], formatDate: 'Y-m-d'])