创建Laravel自定义Helper辅助方法

2020年5月21日 335点热度 0人点赞 0条评论
导读:本文将花点时间来解释Laravel中的Helper方法是如何创建的。

图片


Laravel中的Helper方法是用来创建自定义的方法,也称为辅助方法。在之前我不太热衷于创建自定义函数,随着时间流转,我开始越来越频繁的使用它们。


什么是Laravel助手


Laravel框架中的Helper方法其实就是普通的PHP函数,开发者可以从Laravel应用程序的任何部分来调用它,比如我们已经在该框架中使用内置的route(),url(),view(),dd()等方法一样,不需要引用任何PHP文件或者声明任何namespace来使用,如同奇迹般,他们可以在你的应用程序中的任何部件中使用。


接下来,我开始讲述用于自定义创建辅助方法的用例。如果代码中多次使用了通用逻辑,可以把它们提取到辅助方法中。


我常在Blade视图中使用辅助方法来简化格式设置,例如在数字前添加货币符号,或者在特定的数字中格式化显示,以及在应用程序中设置人性化的日期等。


创建自定义辅助方法


在Laravel中,在app目录中创建一个文件,然后通过composer让应用程序启动时自动加载。创建自定义辅助方法并非易事,在我的经验中,将功能放在Helps.php中,然后将该文件放在app/Utils下,实用程序工具类,你可以根据自身情况在app中任何目录都可以。

接下来,需要将该文件登记到位于composer.json,这个在项目根目录的文件中。

{
"name": "laravel/framework",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"authors": [
{
"name": "Taylor Otwell",
"email": "[email protected]"
}
],
"require": {
"php": "^7.2.5",
"ext-json": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"doctrine/inflector": "^1.4|^2.0",
"dragonmantank/cron-expression": "^2.0",
"egulias/email-validator": "^2.1.10",
"league/commonmark": "^1.3",
"league/flysystem": "^1.0.8",
"monolog/monolog": "^2.0",
"nesbot/carbon": "^2.17",
"opis/closure": "^3.1",
"psr/container": "^1.0",
"psr/simple-cache": "^1.0",
"ramsey/uuid": "^3.7|^4.0",
"swiftmailer/swiftmailer": "^6.0",
"symfony/console": "^5.0",
"symfony/error-handler": "^5.0",
"symfony/finder": "^5.0",
"symfony/http-foundation": "^5.0",
"symfony/http-kernel": "^5.0",
"symfony/mime": "^5.0",
"symfony/polyfill-php73": "^1.17",
"symfony/process": "^5.0",
"symfony/routing": "^5.0",
"symfony/var-dumper": "^5.0",
"tijsverkoyen/css-to-inline-styles": "^2.2.2",
"vlucas/phpdotenv": "^4.0",
"voku/portable-ascii": "^1.4.8"
},
"replace": {
"illuminate/auth": "self.version",
"illuminate/broadcasting": "self.version",
"illuminate/bus": "self.version",
"illuminate/cache": "self.version",
"illuminate/config": "self.version",
"illuminate/console": "self.version",
"illuminate/container": "self.version",
"illuminate/contracts": "self.version",
"illuminate/cookie": "self.version",
"illuminate/database": "self.version",
"illuminate/encryption": "self.version",
"illuminate/events": "self.version",
"illuminate/filesystem": "self.version",
"illuminate/hashing": "self.version",
"illuminate/http": "self.version",
"illuminate/log": "self.version",
"illuminate/mail": "self.version",
"illuminate/notifications": "self.version",
"illuminate/pagination": "self.version",
"illuminate/pipeline": "self.version",
"illuminate/queue": "self.version",
"illuminate/redis": "self.version",
"illuminate/routing": "self.version",
"illuminate/session": "self.version",
"illuminate/support": "self.version",
"illuminate/testing": "self.version",
"illuminate/translation": "self.version",
"illuminate/validation": "self.version",
"illuminate/view": "self.version"
},
"require-dev": {
"aws/aws-sdk-php": "^3.0",
"doctrine/dbal": "^2.6",
"filp/whoops": "^2.4",
"guzzlehttp/guzzle": "^6.3.1|^7.0",
"league/flysystem-cached-adapter": "^1.0",
"mockery/mockery": "^1.3.1",
"moontoast/math": "^1.1",
"orchestra/testbench-core": "^5.0",
"pda/pheanstalk": "^4.0",
"phpunit/phpunit": "^8.4|^9.0",
"predis/predis": "^1.1.1",
"symfony/cache": "^5.0"
},
"conflict": {
"tightenco/collect": "<5.5.33"
},
"autoload": {
"files": [
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Support/helpers.php"
],
"psr-4": {
"Illuminate\\": "src/Illuminate/"
}
},
"autoload-dev": {
"files": [
"tests/Database/stubs/MigrationCreatorFakeMigration.php"
],
"psr-4": {
"Illuminate\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "7.x-dev"
}
},
"suggest": {
"ext-gd": "Required to use Illuminate\\Http\\Testing\\FileFactory::image().",
"ext-memcached": "Required to use the memcache cache driver.",
"ext-pcntl": "Required to use all features of the queue worker.",
"ext-posix": "Required to use all features of the queue worker.",
"ext-redis": "Required to use the Redis cache and queue drivers (^4.0|^5.0).",
"aws/aws-sdk-php": "Required to use the SQS queue driver, DynamoDb failed job storage and SES mail driver (^3.0).",
"doctrine/dbal": "Required to rename columns and drop SQLite columns (^2.6).",
"filp/whoops": "Required for friendly error pages in development (^2.4).",
"fzaninotto/faker": "Required to use the eloquent factory builder (^1.9.1).",
"guzzlehttp/guzzle": "Required to use the HTTP Client, Mailgun mail driver and the ping methods on schedules (^6.3.1|^7.0).",
"laravel/tinker": "Required to use the tinker console command (^2.0).",
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^1.0).",
"league/flysystem-cached-adapter": "Required to use the Flysystem cache (^1.0).",
"league/flysystem-sftp": "Required to use the Flysystem SFTP driver (^1.0).",
"mockery/mockery": "Required to use mocking (^1.3.1).",
"moontoast/math": "Required to use ordered UUIDs (^1.1).",
"nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).",
"pda/pheanstalk": "Required to use the beanstalk queue driver (^4.0).",
"phpunit/phpunit": "Required to use assertions and run tests (^8.4|^9.0).",
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).",
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0).",
"symfony/cache": "Required to PSR-6 cache bridge (^5.0).",
"symfony/filesystem": "Required to create relative storage directory symbolic links (^5.0).",
"symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^2.0).",
"wildbit/swiftmailer-postmark": "Required to use Postmark mail driver (^3.0)."
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}

接下来,运行composer dump-autoload并重新启动应用程序,这样确保Helpers.php在Laravel启动时加载。

这样,你可以在Helpers.php中创建一个方法,然后在应用程序中访问调用此方法。

小结

Laravel 框架中的Helpers是一个非常方便且强大的功能,能够帮助开发者节约大量时间,这也是本文分享的初衷,希望更多人使用和创建自定义方法。


Happy Coding:)

参考资源:

https://laravel.com/docs/7.x/helpers

https://laravel.com/docs/7.x/facades#facades-vs-helper-functions

作者:晓明

来源:21CTO社区

推荐阅读:

PHP技术栈 :PHP 7.4,PHP Storm 201903,Laravel6.7更新

Laravel6与VueJS联合开发详解

PHP 8.0:正对内核中加入属性进行投票

图片

14900创建Laravel自定义Helper辅助方法

root

这个人很懒,什么都没留下

文章评论