#2 Directory Structure of Laravel Application

#2 Directory Structure of Laravel Application

Directory Structure

Directory structure is important to understand the basic structure of any framework and it's data flow.

We'll understand the basic directory structure of Laravel in this article.


Here is the simple directory structure of Laravel Application after it's first installation.

 Laravel Directory Structure at a glance


So, focus on every directory/files of a laravel project, one by one.



app folder is the mother of a laravel application. All of the Models and Controller logic goes in this folder. Here is a full details of app folder with every file and folder's demonstration -

Laravel Directory Structre - App Folder

 See the above image and description in right side to clearify and here I'm demonstrating the most important ones.



In the Controllers folder under Http folder, all of the Controllers are present, which is for MVC's Controller.

There is a basic Controller clss, called Controller.php and all of the other controllers extend thia class's propertiesand methods. We write our logic here.

To send data from controller to view, we've to use this controller. We assign here any data, variables and send them to laravel views with some of the laravel built in functions with(), compact() etc. 



Inside the app folder root, by default, we'll find a User.php file which consist a basic Model for User.Where a basic model structure is defined. In here, inside a User model, we'll see it's extend the Base Model class of Laravel. Any basic modfies, custom database table name, hidden attributes,  relationships are defined in this Model class.

So, we come to the concept of Model of MVC  Framework here. User is that model. 

However, we can change our models to a specific Models folder also, which makes our code more cleaner and beautiful. A Basic User Class is like this -


namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
    use Notifiable;

     * The attributes that are mass assignable.
     * @var array
    protected $fillable = [
        'name', 'email', 'password',

     * The attributes that should be hidden for arrays.
     * @var array
    protected $hidden = [
        'password', 'remember_token',

     * The attributes that should be cast to native types.
     * @var array
    protected $casts = [
        'email_verified_at' => 'datetime',



Bootstrap folder is responsible to initialize/bootstrap the application.

There is a file called app.php, which will do the tasks.

To develop a project, we don't have to put code here initially.

The app file starts like this -


| Create The Application
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)

// ... More



Config Folder is resnponsible all of the configurations like, survice, app classes, database, constants, mail, session, auth, view, cache, cors. All of the configuration are in the specific file name, so there name is self descriptive.

To develop a project, we don't have to put code here initially.



Database folder is very important for a laravel project and it's actually needed to create, update database tables, insert dummy data.



To create a new table or any indexing, or any query execution, we've to put that in database/migrations folder in Laravel.

Suppose, we want a basic table called posts in database, then

  1. We've to create a migration first, and that will store in this folder by this command - php artisan make:migration create_posts_table
  2. It'll create a migrations file like this - 2020_08_23_123020_create_posts_table.php , where the date and time is dependent on your time period.
  3. Then when we run php artisan migrate command, this migrations file logic will store in the migrations folder.



To create dummy data in database, we've to use this seeder. By using seeder, we can insert/update dummy data in database. 

If there is no initialize of empty data insert, there is no use of this seeder.



Public folder is where we put our Stylesheets and Javascripts. And then, from view we've to link that. We don't have to addition work here. index.php will handle autoloading the data here.

So, whenever, we hit an application, it will first comes here, index.php in laravel.



Applications views, components, languages files are stored here.



Application every views are stored here. It'll fullfill the concept of Laravel's MVC(Model, View, Controller) pattern. By default, there is a file welcome.blade.php exists there to give us the initial page view.



In this folder, to manage multi language of our site, we could put different folder here like, en, as, in, then store our known variable in an array and use these in our application. It is helpful for multi language site.



Applications URL, routes are defined here. 



Web.php file manage the initial application's route management. A basic router is already initilized in the pre built web.php.


use Illuminate\Support\Facades\Route;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/', function () {
    return view('welcome');

Which will just return a basic home page url, and return a view welcom.blade.ph for this.



Application encrypted or secured data will stored through here. 

It is not necessary for a basic web application



All test case will be declared here for our Laravel application

It is not necessary for a basic web application



Application system files are stored here

It is not necessary to do anything here while development any project.



All of the sites configurations are stored here. Like database credentials, mail credentials, pusher credentials, broadcasting credentials and so on. A Basic .env file is like this -










It is already well descriptive, so we already understood I think.



All of the packages installed in Laravel project are added here. Whenever we run composer update command, it will update the laravel vendor folder with this packages.



Application to use with built in laravel server, this file is necessary and we dont't have to add anything here. We just run php artisan serve and it'll create a server for us.



Official Documentation of Laravel about Directory Structure - https://laravel.com/docs/7.x/structure


#1 Laravel Project Setup and Installation
#3 Basic about Laravel Routing