0

Given the following code, I simple want the second route to send an arbitrary value for id or any other variable I can access from within show();

Route::get('foo/{id}', 'FoobarController@show')->where('id', '[0-9]+');
Route::get('bar', 'FoobarController@show')->with('id', -1);    // This pseudo-code doesn't work. I want to send parameter id with an arbitrary value
4
  • You might just want to conditional call a method from within your controller after you check the value of id Commented Apr 17, 2016 at 8:40
  • But in the second route how do I actually send the value of the id? Commented Apr 17, 2016 at 8:45
  • As a null able variable {id?} Commented Apr 17, 2016 at 8:46
  • Have you tried in controller show method as an extra parameter named id something link this public function show($id=0){} ? Commented Apr 17, 2016 at 9:21

1 Answer 1

1

Why not like this?

Routes:

Route::get('bar/{id?}', 'FoobarController@show')->where('id', '[0-9]+');

Controller:

class FoobarController extends Controller{
    public function show($id){
        $id = $id ? $id : "default value";
    }
}

Or:

public function show($id="default value"){..}
Sign up to request clarification or add additional context in comments.

Comments

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.