PHP code example of condenast-ru / basic-api-bundle

1. Go to this page and download the library: Download condenast-ru/basic-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/ */

    

condenast-ru / basic-api-bundle example snippets


 declare(strict_types=1);
# config/bundles.php

return [
    # ...
    Condenast\BasicApiBundle\CondenastBasicApiBundle::class => ['all' => true],
];

 declare(strict_types=1);

use Condenast\BasicApiBundle\Annotation as Api;
use Condenast\BasicApiBundle\Request\QueryParamBag;
use Condenast\BasicApiBundle\Response\Payload;
use Condenast\BasicApiBundle\Tests\Fixtures\App\DTO\Article;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

class ArticleController
{
    /**
     * Create article
     *
     * @OA\Response( # Response description, for an API documentation only
     *     response=201,
     *     description="Created article",
     *     @OA\JsonContent(
     *         type="object",
     *         ref=@Nelmio\Model(type=Article::class, groups={"article.read"})
     *     )
     * )
     */
    #[Route(path: "/articles", name: "app.articles.post", methods: ["POST"])]
    #[Api\Resource("Article")] # Resource name used to group actions in API documentation
    #[Api\Deserialization(
        argument: "articles", # The argument of the controller method, the result of deserialization will be passed there
        type: Article::class, # The type of deserialization, such as Article or Article [] for an array of articles
        context: ["groups" => "article.write"], # Deserialization context,
        requestAttributes: ["id" => "id"] # Request attributes to assign their values to the properties of the deserialized object
                                          # It can be useful when, for example, in an edit action you deserialize the DTO, and the identifier of the entity is in the url
    )]
    #[Api\Validation(groups: ["article.write"])] # Validation groups
    #[Api\QueryParam(
        name: "tags", # The name by which the parameter will be available in the QueryParamBag
        path: "extra.tags", # The path to the parameter in the request, if not specified, will be equal to the name.
        isArray: true, # Whether the parameter is an array
        constraints: [ # Validation constraints
            new Assert\Length(min=2),
        ],
        default: [], # Default parameter value
                     # If not specified, then null or an empty array, depending on whether the parameter is declared as an array
                     # If the parameter value does not meet the