如何在自定义帮助程序或指令中获取 Laravel 中的查看数据

我需要在自定义帮助程序或指令中获取从控制器传递的所有视图数据,在此视图刀片模板中调用。


所以在刀片模板中有翻译:


@lang($periodName . '.H1 title', ['time' => $time])

我想让它更短。为此,我创建了 helper periodTrans ('H1 Title')。


function periodTrans($title) {

 return __($periodName . '.' . $title,  ['time' => $time]);

}

有没有办法在辅助函数中访问$periodName和$time变量,而不是像参数一样传递它们并使函数更短?


POPMUISE
浏览 165回答 1
1回答

慕仙森

AFAIK 这应该有效。在您的controller:public function __construct(){    .....    \View::share('periodName', $periodName);    \View::share('time', $time);}你的帮手:function periodTrans($title) {    $data = \View::getShared();     return __($data['periodName'] . '.' . $title,  ['time' => $data['time']]);}
打开App,查看更多内容
随时随地看视频慕课网APP