- 2 years ago
- Zaid Bin Khalid
- 1,642 Views
-
3
The application structure in Laravel consists of the structure of folders, sub-folders, and files included in a project. Once we make a project in Laravel, we get a summary of your application structure as shown in the image here.
The root folder of Laravel is known as the app name that you created. Below is the screenshot. It has various sub-folders and files. The study of folders and files, along with their functional features, is given below.
App
The App is in the application folder. It has the complete source code of the project. It contains events, exceptions, and middleware statements. The App folder deals with different sub folders as explained below.
Console
The console gives the artisan commands that are important for Laravel. You can run any available command with the help of php artisan make:model. In the Kernel file, all the commands are handel.
If we call a specific command in Laravel, then we should make relevant changes in this file.
Events
If you are not able to see events dir don’t worry when you run any event this will be created automatically. You can create events with `php artisan make:event PodcastProcessed` command once you create you will see the folder. This folder has all the events for the project.
Events set off activities, and raise errors or necessary proofs. It gives more flexibility. Laravel manages all the Events on file which is given in Providers/EventServiceProvider.php.
Exceptions
This folder has all the ways needed to control exceptions. It also contains a file Handler.php that controls all the exceptions.
Http
The HTTP folder contains sub-folders for controllers and middleware. It has models, controllers, and views defined for the specific files as Laravel follows the MVC design pattern.
The Middleware sub-folder contains the middleware mechanism, dealing with the filter mechanism and communication between response and request.
Jobs
The Jobs directory controls the activities lined up for the Laravel application. The base class is shared among all the Jobs. It provides a central location to place them under one roof.
Listeners
Listeners are event-dependent. They have ways that are used to control events and exceptions. For example, the login event stated contains a LoginListener event.
Policies
Policies are the PHP classes that have the authorization logic. Laravel gives a feature to create all authorization logic within code classes inside this sub folder.
Providers
This folder has all the service providers needed to register events for core servers and to design a Laravel application.
Bootstrap
This folder inserts all the application bootstrap scripts. It contains a sub-folder, namely cache. Cache has all the files linked with caching a web application. You can also find the file app.php, which starts the scripts important for bootstrap.
Config
The config folder has different designs and related parameters needed for the smooth functionality of a Laravel application. Different files included within the config folder are as shown in the image below. The filenames work as per the functionality related to them.
Database
This directory contains different parameters for database functionalities. It has three sub-directories as given below.
- Seeds − It contains the classes used for the unit testing database.
- Migrations − This folder helps in questioning for migrating the database used in the web application.
- Factories − We use this folder to generate many data records.
Public
It is the root folder that helps in starting the Laravel application. It contains the following files and folders.
- .htaccess − This file gives the server design.
- javascript and css − We considered these files as assets.
- index.php − We need this file for the initialization of a web application.
Resources
The resources directory contains the files which improve your web application. This directory contains the following sub-folders. Their purpose is explained below.
- assets − The assets folder has files, such as LESS and SCSS. We need these files for styling the web application.
- lang −It has a folder, that contains designs for limitation or internalization.
- views − Views are the HTML files or templates which link with end-users and play a primary role in MVC architecture.
Now notice that the resources file destroys rather than having an assets folder. You can see the graphic representation of the same below.
Storage
Storage is the folder that has all the logs and important files. We need such files regularly when a Laravel project is running. It contains the following sub-folders. Their purpose is given below.
- App − It has the files that are called in succession.
- Framework − It contains sessions, cache, and views. We call these regularly.
- Logs − We use this subfolder to trace all exceptions and error logs.
Tests
This directory contains all the unit test cases. The naming convention for naming test case classes is camel_case. It follows the convention as per the functionality of the class.
Vendor
Laravel totally depends on Composer dependencies. For example, we need Composer dependencies to install Laravel setup or to include third-party libraries, etc. The Vendor folder has all the composer dependencies.
Laravel also contains some other files which play a key role in different purposes like GitHub design, packages, and third-party libraries.
Laravel Configuration
We learned about the basic configuration files of Laravel are included in the config directory. Now let us discuss the categories included in the configuration.
Environment Configuration
Environment variables are those which give a list of web services to your web application. We declare all the environment variables in the .env file. It has the parameters needed for starting the configuration.
The .env file has the following parameters by default.
APP_ENV = local
APP_DEBUG = true
APP_KEY = base64:ZPt2wmKE/X4eEhrzJU6XX4R93rCwYG8E2f8QUA7kGK8 =
APP_URL = http://localhost
DB_CONNECTION = mysql
DB_HOST = 127.0.0.1
DB_PORT = 3306
DB_DATABASE = YOUR-DB
DB_USERNAME = YOUR-DB-USER
DB_PASSWORD = YOUR-DB-PASSWORD
CACHE_DRIVER = file
SESSION_DRIVER = file
QUEUE_DRIVER = sync
REDIS_HOST = 127.0.0.1
REDIS_PASSWORD = null
REDIS_PORT = 6379
MAIL_DRIVER = smtp
MAIL_HOST = mailtrap.io
MAIL_PORT = 2525
MAIL_USERNAME = null
MAIL_PASSWORD = null
MAIL_ENCRYPTION = null
Important Points
We should notice the following points while working with the basic configuration files of Laravel.
- We should not link the .env file to the application source control, as each developer or user has some fixed environment configuration for the web application.
- The development team should include the .env.example file for backup options. It should contain the default configuration.
Retrieval of Environment Variables
We can enter all the environment variables stated in the .env file by env-helper functions. It will call the respective parameter. These variables are also listed in the $_ENV global variable whenever the application receives a request from the user end. You can access the environment variable as shown below.
'env' => env('APP_ENV', 'production'),
env-helper functions are called in the app.php file. It includes in the config folder. The above-given example is calling for the basic local parameter.
Accessing Configuration Values
You can easily obtain the configuration values anywhere in the application using the global config helper function. The default values will return if the configuration values do not start in the case.
For example, We use the following code to set the default time zone.
config(['app.timezone' => 'Asia/Tokyo']);
Caching of Configuration
It is important to cache all the configuration values to increase the performance of the web application. The command for caching the configuration values is as follows.
php artisan config:cache
# Below will be the output
Configuration cache cleared successfully.
Configuration cached successfully.
Maintenance Mode
You may need to update some configuration values or perform maintenance on your website sometimes. If you keep your website or configuration values in maintenance mode, it will be easier for you. The web applications in maintenance mode, launch an exception namely MaintenanceModeException with a status code of 503.
You can allow the maintenance mode on your Laravel web application. Use the following command to do it.
The following screenshot shows how the web application looks when it is down.
After finishing working on updates and other maintenance on your web application, you can disable the maintenance mode. You can type the following command to do it.
Now, you can find that the website shows the output with proper functioning. It shows that the maintenance mode is now removed, as shown below.
There are many other artisan commands that you can use to work in Laravel.
- 2 years ago
- Zaid Bin Khalid
- 1,642 Views
-
3