Tự học Laravel: (3) Tạo Controller

Last time I created a view, but this time I will look at creating a controller in detail.


  • Create a about page.
  • Create a PagesController to organize the display of static pages.
  • About page is displayed by PagesController.


First, configure the routing settings on the about page.

// ...
Route::get('/', 'WelcomeController@index');
Route::get('contact', 'WelcomeController@contact');
Route::get('about', 'PagesController@about');
// ...


Use the artisan command to create a new controller. 
artisan is a utility command included with Laravel. 
You can check what you can do with the list option.

php artisan list

Let’s create a controller.

php artisan make:controller PagesController 

The app/Http/Controllers/PagesController.php file has been created.
I will check the contents.

<?php //app/Http/Controllers/PagesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller

Now, add the about method to PagesController to display the view. 
This time, create a pages directory under the resouces/views directory, and store about.blade.php there.

class PagesController extends Controller {
    public function about() {
        return view('pages.about');

Notice the arguments of the view function. Separated by a period, it is called ‘pages.about’. If there are subdirectories in the resources/views directory, separate them with ‘Subdirectory.View’ and a period.


Create a pages directory under the resouces/views directory and create about.blade.php in a text editor.

└── resources
     └── views
         └── pages
              └── about.blade.php
<!-- about.blade.php -->
    <h1>About Me</h1>


Browser at http://localhost:8000/about access to. 
If “About Me” is displayed on the page, it is successful. 
We were able to display the View via the newly created PagesController.


Please modify the previously created contact view to display from PagesController.


You can do the following things.

  • Create Controller
  • Using the artisan command
  • Store View file in a subdirectory

Leave a Reply

Your email address will not be published.

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