Laravel Installation Guide Hello World Example With Views and Controller


Assuming we have a working laravel application running in, say, "",we want our application to show a "Hello World" message when we hit the URL . It involves the creation of two files (the view and the controller) and the modification of an existing file, the router.

The view

First off , we open a new blade view file named helloview.blade.php with the "Hello World" string. Create it in the directory app/resources/views

<h1>Hello, World</h1>

The controller

Now we create a controller that will manage the display of that view with the "Hello World" string. We'll use artisan in the command line.

$> cd your_laravel_project_root_directory
$> php artisan make:controller HelloController 

That will just create a file (app/Http/Controllers/HelloController.php) containing the class that is our new controller HelloController.

Edit that new file and write a new method hello that will display the view we created before.

public function hello()
    return view('helloview'); 

That 'helloview' argument in the view function is just the name of the view file without the trailing ".blade.php". Laravel will know how to find it.

Now when we call the method hello of the controller HelloController it will display the message. But how do we link that to a call to ? With the final step, the routing.

The router

Open the existing file app/routes/web.php (in older laravel versions app/Http/routes.php) and add this line:

Route::get('/helloworld', 'HelloController@hello');

which is a very self-explaining command saying to our laravel app: "When someone uses the GET verb to access '/helloworld' in this laravel app, return the results of calling the function hello in the HelloController controller.