Laravel 5.7 Route Tutorial Step by Step for Beginners

Generic placeholder image
Brijpal Sharma
laravel  

Date:-February 15th, 2019 , Posted On:- Web Development


Today the topic is Laravel 5.7 route tutorial step by step for beginners. I will show you step by step from scratch.

For the basic developer and beginners for all route, the topic is very important. In this post, I use Laravel 5.7 version but you can use any version of Laravel process will be the same.

Basically, the route is used for a redirect for one page to other page suppose my I want to redirect About page from the home page this is possible by route. so let's start.

Step:1 Create a new Laravel Application 

Create a new Larael application using this following command.

laravel new route

 

After successfully creating a Laravel application first we need to understand the structure of Laravel route. we work only three folders.

  1. Controller.
  2. View.
  3. Route.

Route your Application using this following command.

php artisan serve

Update welcome.blade.php with this code.

welcome.blade.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="">Home </a>
    <a href="">About </a>
    <a href="">Contact </a>
</body>
</html>

 

Reload page and result will be.

Laravel 5.7 Route Tutorial Step by Step for Beginners

 

Step: 2 Submit user Request

Now, the user submits a request from view page (welcome page) using this link home, about and contact.  but before it, we need to add a href link.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="/">Home </a>
    <a href="/about">About </a>
    <a href="/contact">Contact </a>
    <hr>
    <h2>This is home page</h2>
</body>
</html>

Now when the user clicks on the about button an about request goes to Laravel route and check for the assigned controller or request.

Step: 3 Create a Controller.

Create a new controller PageController to show the requested page by Laravel Route.

To generate Controller by CLI use this following command.

php artisan make:controller PageController

 

Step:4 Create some route for the button.

Now we create the route to the received request from view by the user.

<?php

Route::get('/','PageController@home');
Route::get('/about','PageController@about');
Route::get('/contact','PageController@contact');

Ok, let's understand how route work.

We have three routes for three pages. when the user request for Slash( "/") ( that's mean for home page ) Laravel route refer to the controller "PageController" and for slash request Method is home (after @ is method name) which is defined into PageController.

Step:5 Create method

PageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PageController extends Controller
{
    public function home()
    {
    	return view('welcome');
    } 

    public function about()
    {
    	return view('about');
    } 

    public function contact()
    {
    	return view('contact');
    }

}

Now, the controller completed.

Step:5 Create the view pages

Create two more view 

  1. About Page
  2. Contact page

about.blade.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="/">Home </a>
    <a href="/about">About </a>
    <a href="/contact">Contact </a>
    <hr>
    <h2>This is About page</h2>
</body>
</html>

 

contact.blade.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="/">Home </a>
    <a href="/about">About </a>
    <a href="/contact">Contact </a>
    <hr>
    <h2>This is Contact page</h2>
</body>
</html>

Visit About page http://localhost:8000/about

Laravel 5.7 Route Tutorial Step by Step for Beginners

Visit contact page http://localhost:8000/contact

Laravel 5.7 Route Tutorial Step by Step for Beginners

Here we completed Laravel 5.7 Route Tutorial Step by Step for Beginners.

Conclusion

We learn Laravel 5.7 route step by step. we install a new Laravel  5.7 application and create some view, route, and controller. In this tutorial, we create three links home, about and contact.

 


About the author

user profile
Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...

0 comments

You must be logged in to post a comment.