dev-idkwhoami's avatar

Local Temporary URL

Hello everybody,

i am trying to generate a temporary URL for a file. That file is stored in a local drive. As I recall this is possible as of Laravel version 11. So here is my code:

How I generate the URL

Storage::temporaryUrl(Storage::disk('files')->path($this->full_path), now()->addMinutes(5))

and how I configured the filesystem disk:

'files' => [
            'driver' => 'local',
            'root' => env('STORAGE_PATH', storage_path('app/private/files')),
            'serve' => true,
            'throw' => false,
],

But when I try to generate that URL I get hit with the following error message: Symfony\Component\Routing\Exception\RouteNotFoundException - Route [storage.local] not defined.

In case it is relevant im using the following versions: PHP 8.3.8, Laravel 11.30.0, Livewire 3.5.12, Livewire Volt 1.6.5

I hope somebody has an idea why im getting this exception.

1 like
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message you're encountering, Route [storage.local] not defined, suggests that Laravel is trying to generate a temporary URL for a local disk, but the necessary route to serve the file is not set up. In Laravel, when you want to serve files from a local disk using temporary URLs, you need to ensure that the appropriate route is defined.

Here's how you can resolve this issue:

  1. Define the Route: Laravel 11 introduced the ability to serve files from local disks using temporary URLs, but you need to define a route to handle these requests. You can do this by adding the following to your routes/web.php file:

    use Illuminate\Support\Facades\Route;
    use Illuminate\Support\Facades\Storage;
    
    Route::get('/storage/{path}', function ($path) {
        $file = Storage::disk('files')->path($path);
    
        if (!file_exists($file)) {
            abort(404);
        }
    
        return response()->file($file);
    })->where('path', '.*')->name('storage.local');
    

    This route will handle requests to /storage/{path} and serve the file if it exists.

  2. Update the temporaryUrl Call: Ensure that your temporaryUrl method call is correctly generating the URL. You should pass the relative path to the file, not the full path. Here's how you can adjust your code:

    $temporaryUrl = Storage::disk('files')->temporaryUrl($this->full_path, now()->addMinutes(5));
    

    Make sure $this->full_path is the relative path from the root of the disk, not the full system path.

  3. Check Configuration: Ensure your filesystem configuration is correct. You've already set 'serve' => true, which is necessary for serving files. Double-check that the 'root' path is correct and points to the directory where your files are stored.

By following these steps, you should be able to generate temporary URLs for files stored on a local disk without encountering the RouteNotFoundException.

1 like
dev-idkwhoami's avatar

It seems for some reason IntelliSense thinks Storage::disk('files')->temporaryUrl() is not a method so i thought it doesn't exist.

TIL Never trust your IntelliSense ^^

1 like
NikunjBhatt's avatar

When I tried, temporary URL created with Storage::temporaryUrl('example.txt', now()->addSeconds(8));, the URL was kept valid even after 8 seconds. It was serving the file example.txt even after half an hour.

However, if I check the URL with request()->hasValidSignature() in the storage.local route, it always returns false, even within 8 seconds.

I am still not sure how temporary URL works in Laravel (v12).

Please or to participate in this conversation.