我想复制要在一个地方定义的网站页面中的变量。 我做了一点搜索,我得到了这段代码
class BaseController extends Controller {
public function __construct() {
$this->beforeFilter(function() {
View::share('setting',Settings::find(1));
});
}
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
我将此代码添加到baseController并进行操作。
public function __construct() {
$this->beforeFilter(function() {
View::share('setting',Settings::find(1));
});
}
但这里只是一个变量定义,我需要定义其他值。 请帮助...
答案 0 :(得分:0)
你可以这样做:
public function __construct() {
$this->beforeFilter(function() {
View::share('setting',Settings::find(1));
//you can add more
View::share('variable_name', $variable_value);
});
}
答案 1 :(得分:0)
我找到了答案 将这些变量放在数组中并使用它。
View::share('datamaster',['setting' => $setting=Settings::find(1),
'nav'=>$nav,
'catpro_address' => $catpro_address,
'apple_pro_cat'=> $apple_pro_cat,
'sound_pro_cat'=>$sound_pro_cat,
'watch_pro_cat'=>$watch_pro_cat,
'storage_pro_cat'=>$storage_pro_cat,
'other_pro_cat'=>$other_pro_cat,
'mobile_pro_cat'=>$mobile_pro_cat,
'tablet_pro_cat'=>$tablet_pro_cat]);