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 Validation Image Example

Hi Guys, Today, I will learn you to create validation image in laravel.we will show example of laravel validation image.The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp). Here, I will give you full example for simply image validation in laravel bellow. solution $request->validate([ 'file' => 'image' ]); Route : routes/web.php Route::get('form/create','FromController@index'); Route::post('form/store','FromController@store')->name('form.store'); Controller : app/Http/Controllers/BlogController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Blade; use App\Models\User; use App\Models\Post; class FromController extends Controller { /** * Write code on Method * * @return response() */ public function create() { return view('form'); } /** * Write code on Method ...

Laravel Get Headers From Request Example

Hi Dev, In this blog ,I Will learn you how to get header in laravel. I will show example of laravel get headers from request you can easliy laravel get all headers from request. I will explain laravel get all headers. I can blow you can easily get headers from request in laravel this example. Exmaple:1 Here In this exmaple laravel Get headers from request to a header method /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index(Request $request) { $headers = $request->header('connection'); dd($headers); } Output : "keep-alive" Exmaple:2 Here In this exmaple laravel get headers from request using a request builder to a header method /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { $headers = \Request::header(); dd($headers); } Output : array:13 [ "host" => array:1 [ 0 => "localho...

PHP Upload File Using Ckeditor Example - Itwebtuts

Hii Developer, In this example,I am going to show you How to do upload custom file with CKEDITOR in PHP application. In this tutorial i explain step by step example code of How to image upload with CKEDITOR php. You can see simple example of php ckeditor custom image upload using browse button. Here i give you full example of How to How to do custom file upload with in CKEDITOR step by step like create one file in this file we are integrate CKEDITO and secound another file which we are created for uploadin custom file. Following The Step: 1)create one index.php file 2)create cstom file uploading file Step:1 Create one index.php file Now we have download CKEDITOR in our local PC. then we are create our index.php file and this file we are use CKEDITOR <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="robots" content="noindex, nofollow"> <title>Ckeditor File Upload</title> ...