Skip to content

Symfony Integration

Ankit Pokhrel edited this page Sep 25, 2018 · 8 revisions

Tus Server

  1. Add ankitpokhrel/tus-php as your dependency.

    $ composer require ankitpokhrel/tus-php
  2. 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;
        }
    }
  3. 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\Controller;
    
    class TusController extends Controller
    {
        // ...
    
        /**
         * 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. You can now access tus server endpoints at http://yourapp.dev/tus or http://yourapp.dev/tus/.

Clone this wiki locally