How to change env dir to the parent folder in Laravel and Vite

Over the years, my appreciation for the following folder structure has grown significantly for web projects.

1/
2|---frontend (vite app)
3|---backend (laravel app)
4.env

It is usually easiest to have one .env file in your root.

In Laravel, add the following line to bootstrap/app.php.

1<?php
2
3$app = new Illuminate\Foundation\Application(
4 $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
5);
6
7$app->useEnvironmentPath(
8 dirname(__DIR__, 2)
9);

This sets the envirornment path to the parent folder.

In Vite, set the envDir config in vite.config.ts.

1import { sveltekit } from '@sveltejs/kit/vite';
2import { defineConfig } from 'vitest/config';
3
4export default defineConfig({
5 plugins: [sveltekit()],
6 envDir: '../',
7});

References

Newsletter

Comments

Published with Hyvor Blogs