Folder and file structure in Laravel

- Andrés Cruz

En español
Folder and file structure in Laravel

Este material forma parte de mi curso y libro completo; puedes adquirirlos desde el apartado de libros y/o cursos Curso y libro Laravel 11 con Tailwind Vue 3, introducción a Jetstream Livewire e Inerta desde cero - 2024.

A project in Laravel can be very changeable; Depending on the configuration you make, as well as the packages you install, you may have more folders and files; starting with version 11 of Laravel, we have some important changes that we must take into account; let's look at the main folders and files of a project in Laravel:

The app folder

The app folder contains the core code of the application. This is the central folder of the project where we will spend most of our time; in this file are almost all the classes of the application. 

The bootstrap folder

The Bootstrap folder contains the app.php file that bootstraps the framework; the first file that is executed is the public/index.php file which finally loads the mentioned app.js file. This folder also has a cache folder that contains files generated by the performance optimization framework, such as route and service cache files. Usually no changes are needed here.

The config folder

The config folder, as its name implies, contains all of your application’s configuration files; database, cors, jetstream, app and many more.

The database folder

The config folder, as its name implies, contains all of your application’s configuration files; database, cors, jetstream, app and many more.

The lang folder

The lang folder houses all the language files; by default, it is not included in Laravel 11; you can publish the folder in case you need to use multiple languages in your application with:

$ php artisan lang:publish

The public folder

The public folder contains the index.php file, which is the entry point for all requests coming into your app and configures autoloading. This folder also have files that can be handled by the browser such as images, JavaScript, and CSS.

The resource folder

The resources folder contains your views, as well as your raw assets, such as CSS or JavaScript. 

The routes folder

The routes folder contains all the route definitions for your application. By default, several route files are included with Laravel: web.php, api.php, console.php and channels.php.

  1. The web.php file contains routes that are used to drive the web application; that is, the one consumed by the browser; these routes are configured to provide session state, CSRF protection, and cookie encryption. 
  2. The channels.php file is where you can register all the event streaming channels your application supports. 

Starting in Laravel 11, to publish the following files that were marked as optional:

  1. The api.php file contains the routes for the creation of a Rest Api; these routes are designed to be stateless, so requests entering the application through these routes must be authenticated using tokens and will not have access to session state. 
  2. The console.php file is where you can define all of your artisan commands. 

We must execute the artisan commands:

$ php artisan install:api

And

$ php artisan install:broadcasting 

Respectively.

Generating the routes for the API will cause Laravel Sanctum to be installed, which we will talk about in another chapter:

./composer.json has been updated

Running composer update laravel/sanctum

Loading composer repositories with package information

Updating dependencies

Lock file operations: 1 install, 0 updates, 0 removals

  - Locking laravel/sanctum (v4.X)

Writing lock file

Installing dependencies from lock file (including require-dev)

Package operations: 1 install, 0 updates, 0 removals

  - Downloading laravel/sanctum (v4.X)

  - Installing laravel/sanctum (v4.X): Extracting archive

The storage folder

The storage folder contains your logs, compiled Blade templates, sessions files, caches, and other files generated by the framework; this folder can be used to store any files generated by your application. 

The test folder

The test folder contains your automated tests; that is, the PHPUnit unit tests. 

The vendor folder

vendor folder contains your Composer dependencies.

The app folder 

Most of the application is located in the app folder, which as we mentioned before, is where we will spend most of our time; most of the classes are in this folder and we can define files with configurations for different purposes. 

In this folder, other folders are located; We will explain the main ones: 

The Http folder

The Http folder contains your controllers, middleware, and form requests.

The Models folder

The Models folder contains all the Eloquent model classes. Each database table has a corresponding ”Model” that is used to interact with that table. Models allow you to query data in your tables, as well as insert, update, and delete records in the table.

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.