I tried to pass variable in route with this, and it works:
Route::get('/path', array("as"=>"myname", function(){
$data = include app_path()."/views/myconfig.php";
return View::make('pageview',$data);
}));
As I need to use the data in most routes, I am thinking to move the $data
outside so that it looks cleaner.
$data = include app_path()."/views/myconfig.php";
Route::get('/path', array("as"=>"myname", function(){
return View::make('pageview',$data);
}));
Route::get('/path2', array("as"=>"myname2", function(){
return View::make('pageview2',$data);
}));
But this end up giving me error saying that Undefined variable: data
. Why is it moving it up become unreadable? What do you suggest a better way I can do it?
Thank you.
Not related to Laravel, it is the matter of PHP itself
$data = include app_path()."/views/myconfig.php";
Route::get('/path', array("as"=>"myname", function() use ($data){
return View::make('pageview',$data);
}));
Route::get('/path2', array("as"=>"myname2", function() use ($data){
return View::make('pageview2',$data);
}));
Why it does not work in your code? Because of scopes. $data
is defined out of callback function's (or closure, call it whatever you want) scope - it's not available for callback. use
keyword let's you do this.