An introduction to laravel-export with FTP

Exporting my Laravel blog to static pages

Published in Laravel on Apr 27, 2022

After developing this blog, I didn’t feel like setting up a server for this. I am not using a database because I want to keep the architecture simple. Therefore, I decided to export my website to static HTML and CSS pages. After all: after writing a post, there is nothing dynamic about the website.

Generating static pages with PHP

Generating a static page from a PHP script is very simple. You call your script from the CLI and store everything in an .html file. A child can do the job.

$ php index.php

Of course, it is important that you also include the assets: think CSS and possibly Javascript.

However, for my application, this is not sufficient. In fact, in a Laravel application, everything goes through the index.php. So you will have to create a request with all the relevant data for each page you have.

Using laravel-export

For this purpose, laravel-export has been developed by Spatie.

Like most packages from Spatie, this one is very easy to use. Installation is done with composer.

$ composer require spatie/laravel-export

After this you have the possibility to publish the config file, if you want. Here you can set additional things, like running an npm run prod before exporting, or running a deployment hook after exporting. If you want, you can publish it with:

$ php artisan vendor:publish --provider=Spatie\\Export\\ExportServiceProvider

Once you have set what you wanted to set, you can run your export with:

$ php artisan export

By default, all files are placed in dist/. You can change this in the config file:

// config/export.php

return [
    'disk' => 'export',
];

An advantage of this is that you can place your files directly on S3, for example. You can then set this up to host your static website. Another option is to put your files on a server directly via FTP, if you have a standard, boring web server. That’s the case with me, so I’m going to set that up today.

Setting up FTP for laravel-export

To export my files directly to my server with FTP, I need to set FTP as a filesystem in Laravel. In addition, I need to tell laravel-export that I want to use this FTP filesystem.

Setting up the FTP filesystem

Let’s start with the first thing: setting up the FTP filesystem. A good start is to just read the documentation on Laravel’s filesystems.

First, we need to install the Flysystem FTP package via the Composer package manager:

$ composer require league/flysystem-ftp "^3.0"

Then, we need to tell Laravel about the FTP filesystem. This is done by adding the Ftp driver to the filesystems array in the config/filesystems.php file:

// config/filesystems.php

'disks' => [

    'ftp' => [
        'driver' => 'ftp',
        'host' => env('FTP_HOST'),
        'username' => env('FTP_USERNAME'),
        'password' => env('FTP_PASSWORD'),
        'root' => env('FTP_ROOT'),
    ],

    // Other disks
],

Of course, this also means that you need to set the FTP credentials in your .env file.

FTP_HOST=ftp.example.com
FTP_USERNAME=username
FTP_PASSWORD=password
FTP_ROOT=/path/to/root

That’s it. Now, you can export your files to your FTP server.

Exporting to FTP

Now, you can export your files to your FTP server. It’s time to tell laravel-export to use the FTP filesystem.

// config/export.php

'disk' => 'ftp',

And that’s all. Now, you can export your files to your FTP server with a simple command:

$ php artisan export

The post you are currently reading, has been published while I was writing the blog itself.

Got any questions? Feel free to shoot me a message on Twitter.