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 Laravel 8 ajax form validation

Laravel 8 Ajax form validation with example

October 20, 2021November 16, 2021

In this article, I will demonstrate to show Laravel server side validation using Ajax. We will use jQuery to show the validation and submit the form. Using the jQuery and AJAX we will prevent the page reloading and show the validation. In this article, we will use the same simple…

Read More
Php How to Upload image with preview in Laravel 9 with example

How to Upload image with preview in Laravel 9 with example ?

May 14, 2022May 14, 2022

Upload image with preview in laravel or for any website can be basic requirement like set up a profile picture to providing the documents. Laravel 9 provides robust functionality to upload and process the image with security. Laravel simply use Request file method to access the uploaded file and then…

Read More
Php How to get random rows in laravel example

How to get random rows in laravel example ?

October 10, 2022March 16, 2024

In this blog post, we’ll take a look at how to get random rows in Laravel database. We’ll explore the use of the QueryBuilder, Eloquent, and the DB facade. laravel has inbuilt eloquent method to get the random records from database using the inRandomOrder method. we will understand the random…

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