MVC and routes
As mentioned earlier, we will now create a component each of the model, view, and controller, using the Artisan CLI. However, as our heading suggests, we will include another important item: the routes. We have already mentioned them in this chapter (in our diagram of the request life cycle in Laravel, and also in the example diagram of the MVC itself).
In this section, we will focus on creating the file, and checking it after it has been created.
Creating models
Let's get hands on:
- Open your Terminal window inside the
chapter-01
folder, and type the following command:
php artisan make:model Band
After the command, you should see a success message in green, stating: Model created successfully
.
- Go back to your code editor; inside the
app
folder, you will see theBand.php
file, with the following code:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Band extends Model { // }
Creating controllers
Now it is time to use the artisan to generate our controller, let's see how we can do that:
- Go back to the Terminal window, and type the following command:
php artisan make:controller BandController
After the command, you should see a message in green, stating: Controller created successfully
.
- Now, inside
app/Http/Controllers
, you will seeBandController.php
, with the following content:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class BandController extends Controller { // }
Note
As a good practice, always create your controller with the suffix <Somename>Controller
.
Creating views
As we can see earlier when using the php artisan list
command, we do not have any alias command to create the application views automatically. So we need to create the views manually:
- Go back to your text editor, and inside the
resources/views
folder, create a new file, namedband.blade.php
. - Place the following code inside the
band.blade.php
file:
<div class="container"> <div class="content"> <div class="title">Hi i'm a view</div> </div> </div>
Creating routes
The routes within Laravel are responsible for directing all HTTP traffic coming from the user's requests, so the routes are responsible for the entire inflow in a Laravel application, as we saw in the preceding diagrams.
In this section, we will briefly look at the types of routes available in Laravel, and how to create a simple route for our MVC component.
At this point, it is only necessary to look at how the routes work. Later in the book, we will get deeper into application routing.
So, let's look at what we can use to handle routes in Laravel:
Code | HTTP | METHOD |Verb |
| GET |
| POST |
| PUT |
| PATCH |
| DELETE |
| OPTIONS |
Each of the routes available is responsible for handling one type of HTTP request method. Also, we can combine more than one method in the same route, as in the following code. Do not be too concerned with this now; we'll see how to deal with this type of routing later in the book:
Route::match(['get', 'post'], '/', function () { // });
Now, let's create our first route:
- On your text editor, open
web.php
inside theroutes
folder, and add the following code, right after thewelcome view
:
Route::get('/band', function () { return view('band'); });
- Open your browser to
http://localhost:8081/band
, and you will see the following message:
Hi i'm a view
Note
Don't forget to start all Docker containers using the docker-compose up -d
command. If you followed the previous examples, you will already have everything up and running.
Bravo! We have created our first route. It is a simple example, but we have all of the things in place and working well. In the next section, we'll look at how to integrate a model with a controller and render the view.