Laravel Installation Hello World Example (Using Controller and View)


Example

  1. Create a Laravel application:

    $ composer create-project laravel/laravel hello-world
    
  2. Navigate to the project folder, e.g.

    $ cd C:\xampp\htdocs\hello-world
    
  3. Create a controller:

    $ php artisan make:controller HelloController --resource
    

This will create the file app/Http/Controllers/HelloController.php. The --resource option will generate CRUD methods for the controller, e.g. index, create, show, update.

  1. Register a route to HelloController's index method. Add this line to app/Http/routes.php (version 5.0 to 5.2) or routes/web.php (version 5.3):
    Route::get('hello', 'HelloController@index');

To see your newly added routes, you can run $ php artisan route:list

  1. Create a Blade template in the views directory:

    resources/views/hello.blade.php:

    <h1>Hello world!</h1>
    
  2. Now we tell index method to display the hello.blade.php template:

    app/Http/Controllers/HelloController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use App\Http\Requests;
    
    class HelloController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            return view('hello');
        }
    
        // ... other resources are listed below the index one above

You can serve your app using the following PHP Artisan Command: php artisan serve; it will show you the address at which you can access your application (usually at http://localhost:8000 by default).

Alternatively, you may head over directly to the appropriate location in your browser; in case you are using a server like XAMPP (either: http://localhost/hello-world/public/hello should you have installed your Laravel instance, hello-world, directly in your xampp/htdocs directory as in: having executed the step 1 of this Hello Word from your command line interface, pointing at your xampp/htdocs directory).