Skip to content

Laravel & Lumen Integration

Ankit Pokhrel edited this page Nov 23, 2020 · 9 revisions

Tus Server

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

    $ composer require ankitpokhrel/tus-php
  2. Create a service provider, say TusServiceProvider.php or you can also use existing service provider.

    If you create a new service provider, do not forget to add it to config/app.php.

    <?php
    
    namespace App\Providers;
    
    use TusPhp\Tus\Server as TusServer;
    use Illuminate\Support\ServiceProvider;
    
    class TusServiceProvider extends ServiceProvider
    {
        // ...
    
        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {
            $this->app->singleton('tus-server', function ($app) {
                $server = new TusServer('redis');
    
                $server
                    ->setApiPath('/tus') // tus server endpoint.
                    ->setUploadDir(storage_path('app/public/uploads')); // uploads dir.
    
                return $server;
            });
        }
    
        // ...
    }
  3. Add a route to serve the request.

    // routes/web.php
    
    Route::any('/tus/{any?}', function () {
        return app('tus-server')->serve();
    })->where('any', '.*');
  4. For Laravel, open VerifyCsrfToken.php and exclude your tus server endpoint from Laravel’s CSRF validation.

    // ...
    
    class VerifyCsrfToken extends BaseVerifier
    {
        // ...
    
        protected $except = [
            '/tus',
            '/tus/*',
        ];
    }

    Note that disabling the CSRF validation is not recommended and you should use X-CSRF-TOKEN header in an ideal case. See this and #292.

    You can now access tus server endpoints at http://yourapp.dev/tus or http://yourapp.dev/tus/.

Clone this wiki locally