PHP LARAVEL - Hello World Laravel

As part of a new assignment, I have started working on Laravel. After completing the setup, I am writing my first program on Laravel. I am not different from other programmers, so I am starting with the basic Hello world, and it works as sanity test to make sure that application has been installed correctly.

Let's how we can write Hello World in Laravel.

Through Route

In your Laravel project, go to routes/web.php file and add the following chunk of code:
Route::get('/', function () {
return 'Hello World!';
});
Now open the application in the browser: http://localhost , you can see the Hello World! in the browser.

Through Controller

Now we will create a new controller with the artisan command:
Go to the project root folder through command prompt and execute below the command.

php artisan make:controller HelloWorldController

Now go to your controller folder /app/Http/Controllers, you can see HelloWorldController.php and add the following function
public function helloWorld(){
return 'Hello World from controller!!!';
}
Now to use the controller to display the message, update routes/web.app file.
Route::get('/', 'HelloWorldController@helloWorld');
Instead of the direct function, now we are pointing the controller and the method name for the route '/'

Now run the application in the browser, you will get Hello World from controller!!!

Happy Programming..!!!

Comments

Popular posts from this blog

Method Reference in Java Streams

PHP LARAVEL Directory Structure