Learning Management Platform for Written Tutorial Series.

Laravel 6 Hello World

Laravel 6 Hello World

Hello world is a traditional simple computer program that beginners use to understand the core concepts of a programming language or framework. It doesn't do much but print the words hello word. The bits and parts that make up the program are then explained to the neophyte.

In the previous tutorial, we installed and configured a Laravel application. We will build on the same project to create a simple Hello World application and look at the key components of a Laravel application.

Topics to be covered

  • Artisan command line tool
  • Artisan command to generate boiler plate code for a controller
  • Basic routing
  • Call a controller method from a route
  • Loading a view from a controller
  • Passing variables from controllers to views
  • Artisan maintenance mode commands

Artisan command line tool

Artisan is a command line tool that automates common tasks in Laravel. The tool is run from the command line. The artisan command line tool can be used to perform the following tasks and many more

  • Generate boilerplate code – easily create controllers, models etc.
  • Database migrations – migrations are used to manipulate database objects. Migrations can be used to create and drop tables etc.
  • Database seeding – seeding is a term used to add dummy records to the database.
  • Routing
  • Application configuration
  • Run unit tests
  • Set application name space
  • Publish package resources

How to use the artisan command line tool

We will work with the command line in this section.

Open the command line and browser to the root of the project that you created in the previous lesson.

cd ~/Dev/larashop

Run the following command to view the list of available commands

php artisan list

You will get a list of all available commands

Laravel Artisan

In the above results, we get the version of Laravel which is version 5.8.* then a list of available commands and their descriptions.

Artisan command to generate boiler plate code for a controller

In this section, we are going to create a simple controller that will hand the HTTP request and load the hello world view.

Run the following command to generate a controller

php artisan make:controller HelloController

HERE,

  • php artisan make:controller HelloController uses the make command to generate boiler plate code for a controller named HelloController.

Executing the above command produces the following results

Controller created successfully.

Open the file /app/Http/Controllers/HelloController.php

You will see the following code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    //
}

HERE,

  • <?php is the standard PHP opening tag
  • namespace App\Http\Controllers; defines the controllers namespace
  • use Illuminate\Http\Request; imports the Request object
  • class HelloController extends Controller{...} defines a class HelloController that extends the parent class Controller. The parent class Controller is part of the Laravel framework.

Let's now define a function called index in our HelloController like so

public function index(Request $request){

HERE,

  • public function index(Request $request){...} defines a public function index that accepts a parameter $request which is of type Request. The type Request is part of the Laravel framework.
  • return 'Hello World'; returns the words Hello World

Basic routing

In this section, we will create a new route that will display hello world in the web browser

Route anonymous functions

Open /routes/web.php file. The routes directory is in the root of the application.

Add the following code to web.php

Route::get('/hello',function(){
    return 'Hello World!';
});

HERE,

  • Route::get('/hello',function(){...} defines a route called hello that can be accessed via the web browser
  • return 'Hello World!'; returns the words Hello World

Let's now start the built-in server that comes with Laravel using artisan like so

php artisan serve

HERE,

  • php artisan serve calls the serve command of artisan to start the built-in PHP server. By default, the built-in command runs on port 8000

Executing the above command produces the following results

Laravel development server started: <http://127.0.0.1:8000>

Let's now load the following URL in our browser

http://localhost:8000/hello

The above URL produces the following result

Hello World!

Call a controller method from a route

Laravel allows us to map routes to controller methods. In this section, we will learn how to do that.

Route to controller routing

In the above section, we used an anonymous function that return the results to the user. In this section, we will call a function that is in a controller to return the results.

Modify our route defined above like so

Route::get('hello', 'HelloController@index');

HERE,

  • Route::get('hello', 'HelloController@index'); maps the URI hello to the method index that is defined in the controller HelloController.

Load the following URL in your browser

http://localhost:8000/hello

You will get the following results

Hello World

Loading a view from a controller

Our above example returned a string directly from a controller. In the real world, results are usually returned from a view. Let's now do that in this section.

Create a new file hello.blade.php in the directory /resources/views.

Open /resources/views/hello.blade.php file

Add the following code

Hello World!!!

Let's now modify the index method in our controller HelloController like so

public function index(Request $request){
	return view('hello');
}

HERE,

  • return view('hello') calls the helper method view that accepts hello as an argument. The argument is the name of the view that we want to load. Notice how we skipped the *.blade.php extension and the view location. Laravel handles that for us in the background.

Reloading our URL produces the following result.

Hello World!!!

Passing variables from controllers to views

Would it be nice if we actually greeted a real person instead of the world? I think so. In this example, we will modify our application to pass in a name variable and greet the user.

Modify the route that we defined in /routes/web.php like so

Route::get('hello/{name}', 'HelloController@index');

HERE,

  • Route::get('hello/{name}', 'HelloController@index'); adds the variable name as specified by the curly brackets to our route. This allows us to pass in any value where we have {name}

Let's now modify our controller index method in /app/Http/Controllers/HelloController.php to accept a parameter called name like so.

    public function index(Request $request,$name){
        $params = [
            'name' => $name
        ];

        return view('hello')->with($params);
    }

HERE,

  • public function index(Request $request,$name){} our function definition accepts a parameter name
  • $params = ['name' => $name] defines an associative array with a key of name that is assigned the value of the variable $name.
  • return view('hello')->with($params); calls the with method on our view helpers and passes in the variable params as the argument.

Finally we modify our view /resources/views/hello.blade.php to print our the value of our name variable like so.

Hello {{$name}}

HERE,

  • {{$name}} double curly brackets are used to print out the value of the $name variable. We will talk more about blade in the coming lessons.

Load the following URL into your web browser

http://localhost:8000/hello/rodrick

You should be able to see the following results.

Hello rodrick

Summary

Laravel is an MVC framework that executes programs as follows. First a user makes a request which is received by the route. The route then maps the request to a controller method. The controller method communicates with a model if necessary then loads the appropriate view which sends the feedback to the user. Yes, its that simple.

What next?

In the next tutorial, we will introduce you to variables and learn what role they play in python applications. If you enjoyed this lesson then show us your appreciation by creating a free accounts.


...