Skip to content

Symfony Integration

Ankit Pokhrel edited this page Nov 18, 2020 · 8 revisions

Tus Server

First, add ankitpokhrel/tus-php as your dependency.

$ composer require ankitpokhrel/tus-php

// For Symfony 5

$ composer require ankitpokhrel/tus-php:dev-symfony-5

Now, you can either use Symfony's autowire feature to inject the TUS server directly to your controller or create a service to configure the TUS server.

With Autowiring

  1. Update config/services.yaml file to register Tus services.

    services:
     _defaults:
         autowire: true
         autoconfigure: true
         
     TusPhp\Cache\FileStore:
         class: TusPhp\Cache\FileStore
         arguments:
             $cacheDir: '%kernel.project_dir%/var/cache/'
    
     TusPhp\Tus\Server:
         class: TusPhp\Tus\Server
         arguments:
             $cacheAdapter: '@TusPhp\Cache\FileStore'
         calls:
             - method: setUploadDir
               arguments:
                 - '%kernel.project_dir%/public/uploads'
             - method: setApiPath
               arguments:
                 - '/files'
  2. Create a controller, say TusController, and add a route to serve the request.

    <?php
    
    namespace App\Controller;
    
    use TusPhp\Tus\Server as TusServer;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    
    class TusController extends AbstractController
    {
        // ...
    
        /**
         * Create tus server. Route matches /tus/, /tus and /tus/* endpoints.
         *
         * @Route("/tus/", name="tus_post")
         * @Route("/tus/{token?}", name="tus", requirements={"token"=".+"})
         *
         * @param TusService $tusService
         *
         * @return Response
         */
        public function server(TusServer $server)
        {
            $response = $server->serve();
    
            return $response->send();
        }
    
        // ...
    }

Without Autowiring

  1. Create a service, say TusService in src/Service.

    <?php
    
    namespace App\Service;
    
    use TusPhp\Tus\Server as TusServer;
    use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
    
    class TusService
    {
        /** @var ParameterBagInterface */
        protected $params;
    
        /**
         * TusService constructor.
         *
         * @param ParameterBagInterface $params
         */
        public function __construct(ParameterBagInterface $params)
        {
            $this->params = $params;
        }
    
        /**
         * Configure and get TusServer instance.
         *
         * @return TusServer
         */
        public function getServer()
        {
            $server = new TusServer('redis');
    
            $server
                ->setApiPath('/tus') // tus server endpoint.
                ->setUploadDir($this->params->get('kernel.project_dir') . '/public/uploads'); // uploads dir.
    
            return $server;
        }
    }
  2. Create a controller, say TusController, and add a route to serve the request.

    <?php
    
    namespace App\Controller;
    
    use App\Service\TusService;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    
    class TusController extends AbstractController
    {
        // ...
    
        /**
         * Create tus server. Route matches /tus/, /tus and /tus/* endpoints.
         *
         * @Route("/tus/", name="tus_post")
         * @Route("/tus/{token?}", name="tus", requirements={"token"=".+"})
         *
         * @param TusService $tusService
         *
         * @return Response
         */
        public function server(TusService $tusService)
        {
            $response = $tusService->getServer()->serve();
    
            return $response->send();
        }
    
        // ...
    }

Note that the route matches /tus/, /tus and /tus/* endpoints. If you have followed one of the above steps correctly, you should be able to access the TUS server endpoints at http://yourapp.dev/tus or http://yourapp.dev/tus/.

Clone this wiki locally