Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
laravel routing

Laravel 8 routing with example

Aman Jain, October 5, 2021January 13, 2022

In this post we will learn to implement the Laravel 8 routing and how it’s different from Laravel other versions. Laravel routing is used to create the URL for each POST, GET requests and it works as a bridge between the controller or html view.

In this article we will also cover to use the middleware and groups in routes. In Laravel there is 4 default routes files.

  1. web.php
  2. api.php
  3. console.php
  4. channel.php

web.php is used to create the request URL for web application. api.php is used for to create the routes for rest api. we will start the Laravel routing tutorial by step by step guide.

Create the routes

In this step we will create a simple routes with a callback to return the response..

<?php

use Illuminate\Support\Facades\Route;

Route::get('/simple-view', function () {
     
    //logic of your route
    return "this is a simple response with text";
});

You can access the route using

http://domain.com/simple-view

Router methods

Laravel have 8 routes methods available to register the routes.

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

// match for allow multiple request method
Route::match(['get', 'post'], '/', function () {
    //
});
// any for any type of request method
Route::any('/', function () { 
    //
});

Call view using the route

We can also call directly the view file using the Laravel 8 route.

Route::view('/view-file', 'index');

//or

Route::get("/view-file",function(){
   return view("index");
}) 

and view file

<p>this is a view file</p>

You can access the route using

http://domain.com/view-file

Creating redirect routes in Laravel 8

In Laravel 8 we can create redirect routes in route file

Route::redirect('/path-here', '/to-path', 301);

Creating Named Routes in larevel 8

Route::get('/home', function(){

})->name("home");

Calling controller method in Laravel routes

<?php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;


Route::get('/create-post',[PostController::class, 'create']);

Parameterized routes

we can pass the parameter to url and can access in our controller

<?php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;

Route::get('/edit-post/{id}',[PostController::class, 'editPost']);

and th controller file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{

     /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function editPost(Request $request,$id)
    {

        return view('post.edit',["id"=>$id]);
    }  
}

Create Namespace and group routing in Laravel 8

By using the namespace and group we can apply multiple rules to multiple routes at once check the below code

Route::namespace('\App\Http\Controllers\Api')->group(function(){

    Route::get("app/test",'PostController@create');
});

Now, create a folder Api in App\Http\Controllers and paste the below code

<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class PostController extends Controller
{

     /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return "create view";
    }
}
beenhere

Also Read :

Laravel 8 middleware and usage

Related

Php Laravel

Post navigation

Previous post
Next post

Related Posts

Php get last insert id in laravel

How to get last insert id in laravel with example ?

March 5, 2022February 22, 2024

In this article i will learn you to get last insert id in laravel, In laravel we can execute our queries in two ways first is using the laravel eloquent and other one is DB builder. so in the both pattern we can get the last inserted i in laravel….

Read More
Laravel Get Domain Name in Laravel

How to Get Domain Name in Laravel ?

November 27, 2023March 16, 2024

In this blog post we will learn to get domain name in laravel. Laravel inbuilt library for handling the request and response can do this task very seamlessly. A domain name is the human-readable address that users enter into their browsers to access a website. Knowing the domain name is…

Read More
Php Custom helper function laravel

How to Create Custom Helper Functions in Laravel ?

December 21, 2021November 8, 2023

Sometimes in our application we want to create custom helper functions in laravel to use in our application, Laravel has so many helpers functions to work with strings, URL, email, array, debugging etc. so in this article i will show you to use custom helper functions in our Laravel application….

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version