phplaravellumenpsr-7

PSR7 on Laravel Routes: access to latest params


Hi have the next route

Route::get('/{param1}',
        'Http\Controllers\SomeController@getCollection');
Route::get('/{param_for_middleware}/{param1}',
        'Http\Controllers\SomeController@getCollection');

And, on my controller, I have

// PSR7 code
use Illuminate\Routing\Controller;
use Psr\Http\Message\ServerRequestInterface;

class SomeController extends Controller
    public function getCollection(ServerRequestInterface $request)
    {
        // here, I need access to param1
        dd($request);
    }
}

With Original Laravel Router We can get value with $this->route()->parameters()['param1]. But, when we use PSR7 on Laravel?


Solution

  • You can, still using the good old method?

    public function getCollection(ServerRequestInterface $request, $param1)
    {
        // here is your param1!
        dd($param1);
    }
    

    Edit

    If you need more than one parameters and you need to ensure their positions, you could do this:

    public function getCollection(
        ServerRequestInterface $request,
        \Illuminate\Http\Request $illuminateRequest
    )
    {
        // here is your param1 & param_for_middleware!
        dd($illuminateRequest->route('param1') . ' & ' .
           $illuminateRequest->route('param_for_middleware'));
    }