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 How to compress and reduce image size while uploading in laravel

How to compress and reduce image size while uploading in laravel 8 ?

May 14, 2022May 14, 2022

While uploading the images in our application user can upload big size image but storing and rendering big size of image can reduce the performance of the application therefor its an important aspect to reduce the size of image without resizing it so we can keep the original size and…

Read More

Create Custom Class or Custom Library in Laravel 10

March 14, 2023March 16, 2024

In Laravel, you can create custom classes or custom library in laravel that can be used throughout your application. These classes can be used to encapsulate common functionality, business logic, or to abstract away complex operations. Thus I this article i will show you to create a class and use…

Read More
Php Running raw sql query in migration laravel

How to run raw SQL query in migration Laravel ?

February 12, 2022February 15, 2022

Laravel provides its inbuilt feature to take care of migrations, Migrations like to make the version control for our database schema and share it across the developers but in laravel migrations we need to create eloquent query Using the schema class or Facade and then we need to run it…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version