What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么?)
问题描述
默认的 created_at 日期继续打印为 MySQL 格式:2015-06-12 09:01:26.我想像 12/2/2017 一样以我自己的方式打印它,以及未来的其他格式.
the default created_at date keep printing out as an MySQL format : 2015-06-12 09:01:26. I wanted to print it as my own way like 12/2/2017, and other formats in the future.
一个名为 DataHelper.php 的文件并将其存储在 /app/Helpers/DateHelper.php - 它看起来像这样
a file called DataHelper.php and store it at /app/Helpers/DateHelper.php - and it looks like this
<?php
namespace AppHelpers;
class DateHelper {
public static function dateFormat1($date) {
if ($date) {
$dt = new DateTime($date);
return $dt->format("m/d/y"); // 10/27/2014
}
}
}
我想要
能够在我的刀片视图中调用它
I want
to be able to called it in my blade view like
DateHelper::dateFormat1($user->created_at)
我不知道接下来要做什么.
I'm not sure what to do next.
在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么?
推荐答案
- 在您的
app/Http目录中,创建一个helpers.php文件并添加您的函数. - 在
composer.json中,在autoload块中,添加"files": ["app/Http/helpers.php"]. - 运行
composer dump-autoload
- Within your
app/Httpdirectory, create ahelpers.phpfile and add your functions. - Within
composer.json, in theautoloadblock, add"files": ["app/Http/helpers.php"]. - Run
composer dump-autoload
应该可以.:)
这篇关于在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 php Laravel 5 中创建自定义辅助函数的最佳实践
基础教程推荐
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- php中的foreach复选框POST 2021-01-01
- Web 服务器如何处理请求? 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php中的PDF导出 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
