1

I have two routes: A/{param}/C and B/{param}/C. Also I have a controller with method:

public function index($param, $param1 = false)
{
    //...
}

In case of A/{param}/C I pass only one parameter - one specified in URL, and the function uses the default for second one. In case of the second route, I want to pass true as second parameter. Since it isn't specified in URL, how to pass it to the function?

1 Answer 1

2

If i understand you correctly, without using $_POST, you won't be able to pass a parameter without be being somewhere in the URI (outside of setting a session variable, which i wouldn't advise)

Another option may be to pass it as a query string parameter. It will still be in the url, but won't necessarily be caught in the route pattern

URLs:

A/{param}/C
B/{param}/C?param1=true

--
Controller:

public function index($param)
{

   $param1 = Request::query('param1'); // if not present false, if present, {value}

}

--

Alternatively, if you'd like a friendly URL, you can place a question mark after your second parameter, indicating that it may or may not exist. This will match both of the below URLs

URL:

A/{param}/C
B/{param}/C/true

Route:

Route::get('B/{param}/C/{param1?}', 'YourController@index');

Controller:

public function index($param, $param1 = false)
{
   //
}
Sign up to request clarification or add additional context in comments.

2 Comments

So I can't pass it in array('as' => '...', 'uses' => 'MyController@index', 'param1' => true) or any other way?
Not when using the index method for both. You could use Route::get('A/{param}/C', 'YourController@index'); where $param1 is set to false, then a second route pointing to a different method Route::get('B/{param}/C', 'YourController@index2'); where $param1 is set to true. To keep your code DRY, you could just have one line of code in index2 calling index, passing in true return $this->index($param, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.