0

How do you pass parameters on a function in routes.php

Route::get('/{order}', array("as" => "ordered", "uses" => 'HomeController@ordered'), function() { Session::put('order',$order') }); 

Doesn't seem to work

4 Answers 4

4

How about like this:

<?php Route::get('/{order}', array("as" => "ordered", "uses" => 'HomeController@ordered'), function() use ($order) { Session::put('order',$order') }); 

More detailed examples can be found here Anonymous functions Example #3 Inheriting variables from the parent scope

What is important here is to notice the use keyword. It lets you pass variables defined outside of your anonymous function.

Edit

My bad, its seems I misunderstood. Try it this way:

<?php Route::get('/{order}', array("as" => "ordered", "uses" => 'HomeController@ordered'), function($order) { Session::put('order',$order') }); 
Sign up to request clarification or add additional context in comments.

1 Comment

I tried it but didn't work. i need to get the value of {order} and use it on the function.
2

Just Change to the following..........

Route::get('/{order}','HomeController@ordered'); 

And On HomeController............

 public function ordered($order){ ///////////////////your code here, use $order here }); 

2 Comments

that's what I'm using right now, I just want to know if it is possible.
It's possible if u shift your function to the controller and keep your route like Route::get('/{order}', array("as" => "ordered", "uses" => 'HomeController@ordered'));
2

Please follow this Route:

Route::get('/{order}','HomeController@ordered'); 

Controller: method of HomeController-

function ordered($order) { } 

you can also pass more than one parameters

Route:

Route::get('/{order}/{id}','HomeController@ordered'); 

Controller:

function ordered($order,$id) { } 

Comments

0

Better (5 years) late than never:

Route::get('/{order}', array("as" => "ordered", "uses" => 'HomeController@ordered'), function() { Session::put('order', request()->route('order')); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.