Accessing the URL parameter in the route prefix from middleware

I am trying to access the route prefix parameter from my middleware.

Given this URL: http://www.example.com/api/v1/campaign/40/status

and the following route:

Route::group( [
    'prefix' => 'api/v1'
], function()
{
    Route::group( [
        'prefix' => 'campaign/{campaign}',
        'where' => [ 'campaign' => '[0-9]+' ],
        'middleware' => [
            'inject_campaign'
        ]
    ], function()
    {
        Route::get( 'status', 'CampaignController@getStatus' );
    } );
} );

      

How do I access the campaign parameter (40 in the example url) from my add_campaign middleware? The middleware is working fine for me, but cannot work out how to access the parameter.

A call $request->segments()

in my middleware gives me part of the route, but that seems like a fragile way to access data. What if the route changes?

+3


source to share


2 answers


You can do it using the shorter syntax

You can use:

echo $request->route()->campaign;

      



or even shorter:

echo $request->campaign;

      

+6


source


Got it!



$request->route()->getParameter('campaign')

0


source







All Articles