Laravel 4 中的自动加载助手
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17239133/
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
Auto-loading Helpers in laravel 4
提问by Mike Rockétt
I've been trying to get Laravel 4 to auto-load helper files from the app/helpers
directory (I created this, obviously).
我一直在尝试让 Laravel 4 从app/helpers
目录中自动加载帮助文件(显然是我创建的)。
I started by doing it the Composer way: add the path to composer.json
, and then running dump-autoload
. This did not work. I then tried with the app/start/global.php
file, which did not work either.
我首先以 Composer 的方式进行操作:将路径添加到composer.json
,然后运行dump-autoload
. 这没有用。然后我尝试使用该app/start/global.php
文件,该文件也不起作用。
Note, I am not throwing classes into the helper files - that's what Facades and Packages are for. I only need small helper functions, similar to those of Laravel's own (in the vendor
directory). I only say this because it seems that composers dump lists classes (with namespaces) only.
请注意,我没有将类放入帮助文件中——这就是 Facades 和 Packages 的用途。我只需要一些小的辅助函数,类似于 Laravel 自己的(在vendor
目录中)。我之所以这么说是因为似乎作曲家转储仅列出类(带有名称空间)。
What can I do to get the helpers to auto-load?
我该怎么做才能让助手自动加载?
Update
更新
It also seems that the ClassLoader::addDirectories()
function does not work for classes - why is it there then? Do I have to use both this and Composer?
该ClassLoader::addDirectories()
函数似乎也不适用于类 - 那为什么会出现呢?我必须同时使用这个和 Composer 吗?
Edit
编辑
It seems my question is not being understood. In my app/helpers
directory, I have a file called paths.php
. I want to be able to call a function within it (theme_path($location)
) within the global scope.
似乎我的问题没有被理解。在我的app/helpers
目录中,我有一个名为paths.php
. 我希望能够theme_path($location)
在全局范围内调用其中的函数 ( )。
采纳答案by Alexandre Danault
Autoloading is only for classes, you can't really autoload random php files based on one of their function names.
自动加载仅适用于类,您不能真正根据其函数名称之一自动加载随机 php 文件。
Your best bet is go OOP and use classes for your helpers. You'll gain the efficiency of autoloaded files only when needed, and you'll be closer to what the rest of the Laravel community does.
你最好的选择是去 OOP 并为你的助手使用类。您只会在需要时获得自动加载文件的效率,并且您将更接近 Laravel 社区的其他人所做的工作。
But if you still want to use plain old non-object php functions, I guess you could simply individually require()
all of them from start.php, but they'd all be loaded on every page request.
但是,如果您仍然想使用普通的老式非对象 php 函数,我想您可以简单地require()
从 start.php 中单独所有这些函数,但它们都会在每个页面请求时加载。
回答by Safeer
You need to do the following:
您需要执行以下操作:
"autoload": {
"files": [
"file location go here"
]
},
Then you'll be able to use the functions in the helper file. However it's worth noting that this will load the file explicitly on every request. See http://getcomposer.org/doc/04-schema.md#filesfor more details.
然后您将能够使用帮助文件中的函数。但是值得注意的是,这将在每个请求上显式加载文件。有关更多详细信息,请参阅http://getcomposer.org/doc/04-schema.md#files。
I would recommend that unless this is a heavily used file of helpers, avoid doing this and just organise it with namespaces and classes to avoid naming collisions.
我建议除非这是一个大量使用的帮助文件,否则避免这样做,只需使用命名空间和类来组织它以避免命名冲突。
回答by Israel Ortu?o
Well... I can show you my composer and helpers to show how I made it to work:
嗯......我可以向你展示我的作曲家和助手来展示我是如何让它工作的:
"autoload": {
"psr-0": {
"Helpers": "app/libraries"
}
}
And then in app/libraries/Helpers/DateHelper.php (for instance)
然后在 app/libraries/Helpers/DateHelper.php (例如)
<?php namespace Helpers;
class DateHelper extends \DateTime {
}
After creating the helper file I just run the
创建帮助文件后,我只运行
composer dump-autoload
作曲家转储自动加载
And now just use your helper as Helpers\DateHelper
现在只需将您的助手用作 Helpers\DateHelper
回答by dede
Not directly related to your question, but Laravel has integrated Carbon library (https://github.com/briannesbitt/Carbon) for dates handling. It's inside vendor/nesbot
folder, so you can use it directly in Laravel.
与您的问题没有直接关系,但 Laravel 已经集成了用于日期处理的Carbon 库(https://github.com/briannesbitt/Carbon)。它在vendor/nesbot
文件夹内,所以你可以直接在 Laravel 中使用它。