本文介紹了在 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/Http
directory, create ahelpers.php
file and add your functions. - Within
composer.json
, in theautoload
block, add"files": ["app/Http/helpers.php"]
. - Run
composer dump-autoload
應該可以.:)
這篇關于在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!