mardi 29 septembre 2020

How to edit $request-url in middleware in Laravel 5.8 before it hits route API

I'm developing an API. I want to edit the incoming $request->url so it hits different routes based on the authorization. It should work for any kind of request (POST, GET , DELETE etc...). By now I 've come this far. The middleware get hit, but $request->server->set('REQUEST_URI','http://...'); doesn 't have any effect. The incoming $request url stays at it is. This is the simplified middleware code of the class RedirectToUrl:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use Illuminate\Http\Request;
class RedirectToUrl
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
     if($request->user()->role()->first()->role === "admin"){
        $request->server->set('REQUEST_URI','http://rest-api-with-session-httponly:8888/api/admin');
     }elseif($request->user()->role()->first()->role === "basic"){
        $request->server->set('REQUEST_URI','http://rest-api-with-session-httponly:8888/api/basic');
     }else{
        $request->server->set('REQUEST_URI','http://rest-api-with-session-httponly:8888/api/basic');
     }
       
         
        return $next($request);
        
    }
} 

I`ve put the middleware RedirectToUrl::class at the end of the middleware priority:

protected $middlewarePriority = [
        \App\Http\Middleware\AddAuthHeader::class,
        \Illuminate\Auth\Middleware\Authenticate::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\Authenticate::class,
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Illuminate\Auth\Middleware\Authorize::class,
        \App\Http\Middleware\CheckRole::class,
        \App\Http\Middleware\RedirectToUrl::class,
    ];

Any hint appreciated. Thx!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3ielTYr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire