PHP code example of violines / rest-bundle

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

    

violines / rest-bundle example snippets




declare(strict_types=1);

namespace App\Entity;

#[Violines\RestBundle\HttpApi\HttpApi]
final class Order
{
    public $amount;
    public $articles;
}

// Or use Doctrine Annotations (



declare(strict_types=1);

namespace App\Controller;

use App\Exception\AuthenticationFailedException;
use App\Entity\Order;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class OrderController
{
    /**
     * @return Order[]
     */
    #[Route('/orders', methods: ['GET'], name: 'find_orders')]
    public function findOrders(): array
    {
        return $this->orderRepository->findOrders();
    }

    #[Route('/order/{id}', methods: ['GET'], name: 'find_order')]
    public function findOrder(int $id): Order
    {
        $order = $this->orderRepository->find($id);

        if (null === $order) {
            throw NotFoundException::id($id);
        }

        return $order;
    }

    /**
     * @param Order[] $orders
     */
    #[Route('/orders/create', methods: ['POST'], name: 'create_orders')]
    public function createOrders(Order ...$orders): Response
    {
        $this->orderRepository->createOrders($orders);

        return new Response(null, Response::HTTP_CREATED);
    }

    #[Route('/order/create', methods: ['POST'], name: 'create_order')]
    public function createOrder(Order $order): Response
    {
        $this->orderRepository->create($order);

        return new Response(null, Response::HTTP_CREATED);
    }
}