Default, Laravel

How to create the first page in Laravel 10?

This tutorial will teach us how to create your first Laravel static page.

Laravel route point incoming request to the specific controller method. When users access a particular page in their browser router knows which controller method can handle the requested URL.

Therefore, let us create our first route. We want to create a homepage for our new website using the Laravel framework. We have to answer the following questions first:

  • What would be the route URL and name?
  • What controller can handle my route request?
  • What controller method can handle my route request?
  • What would be the name of the view that handles my HTML code?

Let’s say that when the user hits http://localhost, which by default points to /. Slash (/) always refers to the root of the website. In our case, the website root will be our homepage.

We would create a new route in Laravel that points to /. We would name our controller HomeController, which we have not yet created.

We are just assuming that our HomeController@index method will handle our new route. In this tutorial, I believe you installed the Laravel framework locally.

How to create a new route in Laravel?

All Laravel routes are defined in the routes/web.php file. Open this file in your favorite editor, and let’s add the following line of code:

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::view('/', 'homepage')->name('homepage');

We have added the Route::view method, which takes two parameters: the route and the name of the view that handles this request.

We do not need a controller now because our page will be static. Therefore, our route can directly point to view instead of going to the controller and then pointing to view.

Later, we will define the controller in this tutorial and modify the above-defined route. Now, if you go to your browser and hit http://localhost, you will get the following error:

We got the above error because our route now points to the homepage view. However, we have not yet created our view file.

Let’s create a view file in resources/views/homepage.blade.php with the following line of code:

<h1>This is my first laravel page</h1>

Now, we created a new view file. You might be wondering what this blade.php extension of the file is. Well, Laravel uses a blade template engine. Therefore, all view files must have this blade.php extension at the end.

Now that our view exists, we will again hit http://localhost in our browser, and this time we will see the following output.

I can see the happiness on your face because you created your first page. However, the tutorial is not over yet. As I promised now, we will learn about creating a controller.

How to define a controller in Laravel 10?

Previously, our route was pointing directly to our view. However, not in all cases do you want to do that. A controller can be used for any purpose:

  • controller can talk to the model and get some data;
  • controller may interact with some service that runs business logic;
  • controller can pass dynamic data to your view file

Therefore, any dynamic pages you want to create should have a controller. Let’s see how we can create a controller. Go to your project root directory and run the following command in your terminal:

php artisan make:controller HomeController

Above command will create a new controller file in app/Http/Controllers/HomeController.php. Open this file, and let us create a new method.

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\View\Factory;

class HomeController extends Controller
{
    public function index(Request $request): Factory|View|Application
    {
        return view('homepage');
    }
}

Alright, we have added an index function in our HomeController class. The first argument is always Request object there is a reason why:

  • You can access query params
  • You can access post/put params
  • You can fetch cookies or user details
  • You can access different headers etc.

Request object gives you all the necessary data that every web request contains. For now, do not worry about this. Later on, we will learn in-depth about Request and Response objects.

Our function calls the view method by passing a string homepage in it. The view function calls the view we defined earlier.

Alright, now that we have defined our controller class and index method inside. We would change our route to point to this new controller method instead then pointing directly to the view file.

Open the routes/web.php file and modify our route to point to our new controller method.

<?php

use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', [HomeController::class, 'index'])->name('homepage');

The above code Route:get function defines a GET request for route / that points to the HomeController@index method. Open your browser and hit http://localhost to see if it is pointing correctly to our new controller.

How to pass parameters to Laravel view?

You might wonder how I can pass additional data to my Laravel view. Open your controller HomeController class, and let’s pass a variable called message that we will use in our view file.

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\View\Factory;

class HomeController extends Controller
{
    public function index(Request $request): Factory|View|Application
    {
        return view('homepage', [
            'message' => 'This is my first Laravel page',
        ]);
    }
}

You can see in the above code that your view function takes the name of the view as the first argument, and the second argument is an array.

You can pass an array of key/value pair to your view file. These passed array keys will be converted to a PHP variable, holding the value you attached to your key.

In the above example, in your view, you can use the $message variable that points to the “This is my first Laravel page” string. Let’s open our homepage.blade.php file and replace the hardcoded line with the following variable.

<h1>{{ $message }}</h1>

Open your browser and hit http://localhost to see if we can see the following output.

That is it for this tutorial. In the following tutorial, we will learn more in-depth about Laravel routes.

What did we learn today?
Today we have learned the following things:

  • How to create a Laravel route
  • How to create a Laravel controller
  • What is the purpose of the controller
  • How can we create a path that points to either view or controller directly
  • What is the template engine Laravel use for a parsing HTML file