Create a New Page in Laravel

After installing and configuring Laravel, one of the first steps in order to understand how the framework works is to create a custom static page with its own route and render a view file with some content. To create a new page in Laravel, first look over parts of the directory structure.

As mentioned in the Getting Started with Laravel post, the config/app.php provides the application’s environment variables.

A routes directory is used to define the routes configuration, and the web.php file within this directory is used to define routes that are used for the web interface.

Within this file, a route has already been defined that loads the Laravel Welcome page via the welcome view.

// routes/web.php

<?php

Route::get('/', function () {
    return view('welcome');
});

To define a custom route, simply copy the syntax from the code above, and change the first parameter of the get() method to match the route you would like to add.

// routes/web.php

<?php

Route::get('/', function () {
    return view('welcome');
});

// Here is your custom route
Route::get('/hello-world', function () {
    return view('helloworld');
});

With each route defined, a view is rendered using the view() function. The first argument passed into this global helper should correspond to the name of a view file within the resources/views directory of the application.

You can also pass in a second argument, which is an array of data that should be made available to the view.

Laravel uses Blade, which is a template engine used to render content. Blade adds a .blade.php extension to the view files, so the file name for the helloworld view should be named helloworld.blade.php.

The “Dot” notation can also be used to reference nested views. So if the template file was located in the following directory:

resources/views/custom/helloworld.blade.php

The first argument passed in the view() function within the web.php routes file should be custom.helloworld.

For simplicity, the view file in this example is populated with some basic HTML.

// resources/views/helloworld.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel - Hello World</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

If you have added the route and the view template correctly, navigate to the route in the browser and you should notice that your view has successfully been rendered.

Create a New Page in Laravel

Note: This article is based on Laravel version 5.4.