Skip to main content

Laravel Routing Tutorial | Laravel Routing Example

Laravel 8 Routing Example

Hi dev,

In this example,I will describe you step by step laravel 8 routing tutorial. I will learn how to create new route in laravel 8. you can easily create post route in laravel 8 application. i will also show how to create route in laravel 8 controller.

I will step by step process of how to create first route in laravel and understanding of laravel routing with brief.

What is Laravel Routing?

Using Routing you can create a request URL for your application. you can design set of HTTP request like POST Request, GET Request, PUT Request and DELETE Request using routing in laravel 8.

You can easily create route in web.php file inside a routes folder. in web.php file you can create your route list there are several ways. i will show you bellow step by step how you can create it and how it works, so let's see step by step explanation.

Create Simple Route:

Now, I will create very simple route and will let you know how you can access it. so let's create simple route using following line adding in route file:

routes/web.php

Route::get('simple-route', function () {
return 'This is Simple Route Example of itwebtuts.com';
});
Access URL:

http://localhost:8000/simple-route
Route with Call View File:

You can create route with directly call view blade file from route directly. You can see bellow created route example:

you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:

routes/web.php

Route::view('/blog', 'index');

Route::view('/blog', 'index', ['name' => 'itwebtuts']);
resources/views/index.php

<h1>his is Simple Route with Call View File Example of itwebtuts.com</h1>
Access URL:

http://localhost:8000/blog
Route with Controller Method:

Here, you can create route with call controller method so, you can simply create controller method and call that method with your route as like bellow:

routes/web.php

use App\Http\Controllers\PostController;
// PostController
Route::get('/post', [PostController::class, 'index']);
app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

class PostController extends Controller
{

/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('index');
}
}
resources/views/index.php

<h1>his is Simple Route with Controller Method Example of itwebtuts.com</h1>
Access URL:

http://localhost:8000/post
Create Route with Parameter:

Here, we will create simple route with passing parameters. you can can create dynamic route with your controller. so let's create as bellow

routes/web.php

use App\Http\Controllers\PostController;
// PostController
Route::get('/post/{id}', [PostController::class, 'show']);
app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

class PostController extends Controller
{

/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function show($id)
{
return 'Post ID:'. $id;
}
}
Access URL:

http://localhost:8000/post/15
Create Route Methods:

You can create get, post, delete, put, patch methods route as bellow i created. you can understand how it works.

So, let's see bellow route examples:


<?php
use App\Http\Controllers\UserController;
// UserController
Route::get('users', '[UserController::class, 'index']');
Route::post('users', '[UserController::class, 'post']');
Route::put('users/{id}', '[UserController::class, 'update']');
Route::delete('users/{id}', '[UserController::class, 'delete']');
You can also check how many routes i created using following command:

php artisan route:list

You can also create resource routes as i written tutorial on it. You can see here: Create Resource Routes in Laravel.

I will help you...

Comments

Popular posts from this blog

Laravel Scout Algolia Full Text Search Example

Hi Guys Today,I will tell how you can full text search utilizing scout algolia. i will show the example of laravel scout algolia full text search.you can full text search utilizing scout algolia api.it can this example full expound scout algolia full text search. I will show the rudimental step of scout algolia api full text search.if you can utilize full text search for install scout and Algolia api package.we are utilizing algolia api utilizing full text search example in laravel. Here the following steps example laravel full text search Using scout algolia Step 1: Create Laravel Project In this step create laravel project following command. composer create-project --prefer-dist laravel/blog Step 2: Database Configuration After create laravel project , we require to make database configuration, you have to add following details on your .env file. 1.Database Username 1.Database Password 1.Database Name In .env file also available host and port details, you can configu

Laravel IP Address Using Get Location Example

Hi Dev, Today,I will learn you how to get location useing ip address in laravel. we will show example of laravel ip address using get location. you can easy to get location useing ip address in laravel.In this example, I will useing stevebauman/location packege get location useing ip address in laravel. Many time you will need to get visitor's details for security, spam prevention etc. It's very easy to get visitor ip address and their location in PHP Laravel. Step 1: Install stevebauman/location Now, We will install stevebauman/location package using below command.Require this package with composer. It is recommended to only require the package for development. composer require stevebauman/location Step 2: Add providers and aliases In this step,We will add below providers and aliases in the "config/app.php" file. config/app.php 'providers' => [ .... Stevebauman\Location\LocationServiceProvider::class, ], 'aliases' => [ .... 'Loca

Laravel 6 validation required if another field is empty

Hii guys, In this artical, i will give you example of laravel 6 in validation required if another field is empty. We know laravel provide several in-built laravel 6 validation required_without . If you need to add validation rules like required if other field is empty in laravel then you can do it using required_without. I am going to explain you, If you can not enter test (field) value at that time test1 (field) is required. So at that time you can add validation required_without. So, you can use this way: "test1" =>"required_without:test" Example: public function store(Request $request) { $request->validate([ "test" =>"required", "test1" =>"required_without:test" ]); dd("Done!"); } If return validation error otherwise show Done!. It will help you...