Tự học Laravel: (1) Luồng hiển thị

Lavarl is a framework of the MVC model , and the process flow is basically as shown above. In this article, I will simply follow the User->Routing->Controller->View part, excluding the Model and Database parts in the above figure.


First, create a new project. Name the project blog.

laravel new blog
cd blog
php artisan serve

Start the project on PHP’s built-in server. Go to http://localhost:8000 in your browser and check that the “Laravel” logo is displayed.


First, let’s look at the directory structure. There are many directories and files, but just keep a quick glance. As you use Laravel, you will find it gradually. Let’s move forward one step at a time. 
First of all, we will introduce only relevant directories in this article.

├── app
│   └── Http
│       └── Controllers
├── resources
│   └── views
└── routes


First, let’s look at routes/web.php which describes Routeing configuration.

<?php // routes/web.php

Route::get('/', function () {
    return view('welcome');

I use a closure (an anonymous function) in the action part. 
In the closure, we support displaying the welcome view (as a response). 
The role of Routing is to execute the action according to the combination of HTTP method and path.


Next, take a look at the ‘welcome’ view above.
View files are stored in app/resources/views.

<!-- resouces/views/welcome.blade.php -->
<!doctype html>
<html lang="{{ app()->getLocale() }}">
        <div class="flex-center position-ref full-height">
            <div class="content">
                <div class="title m-b-md">

This is exactly what you see when you visit http://localhost:8000/

Laravel uses a template engine called blade to generate HTML from view files (HTML templates). Therefore, the file extension is .blade.php. In this example, it is welcome.blade.php.

The role of View is to transform data into a form that is easy for the user to see. It is responsible for the output of the User Interface.


Earlier, in Routing, we used closures for actions. 
Let’s create a simple controller and replace it with a closure.

Create a controller using the artisan command.

php artisan make:controller WelcomeController 

Since an empty WelcomeController.php is generated, add the index() method as follows.

<?php //app/Http/Controllers/WelcomeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class WelcomeController extends Controller
    public function index()
        return view('welcome');

The index method executes the view function and returns its return value. The return value of the view function is the response to the browser.

The view function is a helper function that creates HTML from the view file. Here we are using the welcome view to instruct you to create HTML.

The controller receives input data from the user, requests the Model to process it, and requests the View to display the result of execution. This example is quite simple, does not pass anything to View, but just returns HTML.

Change the Routing configuration action from closure to controller.

<?php // routes/web.php
//Route::get('/', function () {
//    return view('welcome');
Route::get('/', 'WelcomeController@index');
index method of WelcomeController class

The action part can be called a controller class method by writing “Controller-class-name@method-name”.

Access http://localhost:8000 with your browser and check the operation. 
It is successful if the “Laravel” logo is displayed.


I understand the following.

  • A series of flow of Routing->Controller->View
  • Routing, Controller, View directory file configuration
  • Routing setting
  • Calling a View from a Routing Configuration Closure
  • Call of View from Controller

I looked at Laravel’s Routing, Controller, and View at high speed. 
The content was simple, but I learned the basic flow of the Web application framework. 
The basics of everything are important.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.