Laravel
Learn how to configure Laravel for use with Filebase.

What is Laravel?

Laravel is a web application framework written using PHP that offers an easy to read foundation for building web apps.
Laravel can be configured to use S3-compatible storage such as Filebase. Read below to learn how to configure Laravel for use with Filebase.

Prerequisites:

  • Download and install Composer at the Command Line.
  • Have Your Filebase Access and Secret Keys. Learn how to view your access keys here.
  • Create a Filebase Bucket. Learn how to create a bucket here.
This guide was configured and tested using Ubuntu 20.04. Commands may vary depending on your operating system.

1. Create a new Laravel project with the following command:

composer create-project laravel/laravel PROJECT-NAME

2. Use the cd command to move into the newly created project directory.

3. Create a new file in the ./resources/views directory and name it fileUpload.blade.php with the following contents:

1
@extends('layouts.app')
2
3
@section('content')
4
<div class="container">
5
<div class="row justify-content-center">
6
<div class="col-md-8">
7
<div class="card">
8
<div class="card-header">File Upload</div>
9
<div class="card-body">
10
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data"
11
@csrf
12
13
<input type="file" name="file" class="form-control">
14
<input type="submit" class="btn btn-primary">
15
16
</form>
17
</div>
18
</div>
19
</div>
20
</div>
21
</div>
22
23
@endsection
Copied!

4. Move back into your project’s top directory and open the ./routes/web.php file.

Replace the existing contents with the following:
1
<?php
2
3
Route::get('/', function () {
4
return view('fileUpload');
5
});
6
7
Route::post('upload',function(){
8
request()->file('file')->store(
9
'my-file',
10
'Filebase'
11
);
12
})->name('upload');
13
Auth::routes();
14
Route::get('/home', '[email protected]')->name('home');
Copied!
‘My-file’ refers to the directory that your files will be uploaded to. Change this value to reflect your intended configuration.

5. Move back into your project’s top directory and open the .env file.

Replace the existing contents with the following:
1
APP_NAME=Laravel
2
APP_ENV=local
3
APP_KEY=base64:ZBWX+Pgx8ABy8CThdSi9ixRCyQmKQ1iWDsv1TSCMBUM=
4
APP_DEBUG=true
5
APP_URL=http://localhost
6
LOG_CHANNEL=stack
7
8
DB_CONNECTION=mysql
9
DB_HOST=127.0.0.1
10
DB_PORT=3306
11
DB_DATABASE=laravel
12
DB_USERNAME=root
13
DB_PASSWORD=
14
15
BROADCAST_DRIVER=log
16
CACHE_DRIVER=file
17
QUEUE_CONNECTION=sync
18
SESSION_DRIVER=file
19
SESSION_LIFETIME=120
20
21
REDIS_HOST=127.0.0.1
22
REDIS_PASSWORD=null
23
REDIS_PORT=6379
24
25
MAIL_DRIVER=smtp
26
MAIL_HOST=smtp.mailtrap.io
27
MAIL_PORT=2525
28
MAIL_USERNAME=null
29
MAIL_PASSWORD=null
30
MAIL_ENCRYPTION=null
31
32
AWS_ACCESS_KEY_ID=
33
AWS_SECRET_ACCESS_KEY=
34
AWS_DEFAULT_REGION=us-east-1
35
AWS_BUCKET=
36
37
PUSHER_APP_ID=
38
PUSHER_APP_KEY=
39
PUSHER_APP_SECRET=
40
PUSHER_APP_CLUSTER=mt1
41
42
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
43
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
44
45
FB_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX
46
FB_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
47
FB_DEFAULT_REGION=us-east-1
48
FB_BUCKET=filebase-sample-bucket
49
FB_URL=s3.filebase.com
Copied!
You will need to change the FB_ACCESS_KEY_ID and FB_SECRET_ACCESS_KEY values to reflect your Filebase Access and Secret Keys.

6. Move back into your project’s top directory and open the ./config/filesystems.php file.

Replace the existing contents with the following:
1
<?php
2
return [
3
'default' => env('FILESYSTEM_DRIVER', 'local'),
4
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
5
'disks' => [
6
'local' => [
7
'driver' => 'local',
8
'root' => storage_path('app'),
9
],
10
11
'public' => [
12
'driver' => 'local',
13
'root' => storage_path('app/public'),
14
'url' => env('APP_URL').'/storage',
15
'visibility' => 'public',
16
],
17
18
'Filebase' => [
19
'driver' => 's3',
20
'key' => env('FB_ACCESS_KEY_ID'),
21
'secret' => env('FB_SECRET_ACCESS_KEY'),
22
'region' => env('FB_DEFAULT_REGION'),
23
'bucket' => env('FB_BUCKET'),
24
'endpoint' => 'https://s3.filebase.com'
25
],
26
],
27
];
Copied!

7. Next, install the Laravel driver with the commands:

php artisan make:auth
composer require league/flysystem-aws-s3-v3 "^1.0"

8. Then start your Laravel server with the following command:

php artisan serve

9. Then go to your web browser and navigate tohttp://127.0.0.1:8000.

10. You will see a screen that prompts file uploads.

Each file uploaded will be uploaded to your Filebase bucket that you specified in the .env file.
If you have any questions, please join our Discord server, or send us an email at [email protected]