The basic concept of directory structure in Laravel

Tram Ho

1. Introduction

By default the Laravel application directory structure is designed to build both small and large applications. Of course, you can completely reorganize the directory structure as you wish. Laravel hardly imposes restrictions on where classes are located – as long as Composer can automatically load the classes.

Where is the Models folder? When starting to work with Laravel, many developers were confused because they felt the lack of a model folder. However, Laravel has intentionally not created a model folder. We see the word “models” as ambiguous because it has so many different meanings from different people. Some developers see “model” as the whole logic of the application, while others treat “models” as classes that can interact with the database.

For that reason, Laravel chooses to put the default Eloquent model in the app directory, and allows developers to place it elsewhere they want.

Root directory

3. App

The app directory, as you expect, it will contain all the core in your application. We will explore the details of it soon; however, most of the classes in your application are here.

4. Bootstrap

The bootstrap directory contains frameword’s boot files and autoloading configuration files. It also has a cache directory containing the files that the framework generates to improve performance such as routes and services cache files.

5. Config

The config directory, as the name implies, contains all the configuration files. It’s great to go through all of its files with the configurations available to you.

6. Database

The database directory contains database migration and seeds files. If you want, you can also use it to organize an SQLite database.

7. Public

The public directory contains the index.php file, which is the key to all requests to your application. It also contains some resources such as images, JavaScript, and CSS.

8. Resources

The resources directory contains views and raw, untapped resources such as LESS, SASS, or JavaScript. It also contains all the language files in your application.

9. Routes

The routes directory contains all route definitions in your application. By default, there are three route files added with Laravel: web.php, api.php, and console.php.

The web.php file contains routes, RouteServiceProvider, in the middleware group web. It provides session state, CSRF protection, and cookie encryption. If your application does not have a stateless, RESTful API, most of the routes you define are located in the web.php file.

The api.php file contains routes RouteServiceProvider in the middleware group api, it provides rate limiting. The routes are stateless defined, so requests sent to your application via routes will be authenticated by tokens and will not have access to session state.

The console.php file is where you define all the Closure based console commands. Each Closure is bound to a command instance allowing a simple approach to interacting with each command’s IO methods. Although it does not define HTTP routes, it defines console based entry points (routes) in your application.

10. Storage

The storage directory contains your compiled Blade templates files, file based sessions, caches files, and framework-generated files. Inside it includes apps, frameworks, and logs. The app directory is used to store files generated by your application. The framework directory contains files generated from frameworks and caches. Finally, the logs folder contains logs files.

The storage / app / public directory stores user-generated files as avatars, which must be public. You should create a link at public / storage to this directory using the command php artisan storage: link.

11. Tests

The tests folder contains your tests files. For example PHPUnit it provides very complete. Each test class should contain a suffix with the word Test. You can run your test class with the command phpunit or php vendor / bin / phpunit.

12. Vendor

The vendor contains the Composer’s dependencies.

Directory App

1. App

Most of your apps are located in the app directory. By default, this folder has namespaced as App and is automatically loaded by Composer using PSR-4 autoloading standard.

The app directory contains several internal subfolders such as Console, Http, and Providers. Think of Console and Http as the directory providing AP for your application error code. HTTP and CLI protocols are two mechanisms for interacting with your application, but it doesn’t really contain the application’s logic. You can understand, We have a good way to execute commands to your application. The Console folder contains all of your Artisan commands, while the Http folder contains controllers, middleware, and requests.

When you use the make Artisan command, a bunch of folders will be created inside the app directory. For example, the app / Jobs directory will not exist until you execute the make: job command Artisan to generate a job class.

Many classes are generated inside the app directory with the Artisan command. You can review which commands exist with the php artisan list make command in the terminal.

2. Broadcasting

The Broadcasting directory includes all the notification channels for your application. All classes will be generated dynamically using the make: channel statement. That directory does not exist by default, but will be created for you to create the first channel. To learn more about those channels, see the documentation on event broadcasting.

3. Console

The Console folder contains all of your application Artisan commands. Those are the commands generated by the command: command. It also contains the application’s kernel console, which is where you can modify the registered Artisan commands and scheduled tasks.

4. Events

By default this directory doesn’t exist, but it will be generated using the event: generate and make: event commands. The Events folder, as you would expect, will contain event classes. Events can be used to notify other parts of your application that certain actions occur, in addition to its ability to be very flexible and decoupling.

5. Exceptions

The Exceptions folder contains exception handling in your application, but it is also a good place to fire many exceptions by the application. If you want to customize the exception or rendered, you should adjust the Handler class inside the folder.

6. Http

The Http directory contains controllers, middleware, and form requests. All the logic of handling requests into your application will be in this directory.

The Jobs Directory By default this directory does not exist, but it will be generated if you execute the make: job Artisan command. The Jobs folder contains your application’s queueable jobs. Jobs can be queued by the application or to run in sync within the current request life cycle. Jobs can run synchronously while the current request is a “commands” when executed in the command pattern.

7. Listeners

By default this directory does not exist, but it will be generated if you execute the event: generate or make: listener Artisan commands. The Listeners folder contains classes that handle events. The event listeners receive an event instance and execute the logic when the response to the event has been fired. For example, a UserRegistered event must be handled by a SendWelcomeEmail listener.

8. Mail

By default this directory does not exist, but it will be generated if you execute the make: mail Artisan command. The Mail folder contains all classes representing the mail sent by your application. Mail objects allow you to package all logic into one, a class that can be sent by the Mail :: send command.

9. Notifications

By default this directory does not exist, but it will be generated if you execute the make: notification Artisan command. The Notifications folder contains all the “transaction” notifications sent by the application, for example, a notification of the event occurring in your application. In addition, notifications of Laravel can also notify email, Slack, SMS, or save in the database.

10. Policies

By default this directory does not exist, but it will be generated if you execute the make: policy Artisan command. The Policies folder contains convention classes that grant your application permission. Conventions are used to determine if a user can take action on a given resource. For more details, see the authorization documentation.

11. Providers

The Providers folder contains all of your application service providers. Service providers launch your application with services in the service container, register events, or perform any other work to prepare requests for your application.

When you have just installed the project, the directory already contains some providers. Feel free to add your providers as needed.

12. Rules

The directory does not exist by default, but will be created for you if you execute the make: rule Artisan command. The Rules folder includes request objects and custom custom rules valid for your application. Rules are used to summarize complex request logic in a simple object. For more information, see the validation documentation.

End

  • The first article I want to explain about the directory structure components in laravel FW. Hope it will give you a good start. In the next article, I will Request Lifecycle to see where it came from. good bye,
Share the news now

Source : Viblo