Skip to content
Create and validate signed URLs with a limited lifetime
Branch: master
Clone or download
Latest commit 1a6dcb8 Feb 28, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Don't use config() inside config files. Nov 5, 2017
src Apply fixes from StyleCI Feb 9, 2017
tests add support for laravel 5.8 Feb 27, 2019
.editorconfig first commit Aug 14, 2015
.gitattributes Git Aug 14, 2015
.gitignore first commit Aug 14, 2015
.scrutinizer.yml Interface implementation; More tests Aug 14, 2015
.styleci.yml Update .styleci.yml Sep 4, 2018
.travis.yml add support for laravel 5.8 Feb 27, 2019 Rename Aug 14, 2015 Enable MIT badge on GitHub Oct 16, 2017 Update Sep 4, 2018
composer.json wip Feb 27, 2019
phpunit.xml.dist add support for laravel 5.8 Feb 27, 2019


Create secured URLs with a limited lifetime in Laravel

Latest Version on Packagist Build Status Quality Score StyleCI Total Downloads

This package can create URLs with a limited lifetime. This is done by adding an expiration date and a signature to the URL.

This is how you can create signed URL that's valid for 30 days:

UrlSigner::sign('', 30);

The output will look like this:

The URL can be validated with the validate-function.


The package also provides a middleware to protect routes.


As you would have guessed the package can be installed via Composer:

composer require spatie/laravel-url-signer

In Laravel 5.5 the service provider and facade will automatically get registered. In older versions of the framework, just add the serviceprovider, and optionally register the facade:

// config/app.php

'providers' => [

'aliases' => [
    'UrlSigner' => Spatie\UrlSigner\Laravel\UrlSignerFacade::class,


The configuration file can optionally be published via:

php artisan vendor:publish --provider="Spatie\UrlSigner\Laravel\UrlSignerServiceProvider"

This is the content of the file:

return [

    * This string is used the to generate a signature. You should
    * keep this value secret.
    'signatureKey' => env('APP_KEY'),

     * The default expiration time of a URL in days.
    'default_expiration_time_in_days' => 1,

     * These strings are used a parameter names in a signed url.
    'parameters' => [
        'expires' => 'expires',
        'signature' => 'signature',



Signing URLs

URL's can be signed with the sign-method:


By default the lifetime of an URL is one day. This value can be change in the config-file. If you want a custom life time, you can specify the number of days the URL should be valid:

//the generated URL will be valid for 5 days.
UrlSigner::sign('', 5);

For fine grained control, you may also pass a DateTime instance as the second parameter. The url will be valid up to that moment. This example uses Carbon for convenience:

//This URL will be valid up until 2 hours from the moment it was generated.
UrlSigner::sign('', Carbon\Carbon::now()->addHours(2) );

Validating URLs

To validate a signed URL, simply call the validate()-method. This return a boolean.


Protecting routes with middleware

The package also provides a middleware to protect routes:

Route::get('protected-route', ['middleware' => 'signedurl', function () {
    return 'Hello secret world!';

Your app will abort with a 403 status code if the route is called without a valid signature.


Please see CHANGELOG for more information what has changed recently.


$ vendor/bin/phpunit

Usage outside Laravel

If you're working on a non-Laravel project, you can use the framework agnostic version.

Similar libraries

If you need signed url's for CloudFront, consider dreamonkey's package, which is based on this library.


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.


You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.


Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.


The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.