Laravel 7.x — P4 : Return View (routes)

Laravel 7.x — P4 : Return View (routes)

We looked at how to return some basic HTML in the previous article, but we want to move out of the route file; that’s not what the route file should do. The route file should direct you to the controller that in turn returns the view.

We looked at how to return some basic HTML in the previous article, but we want to move out of the route file; that’s not what the route file should do. The route _file should direct you to the _controller _that in turn returns the _view. For simple static pages, it’s fine to return the view directly from the route _and there are a few ways to achieve that. First thing though, we have to create a basic _view. We’re going to be looking at views _extensively in later articles, but this is a basic introduction that’s more focused on the _route_than the _view itself.

Under the _resources/views _directory, create a file and name it _about.blade.php. _Don’t overthink it: right-click -> New File.

Image for post

Copy the content out of the _index.blade.php _file and paste it in there. Modify it to match the code below.

<!DOCTYPE html>
    <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">

            <title>About Page</title>

        </head>
        <body>
            <h1>About</h1>
            <div>
                Some about page content.
            </div>
        </body>
    </html>

To return the about.blade.php _view to the user, we need to create a _route. We’ve already seen one way to create a _route _when we returned our _index.blade.php _file. Create a new _route _in _routes/web.php _and add the code below.

<?php

    Route::get('about-us', function() {
        return view('about');
    });

When the user visits the about-us _page (_127.0.0.1:8000/about-us), Laravel will return the _about.blade.php _view with the help of the _view() _helper.

Image for post

You can also return a view with the Route::view() _syntax. It’s incredibly common for Laravel to return a view that special syntax was developed to deal with that concept. Let’s create another page called _company. _Under _resources/views, create _company.blade.php _and add the code below (similar to About).

<!DOCTYPE html>
    <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">

            <title>Company</title>

        </head>
        <body>
            <h1>Company</h1>
            <div>
                Company Details
            </div>
        </body>
    </html>

php programming software-development web-development laravel

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...