Brijpal Sharma
laravel
Publish on:-February 15th, 2019 ,
Category:- Web Development
Brijpal Sharma
laravel
Publish on:-February 15th, 2019 ,
Category:- Web Development
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.
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.
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.
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.
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
Now we create the route to the received request from view by the user.
<?php
Route::get('/','[email protected]');
Route::get('/about','[email protected]');
Route::get('/contact','[email protected]');
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.
<?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.
Create two more view
<!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>
<!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 //localhost:8000/about
Visit contact page //localhost:8000/contact
Here we completed Laravel 5.7 Route Tutorial Step by Step for Beginners.
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.
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...
You must be logged in to post a comment.