Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

Laravel Get Route Parameters in middleware Example

Today, We want to share with you Laravel Get Route Parameters in middleware Example.In this post we will show you Get Laravel Route Parameters in Middleware, hear for Access Route Parameters from Global Middleware we will give you demo and example for implement.In this post, we will learn about Laravel 5.7 – How to get route parameters in your route middleware with an example.

Laravel Get Route Parameters in middleware Example

There are the Following The simple About Laravel Get Route Parameters in middleware Example Full Information With Example and source code.

  PHP MySQL LIKE operator string Function

As I will cover this Post with live Working example to develop php – Laravel middleware get route parameter, so the Get Laravel 5.7 route parameters from the request object for this example is following below.

Get Laravel Route Parameters Example

app/Http/routes.php

// Inline Laravel function to simple handle the route
Route::get('/', function () {
    return view('dashboard');
});

//Laravel Controller method or function to handle middleware the route
Route::get('/wallet', '[email protected]');

Laravel route is generally annotated with main code curly braces

Route::get('/companys/{name}', function ($name) {
    return $name
});

Accessing Route Parameters

php artisan make:middleware CompanyMiddleware

The Laravel PHP file created app/Http/Middlware/CompanyMiddleware.php.

Example 1 : Laravel handle method within the CompanyMiddleware class

app/Http/Middleware/CompanyMiddleware.php

$request->route(‘parameter_name’)

public function handle($request, Closure $next)
{   
    dd($request->route('parameter_name'));
    return $next($request);
}

app/Http/routes.php

Route::get('/companys/{name}', [
    'middleware' => 'App\Http\Middleware\CompanyMiddleware',
     function () {
        return view('dashboard');
}]);

App\Http\Kernel.php

protected $routeMiddleware = [
    'CompanyMiddleware' => \App\Http\Middleware\CompanyMiddleware::class,
];

simple Latest Laravel Project Run this command To php artisan serve on your CMD after then open http://localhost:8000/companys/pakainfo. We should display pakainfo some dumped in the chrome or mozila browser.

  Laravel Multiple middleware from controller in constructor

Example 2 : Route parameters in the middlware

$request->route()->paremeters()

Route::get('/companys/{id}/{category}/{name}', [
    'middleware' => CompanyMiddleware',
     function () {
        return view('dashboard');
}]);

Laravel update the middleware’s main handle method

public function handle($request, Closure $next)
{   
    dd($request->route()->parameters());
    return $next($request);
}

And then We gonna to this url http://localhost:8000/companys/23/pakainfo/company should sample url for the test parametes in your any Chrome or Mozila browser as an array, as well as the all parameter differents names as the keys, as well as what was argument passed as values.

Get Laravel Route Parameters in Middleware

app/http/routes.php

Route::get('/about', function() {
    return view('about');
});
Route::get('/role', function() {
    return view('role');
});
Route::get('/setting', function() {
    return view('setting');
});

// Route that handles wallet comes last
Route::get('/{employeename}', function() {
    return view('wallet');
});

app/Http/routes.php

Route::get('/{employeename}',  [
     'middleware' => 'WalletMiddleware',
     function () {
        return view('dashboard');
}]);

wallet middleware, I Included this in the handle main function

<?php

namespace App\Http\Middleware;

use Closure;

// Included Response namespace
use Illuminate\Http\Response;

class WalletMiddleware
{
    public function handle($request, Closure $next)
    {   
        $routes = ["about", "setting", "role"];
        $route = $request->route('employeename');

    // Redirect to main your custom blade file or any view page if it doesn't here some relate to a wallet
        if (in_array($route, $routes)) {
            return new Response(view($route));
        }

        return $next($request);
    }
}

Angular 6 CRUD Operations Application Tutorials

Read :

  php try catch Block Exception Handling

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Get Route Parameters in middleware Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of