This Post Is A Part Of A Series
A course to get you started with Lavarel Development in 4 Hours
- Why Laravel Is The Best Php Framework In 2019, 14 Reasons
- MVC In Laravel, Everything About Model View Controller In Laravel
Laravel framework has gained much traction amongst the PHP community and there are multiple reasons why Laravel is the best PHP Framework available in 2019. To understand how Laravel Framework works, it is crucial to understand, what is MVC In Laravel Framework (Model View Controller) and how a user request is processed in MVC architecture.
What Is A Software Architecture?
To understand what is (Model View Controller) MVC in Laravel, it is essential to understand what is software architecture. It is also crucial to understand how software architecture impacts the successful development, deployment, and maintenance of an application.
So, what is software architecture?
In simple words, software architecture can be defined as a structural solution for various components of your software. Now when I say structural solution, it does not mean the directory structure but, how these components interact with each other. The best architecture for an application would be the one that fulfills the most wanted software characteristics in the best possible manner. These expected characteristics can be from a technical point of view as well as the business requirement of the application.
Let’s look at some of these characteristics to understand better how they might impact the choices you make while choosing the correct architecture for your application. As these operational, as well as technical characteristics expected from the software, can be many, let’s take a few examples:
- Software Performance
- Low Fault Tolerance
You can have a look at the complete list of quality characteristics which might affect your decision on the software architectural pattern.
Now, for example, while assessing the business requirements of the software, the stakeholders tell you that their business is very agile. Due to the agility of the business, they might come up with regular updates, and that would need a change in the various functional aspects of the software regularly. At this point, you would want to include “Flexibility” as one of the essential characteristics of the architecture that you create for the software.
So, in a nutshell, you would choose various technical and business characteristics that are most important and based on that you would come up with a solution of structuring the application. A structure that provisions best to these characteristics would form the architecture of your application. Now, based on the common problems people face, there are specific architectural patterns that are available. Here is a list of common architectural patterns and we are going to focus on Model View Controller because you would find MVC in Laravel Framework.
Introduction To Model, View Controller Architecture, MVC In Laravel Framework
The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the Model, the View, and the Controller. To understand MVC in Laravel in a better way, let’s understand all of these individual components and then move to how they work together in Laravel.
The Model corresponds to the data-related logic or the database of the application. Model is the only component of the application that is responsible for interacting with the database of the application. The other two components, the “Controller” and the “View”, DO NOT interact with the database at all. It’s the Model that is responsible for fetching all the data related to particular business logic and passing in on to the Controller. So for example, if you are building an eCommerce application and there is a product object. It is the product model that is responsible for fetching that product data from the database.
The Controller would contain the business logic of the application. The Controller acts as an interface between the Model and the view. It’s the Controller that requests the Model to provide the data needed from the database. Once the Controller gets the required data, it applies the logic to the data and the passes on the data to the view to be finally displayed it to the user. The Controller is the only component that the user interacts with, more on this once we see how these three interact with each other.
Introduction To Routes
Before I jump into how Model, View, and Controller work together, I would like to introduce you to routes. For me, it makes sense to talk about the routes at this stage. Routes are one aspect of the application workflow that used to confuse me to the core when I started developing my first Laravel application. Routes are like maps and based on the kind of the request that the user is making routes decide to which controller class the user needs to be redirected. So before interacting with the Controller itself, it’s the route that decides to which Controller will cater to the user’s request, and for that matter, whether it needs to be sent to a controller altogether. MVC in Laravel will start making all the more sense when you understand that there are multiple routes each catering to a different kind of incoming request and in turn is sending the request to a controller which is responsible for answering that request.
How Model View And Controller Work Together
Now since we have an understanding of MVC in Laravale and all the individual components of the MVC architecture, let’s see how these connect and see how Model View Controller patterns works in Laravel Framework. I would divide it into steps for a better understanding.
- Step One:
When a user sends a request, the request is sent to the router. Based on the kind of request, the router then decides to which Controller the request needs to be redirected. Once the router has made a choice, the request is passed to the Controller. This is the most basic explanation of Step 1 of the application flow.
Now the Controller has received the request, and the Controller also contains the entire application logic for processing the request. Based on the application logic that the Controller has the Controller starts processing the request. If your program’s logic at this stage needs any data from the database, then the Controller would request for that data from the database.
Let me explain this with an example, for instance, let’s assume that the user has requested to see the shop page and the page contains all the products. At this point, the Controller would need to fetch the product data from the database. The Controller would request the “Product” Model for the products that have been requested by the user. The product model would, in turn, query the database over here and then request for the required products from the database. Once the Model has the required information, it would then pass on the information to the Controller to process the information.
Once the Controller has fetched the required information from the Model, it applies the logic to the information. Based on the business logic, the Controller chooses the view that needs to be returned to the user so that the user sees the information he had requested.
This is how the MVC Architectural pattern in Laravel works. As you start working with Laravel Framework, you tend to realize over, a period of time, that it’s not a rigid MVC framework. There are many other components that do not follow the model view controller architectural pattern strictly. However, having a core understanding of the MVC architectural pattern is crucial to start developing in Laravel Framework
What Are Your Thoughts About MVC In Laravel. Comment To Let Me Know
What are your thoughts on MVC In Laravel framework? I would love to have a discussion and know your thoughts on the article. Please feel free to post your feedback in the comments below and improve upon what we have over here. Happy Commenting 🙂 !