user1029829
user1029829

Reputation: 961

How to pass extra parameters to controller from Laravel route

I'm trying to handle basic validation of my API calls in the Laravel's routes. Here is what I want to achieve:

Route::group(['prefix' => 'api/v1/properties/'], function () {
     Route::get('purchased', 'PropertiesController@getPropertyByProgressStatus', function () {
       //pass variable x = 1 to the controller
     });

     Route::get('waiting', 'PropertiesController@getPropertyByProgressStatus', function () {
       //pass variable x = 2 to the controller
});

});

Long story short, depending on the segment of the URI after api/v1/properties/ I want to pass a different parameter to the controller. Is there a way to do that?

Upvotes: 1

Views: 4641

Answers (2)

user1029829
user1029829

Reputation: 961

I was able to get it to work with the following route.php file:

Route::group(['prefix' => 'api/v1/properties/'], function () {
    Route::get('purchased', [
        'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 1
    ]);
    Route::get('remodeled', [
        'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 1
    ]);
    Route::get('pending', [
        'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 3
    ]);
    Route::get('available', [
        'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 4
    ]);
    Route::get('unavailable', [
        'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 5
    ]);
});

and the following code in the controller:

public function getPropertyByProgressStatus(\Illuminate\Http\Request $request) {

$action = $request->route()->getAction();
print_r($action);

Pretty much the $action variable is going to let me access the extra parameter that I passed from the route.

Upvotes: 2

JuanDMeGon
JuanDMeGon

Reputation: 1211

I think that you can do it directly in the controller and receiving the value as a parameter of your route:

First you need to specify the name of the parameter in the controller.

Route::group(['prefix' => 'api/v1/properties/'], function ()
{
    Route::get('{parameter}', PropertiesController@getPropertyByProgressStatus');

In this way, the getPropertyByProgressStatus method is going to receive this value, so in the controller:

class PropertiesController{
....
public function getPropertyByProgressStatus($parameter)
{
    if($parameter === 'purchased')
    {
        //do what you need
    }
    elseif($parameter === 'waiting')
    {
        //Do another stuff
    }
....
}

I hope it helps to solve your issue.

Take a view for this courses: Learn Laravel or Create a RESTful API with Laravel

Best wishes.

----------- Edited --------------- You can redirect to the route that you want:

Route::group(['prefix' => 'api/v1/properties/'], function () {
     Route::get('purchased', function () {
       return redirect('/api/v1/properties/purchased/valueToSend');
     });

     Route::get('waiting', function () {
       return redirect('/api/v1/properties/waiting/valueToSend');
     });

    Route::get('purchased/{valueToSend}', PropertiesController@getPropertyByProgressStatus);
     });

    Route::get('waiting/{valueToSend}', PropertiesController@getPropertyByProgressStatus);
     });
});

The last two routes response to the redirections and send that value to the controller as a parameter, is the most near that I think to do this directly from the routes.

Upvotes: 0

Related Questions