在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么?

What is the best practice to create a custom helper function in php Laravel 5?(在 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 中创建自定义辅助函数的最佳实践是什么?

推荐答案

  1. 在您的 app/Http 目录中,创建一个 helpers.php 文件并添加您的函数.
  2. composer.json 中,在 autoload 块中,添加 "files": ["app/Http/helpers.php"].
  3. 运行composer dump-autoload
  1. Within your app/Http directory, create a helpers.php file and add your functions.
  2. Within composer.json, in the autoload block, add "files": ["app/Http/helpers.php"].
  3. Run composer dump-autoload

应该可以.:)

这篇关于在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)