我在Laravel中呼叫助手时遇到一些问题。 我有刀片,上面有这段代码
@foreach ($sel->get_offers() as $o)
<option value="{{$o->get_date_range()->get_range()}}" {{($o->get_offer_key() == $u_key)? 'selected': ''}}>
@php
$select = $o->get_date_range()->get_start('j M D');
$change = str_replace(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [uctrans('labels.Monday'), uctrans('labels.Tuesday'), uctrans('labels.Wednesday'), uctrans('labels.Thursday'), uctrans('labels.Friday'), uctrans('labels.Saturday'), uctrans('labels.Sunday'), uctrans('labels.Jan'), uctrans('labels.Feb'), uctrans('labels.Mar'), uctrans('labels.Apr'), uctrans('labels.May'), uctrans('labels.Jun'), uctrans('labels.Jul'), uctrans('labels.Aug'), uctrans('labels.Sep'), uctrans('labels.Oct'), uctrans('labels.Nov'), uctrans('labels.Dec')], $select); @endphp
{{$change}}
</option>
@endforeach
所以现在,我需要像这样从变量中将变量$change
从刀片中删除并导入到助手中(但仍然调用刀片)
function translate_selects()
{
$change = str_replace(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [uctrans('labels.Monday'), uctrans('labels.Tuesday'), uctrans('labels.Wednesday'), uctrans('labels.Thursday'), uctrans('labels.Friday'), uctrans('labels.Saturday'), uctrans('labels.Sunday'), uctrans('labels.Jan'), uctrans('labels.Feb'), uctrans('labels.Mar'), uctrans('labels.Apr'), uctrans('labels.May'), uctrans('labels.Jun'), uctrans('labels.Jul'), uctrans('labels.Aug'), uctrans('labels.Sep'), uctrans('labels.Oct'), uctrans('labels.Nov'), uctrans('labels.Dec')], $select);
return $change;
}
如何在刀片服务器/控制器中称呼此助手?我没有对config / app.php进行任何更改。
答案 0 :(得分:0)
您可以创建文件并将其命名为helper.php。
此文件将没有其正常功能的类或接口。
然后在composer.json文件中将helper.php路径放在文件密钥中。
那样。
"autoload": {
"files": [
"app/helper.php" // full path here for your file
],
}
helper.php文件将是这样。
<?php
function translate_selects()
{
$change = str_replace(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [uctrans('labels.Monday'), uctrans('labels.Tuesday'), uctrans('labels.Wednesday'), uctrans('labels.Thursday'), uctrans('labels.Friday'), uctrans('labels.Saturday'), uctrans('labels.Sunday'), uctrans('labels.Jan'), uctrans('labels.Feb'), uctrans('labels.Mar'), uctrans('labels.Apr'), uctrans('labels.May'), uctrans('labels.Jun'), uctrans('labels.Jul'), uctrans('labels.Aug'), uctrans('labels.Sep'), uctrans('labels.Oct'), uctrans('labels.Nov'), uctrans('labels.Dec')], $select);
return $change;
}
很高兴告诉我您的情况如何。
答案 1 :(得分:0)
为Helper添加新商品
artisan make:provider HelperServiceProvider
移至app/Providers/HelperServiceProvider.php
通过注册方法调用所有HelperFile
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
在providers
config / app.php中添加条目
'providers' => [
'App\Providers\HelperServiceProvider',
]
现在您可以在任何地方调用自定义帮助器功能。