Laravel 5 Hello World

Download: Laravel Hello World

Introduction

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. If you did not read the tutorial, use the previous tutorial link above to read it. You will need to create the project

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
  • Passing variables from controllers to views

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

How to use the artisan command line tool

  1. Open the command prompt.
  2. Run the following command to browse to C:/xampp/htdocs/larashop cd C:/xampp/htdocs/larashop

Run the following command to view the list of available commands

php artisan list

You will get a list of all available commands

Composer command prompt

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. 1. Open the command prompt. 2. Run the following command to browse to <C:/xampp/htdocs/larashop cd C:/xampp/htdocs/larashop

Run the following command to generate boiler plate code for Hello controller

php artisan make:controller Hello

HERE,

  • php artisan is used to run the artisan command line tool
  • make:controller Hello specifies the command that the tool should run. This command will create boiler plate code for a controller Hello in /app/Http/Controllers/Hello.php

You will get the following message Controller created successfully.

Open /app/Http/Controllers/Hello.php

You will see the following code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class Hello extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

HERE,

  • namespace App\Http\Controllers; defines the namespace for our controller
  • use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; imports namespaces with the required classes to run the controller
  • class Hello extends Controller defines the class for the controller. The controller extends the base controller
  • public function index(){} defines the default function for the controller
  • public function create(){} defines the function that can be used to create new resources such as new record in a database table
  • public function store(Request $request) defines the function used to store a newly created resource in storage
  • public function show($id) defines the function that retrieves a single resource based on the supplied id.
  • public function edit($id) defines the function used to retrieved a record based on the supplied id for editing
  • public function update(Request $request, $id) defines a function that is used to update a resource in storage
  • public function destroy($id) defines the function used to remove a resource from storage

Basic routing

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

  1. Open routes.php file. The file is located in /app/Http/
  2. Add the following code to routes.php
Route::get('/hello',function(){
    return 'Hello World!';
});

HERE,

  • Route::get('/hello',function(…){…}); responds to the GET method of HTTP for the URI hello. function(…) defines an anonymous function that does the actual work for the requested URI.
  • return 'Hello World!'; returns Hello World! to the requesting browser.

  • Go to the web browser

  • Open the following URL
http://localhost/larashop/public/hello

You will get the following output

Hello World!

Route to controller

For the sake of reference, we will comment the above code as follows

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

Add the following code to /app/Http/routes.php

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

Open /app/Http/Controllers/Hello.php

Add the following code to the index function

public function index()
{
    return 'hello world from controller : )';
}

Save the changes

Open the following URL in your browser.

http://localhost/larashop/public/hello

You will get the following message

hello world from controller : )

Loading a view from a controller

In a typical application, a controller will have to load a view. In this tutorial, we will

  1. Create a view that will be loaded from the controller
  2. Pass a variable to the view and modify
  3. Add a new route to /app/Http/routes.php that will pass in the Hello world view

  4. create a new file hello.blade.php in /resources/views/

  5. Add the following code to /resources/views/hello.blade.php
<!DOCTYPE html>
<html>
    <head>
        <title>Laravel</title>

        <link href="//fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">

        <style>
            html, body {
                height: 100%;
            }

            body {
                margin: 0;
                padding: 0;
                width: 100%;
                display: table;
                font-weight: 100;
                font-family: 'Lato';
            }

            .container {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
            }

            .content {
                text-align: center;
                display: inline-block;
            }

            .title {
                font-size: 96px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">Hello {{$name}}, welcome to Laraland! : )</div>
            </div>
        </div>
    </body>
</html>

HERE,

  • Hello {{$name}}, welcome to Laraland! : ) is the text that is displayed in the browser. {{$name}} prints the value of the name variable. Open /app/Http/routes.php

Add the following code

Route::get('/hello/{name}', 'Hello@show');

HERE,

  • Route::get('/hello/{name}', 'Hello@show'); directs the URI GET request for /hello/value to show() function in Hello controller.

Open app/Http/Controllers/Hello.php

Modify show($id){...} to the following

public function show($name)
{
    return view('hello',array('name' => $name));
}

HERE,

  • public function show($name) defines a show function that accepts a parameter $name
  • return view('hello',array('name' => $name)); loads a view named hello.blade.php and passes a variable name to the view. Note: we did not specific the extensions .blade.php. Laravel takes care of that for us.

Open the following URL

http://localhost/larashop/public/hello/Rodrick

HERE,

  • hello/Rodrick Rodrick is the value of the parameter name that we want to display in the browser. You will get the following results

Composer command prompt

Summary

In this tutorial, we looked at how we can create a basic application that displays hello world in the web browser. We also looked at how we can route to a controller, load a view and pass a parameter to a view and show the value of the parameter in the web browser.

What’s next?

This tutorial is very basic but sets a very good foundation for building Laravel web applications. The next tutorial will Laravel Routing & Contro

Related Tutorials