10 => Models, Views & Controllers
This blog will cover the basics of what the MVC pattern is and how Laravel implements the pattern. The beginning of this entry will provide a high level overview of the pattern and will dive into some helpful functionality that Laravel provides that makes using the pattern easier.
Model
The applications models contain the applications main business logic. They are the main part of the application. The controllers delegate jobs to models and return views to the client.
Models can be created using the artisan command
php artisan make:model [name]
By including the -c flag the previous command will make a Controller for that given model with CRUD methods scaffolded.
php artisan make:model [name] -c
The applications models can be found in
app_route / app / models
View
The view is what the user interacts with, in the case of web application it is the HTML that is served to the client. Views are served to the client by controllers.
The applications views can be found in
app_route / resources / views
Controller
Controllers handle the delegation of tasks like handing off jobs to models to perform, rendering views with data and returning them to the client. Controller logic should be lightweight and should require minimal modification when changes are made, with most of the changes being done inside models.
Controllers can be created using the artisan command
php artisan make:controller --model=[ModelName] [ControllerName]
The CRUD methods that are found in generated controllers represent certain CRUD actions.
index () -> Render a list of resources
show () -> Render a single resource
create () -> Render a view to create a new resource
store () -> Persist the new resource
edit () -> Render a view to edit an existing resource
update () -> Persist the edit to the existing resource
destroy () -> Delete a resource
The applications controllers can be found in
app_route / app / Http / Controllers
Routes
The web server listens for traffic on different 'routes' and maps these routes to actions on a controller.
The task ID that is provided in the route is automatically fetched from the database when a request is sent to the /tasks/{task} route. If the route /tasks/1 receives a request then the task with ID 1 is fetched from the database and is available throughout the update method.
Validation
When a request is received by a controller the data passed in can be validated by functionality provided by Laravel. Multiple validation criteria can be provided to each data point by grouping them into an array, although the following only validates each piece of data against one criterion.
Laravel implements the standard MVC pattern with some extra built in features that make managing the flow of data easier, although certain practices must be adhered to in order for this to work properly. This forces developers to learn these practices however means that moving from project to project is an easier process as because there are common features across projects.



Comments
Post a Comment