Laravel 4 Helpers 和基本功能的最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17088917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Practices for Laravel 4 Helpers and Basic Functions?
提问by Jason Spick
I'm trying to understand the best place to put a global function in Laravel 4. For example, date formatting. I don't think making a facade is worth it as facades are too modular. I've read articles about creating a library folder and storing classes there but that also seems like a lot for a simple function. Shouldn't a 'tool' like this be available in Blade templates?
我试图了解在 Laravel 4 中放置全局函数的最佳位置。例如,日期格式。我不认为制作立面是值得的,因为立面太模块化了。我读过关于创建库文件夹和在那里存储类的文章,但这对于一个简单的函数来说似乎也很多。Blade 模板中不应该提供这样的“工具”吗?
What are the best practices for something like this and how do I make it available to Blade templates?
此类事情的最佳实践是什么,我如何使其可用于 Blade 模板?
回答by Alexandre Danault
The ugly, lazy and awful way: At the end of bootstrap/start.php
, add an include('tools.php')
and place your function in that new file.
丑陋、懒惰和可怕的方式:在 的末尾bootstrap/start.php
,添加一个include('tools.php')
并将您的函数放在该新文件中。
The clean way: Create a library. That way it'll be autoloaded ONLY when you actually use it.
干净的方法:创建一个库。这样它只会在您实际使用时自动加载。
- Create a
libraries
folder inside yourapp
folder - Create your library file, create a class in it, and add static functions to it
- Option 1: Edit
start/global.php
to addapp_path().'/libraries'
to theClassLoader::addDirectories(
array. - Option 2: Edit
composer.json
to add"app/libraries"
to theautoload
array. Runcomposer dump-autoload
- Call your class and static functions from your views.
libraries
在您的app
文件夹中创建一个文件夹- 创建您的库文件,在其中创建一个类,并向其中添加静态函数
- 选项 1:编辑
start/global.php
以添加app_path().'/libraries'
到ClassLoader::addDirectories(
数组。 - 选项 2:编辑
composer.json
以添加"app/libraries"
到autoload
数组。跑composer dump-autoload
- 从您的视图中调用您的类和静态函数。
About your options, quoted from the global.php
file
关于您的选项,引用自global.php
文件
In addition to using Composer, you may use the Laravel class loader to load your controllers and models. This is useful for keeping all of your classes in the "global" namespace without Composer updating.
除了使用 Composer,您还可以使用 Laravel 类加载器来加载您的控制器和模型。这对于在没有 Composer 更新的情况下将所有类保存在“全局”命名空间中很有用。
You can combine both options, where the Laravel class loader will automatically search for classes in the registered directories (Option 1, easier) and Composer will keep record of all the classes but only after you update it (Option 2, might improve performance).
您可以结合使用这两个选项,其中 Laravel 类加载器将自动搜索注册目录中的类(选项 1,更简单),而 Composer 将保留所有类的记录,但仅在您更新它之后(选项 2,可能会提高性能)。
回答by JasonMortonNZ
My way of doing this is to create a new folder in the /app
directory in the root of your Laravel 4 project. Then add this folder to the first array of the /app/start/global.php
file like so:
我这样做的方法是/app
在 Laravel 4 项目的根目录中创建一个新文件夹。然后将此文件夹添加到文件的第一个数组中,/app/start/global.php
如下所示:
<?php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/classes', // This line is the one I've added.
));
As long as the folder structure within the new /app/classes
folder follows your namespacing convention. Laravel 4 will autoload all the classes/files within this folder. This way there's no need to dig into any composer files or run composer command.
只要新/app/classes
文件夹中的文件夹结构遵循您的命名空间约定。Laravel 4 将自动加载此文件夹中的所有类/文件。这样就无需深入研究任何作曲家文件或运行作曲家命令。
Not sure if this is best practice but it certainly works.
不确定这是否是最佳实践,但它肯定有效。
If you created a simple file called /app/classes/Helpers/Helper.php
such as this:
如果您创建了一个简单的文件/app/classes/Helpers/Helper.php
,如下所示:
<?php namespace Helpers;
class Helper {
public static function helloWorld()
{
return 'Hello World';
}
}
All you would need to do is call Helpers\Helper::helloWorld();
所有你需要做的就是打电话 Helpers\Helper::helloWorld();
You could also alias this helper class in your /app/config/app.php
file. Just add something like this to the end of the aliases
array:
您还可以在您的/app/config/app.php
文件中为这个助手类设置别名。只需将这样的内容添加到aliases
数组的末尾:
'Helper' => 'Helpers\Helper'
回答by Antonio Carlos Ribeiro
Laravel's helpers.php method is to add it to your "files" in composer.json (https://github.com/laravel/framework/blob/master/composer.json):
Laravel 的 helpers.php 方法是将它添加到 composer.json ( https://github.com/laravel/framework/blob/master/composer.json) 中的“文件”中:
"autoload": {
"classmap": [
...
],
"files": [
"app/libraries/helpers.php"
],
},
What I do is to create small classes (a few methods per class, one line per method, everything extended from something and DRY, that's my goal),
我所做的是创建小类(每个类有几个方法,每个方法一行,一切都从某事和 DRY 扩展而来,这是我的目标),
class ExtendedCarbon extends Carbon\Carbon {
public function formatDDMMAAAA($date)
{
/// format and return
}
}
save them to them in app/libraries and add to composer.json:
将它们保存在 app/libraries 中并添加到 composer.json:
"autoload": {
"classmap": [
...
"app/libraries",
...
],
},
Execute
执行
composer dump
And then just use them wherever you need
然后在任何需要的地方使用它们
$formatted = (new ExtendedCarbon)->formatDDMMAAAA($date);
Watch this video about refactoring: http://www.youtube.com/watch?v=DC-pQPq0acs
观看有关重构的视频:http: //www.youtube.com/watch?v=DC-pQPq0acs
By the way, I'm kind of sure it was just an example, but you might not need a helper to format dates, since all dates in Laravel are instances of Carbon (https://github.com/briannesbitt/Carbon) and it has loads of methods to format date and time.
顺便说一句,我敢肯定这只是一个例子,但您可能不需要帮助来格式化日期,因为 Laravel 中的所有日期都是 Carbon 的实例(https://github.com/briannesbitt/Carbon)和它有很多方法来格式化日期和时间。
回答by Franz
You can also use View::share()
together with closures to achieve this - I just posted about this: http://www.develophp.org/2014/07/laravel-4-blade-helper-functions/
您也可以View::share()
与闭包一起使用来实现这一点 - 我刚刚发布了这个:http: //www.develophp.org/2014/07/laravel-4-blade-helper-functions/
Added benefit: You don't need to create an extra class and also keep the global namespace clean.
额外的好处:您不需要创建额外的类并保持全局命名空间干净。