Laravel 9 Routing - Laravel 9 Routing Example Tutorial

Laravel 9 Routing – Laravel 9 Routing Example Tutorial

Laravel 9 Routing – Laravel 9 Routing Example Tutorial

In this post we will give you Laravel 9 Routing – Laravel 9 Routing Example Tutorial, hear for Laravel 9 Routing – Laravel 9 Routing Example Tutorial we will give you details about it.

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

What is Laravel 9 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 9.

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 onlinecode.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' => 'onlinecode']);

resources/views/index.php

<h3>his is Simple Route with Call View File Example of onlinecode.org</h3>

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

<h4>his is Simple Route with Controller Method Example of onlinecode.org</h4>

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:

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
 /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    // Edit boot method  
    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));
    });

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

Hope this code and post will helped you for implement Laravel 9 Routing – Laravel 9 Routing Example Tutorial. if you need any help or any feedback give it in the comment section or you have a good idea about this post you can give it a comment section. Your comment will help us to help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs https://onlinecode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

9  +  1  =  

We accept paid advance and paid guest Posting on our Site : Contact US