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 9 ?

May 14, 2022May 15, 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
Php Unique Validation in laravel 8

Unique validation in Laravel with example

November 21, 2021June 17, 2022

Laravel also support validation for database operations. Laravel unique validation validates the table column uniqueness using unique validation. In this tutorial we will learn about the unique validation to table columns and also while updating the value too. you can use this to check the uniqueness of email, username ,…

Read More
Php How to Delete Cookies in Laravel

How to Delete Cookies in Laravel ?

July 26, 2022July 27, 2022

In this article we will learn to delete cookies in laravel. In our last article we learnt to set and get cookies and how cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. In…

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

  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version