Goal
In the Image upload with Laravel 2/3: API route, .env and cors.php we will create a backend api route, configure environment variables and ensure the communication between frontend and backend (CORS).
Prerequisites
The laravel backend of part 1 should already be implemented.
Add backend route
First we will add a route for post requests in the file
api.php
located in the directory backend/routes
: api.php
Before
get("/user", function (Request $request) {
return $request->user();
});
api.php
Change
Route::post("/save/image", function (Request $request) {
(new App\Http\Controllers\ImageController())->saveImage($request);
});
api.php
After
get("/user", function (Request $request) {
return $request->user();
});
Route::post("/save/image", function (Request $request) {
(new App\Http\Controllers\ImageController())->saveImage($request);
});
We will make use of this route in the frontend in part 3.
Configure backend .env file
Next we will modify the .env
-File located in the root folder of the project.
.env
Before
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:es1A0OLpKyOO2lSodFxbJZI5KX5VDNIbdN8zbFzn7+0=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=backend
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
We will add the following key value pairs at the end of the file:
.env
Change
FRONTEND_URL=http://localhost:3000
BACKEND_URL=http://localhost:8000
With that we tell Laravel that our frontend will be available at
http://localhost:3000
and our backend at http://localhost:8000
. The complete .env
– file finally looks like this:
.env
After
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:es1A0OLpKyOO2lSodFxbJZI5KX5VDNIbdN8zbFzn7+0=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=backend
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
FRONTEND_URL=http://localhost:3000
BACKEND_URL=http://localhost:8000
For a smooth communication between the frontend and backend we will add the frontend URL we already defined in the
.env file
before in file backend/config/cors.php
: cors.php
Before
['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
];
In line 22 we will change the value
allowed_origins
from ['*']
to ['http://localhost:3000']
: cors.php
Change
'allowed_origins' => ['http://localhost:3000'],
The file finally looks like this:
cors.php
After
['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['http://localhost:3000'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
];
More details about Cross Origin Ressource Sharing (CORS) is available here CORS-Error.
We took care of the communication between frontend and backend by assigning URLs with fixed ports for both ends.
Next part
React
Image upload with Laravel
3/3: React Frontend
In the last part of the image upload with laravel series we will create the react frontend. This provides a user interface for selecting and uploading the image.