在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么? [英] What is the best practice to create a custom helper function in php Laravel 5?

查看:31
本文介绍了在 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 中创建自定义辅助函数的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆