Laravel

Create Hello World in Laravel

January 30, 2018

author:

Create Hello World in Laravel

In this section, we will try and code a simple Laravel program to understand nitch-glitch of this framework. As in any other language, probably the easiest and starting step is to create a Hello World! Application. We will do the same here.

If you have followed the installation steps, your Laravel project should be ready to run.
Verify running following command from your project directory:

php artisan serve

And, goto the following link in a browser of your choice:

localhost:8000

If you see something like this on your screen, we are ready to start. (Debug the issue or comment below if you don’t see Laravel Welcome page in your browser)

Laravel Default Homepage

# Laravel 5.6 Hello World Application

We will run ‘Hello World!’ in Laravel in three different ways:
• From Route (web.php)
• From Controller (helloWorldController.php)
• From View (helloWorld.blade.php)

Let us code each of them one by one. Starting with the easiest way, let us run from a route.

# Hello World From Route

In your fresh Laravel project, goto routes/web.php file and replace the file code with following chunk of code:

// routes/web.php
...
Route::get('/', function () {
    return 'hello world!';
});

You can test this, open the following link in the browser:

localhost:8000/

Do you see something like this?
Hello World in Laravel Route

# Hello World From Controller

Now, we will create a new controller with php artisan command:
php artisan make:controller HelloWorldController

Now goto app/Http/Controllers/HelloWorldController.php and insert following function:

# app/Http/Controllers/HelloWorldController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloWorldController extends Controller
{
    public function hello(){
        return 'hello world!';       
    }
}

To render data from the controller, let us integrate it with our helloWorld route in the web.php that we created earlier.

# routes/web.php
...
Route::get('/', 'HelloWorldController@hello');

Now run this application in the same way as we did before. You’ll see no change in the output, but this time we have used a different way to render text on the screen.

# Hello World In View

This is the most preferred way to render data in a web application with Laravel. Rendering data from view helps us utilize the actual MVC structure of Laravel viz. keeping the business logic away from the user interface. Laravel uses special environment for this – the blade templating. It can render both HTML and PHP in the same file. We will have a deep look into blading in coming tutorials.

So the first step is to create a new blade file in resources/views.
Create a new file helloWorld.blade.php manually and use the following code in it.

The next step is to integrate our view (blade file) with the controller. So make following changes to HelloWorldController.php

# app/Http/Controllers/HelloWorldController.php
...
public function hello(){
    return view('helloWorld');       
}

This goes without saying – run the application and check browser for updates.
Hello World from Laravel View

Conclusion
In this segment, we took a sneak-look at Laravel’s working architecture and learned different ways to render data. Hope you enjoyed it. In future tutorials, we will dive deep down in each of this sections individually. Till then, play around with what we have learned until now and try different use cases.

Leave a comment

Your email address will not be published. Required fields are marked *