Symfony 自动加载命名空间的困难
我正在尝试在 symfony 2 中自动加载一个类。该类位于 vendor/lessc/lessc.inc.php
它未加载,表示未找到 lessc 类。我做错了什么?
来自 app/autoloader.php
$loader->registerNamespaces(array(
//...
'Less' => __DIR__.'/../vendor/lessc',
));
来自 lessc.inc.php
namespace Less;
class lessc {
//..
}
来自 vendor/assetic/src/Assetic/Filter/LessphpFilter.php 调用lessc
类。
namespace Assetic\Filter;
use Assetic\Asset\AssetInterface;
use Less;
//...
class LessphpFilter implements FilterInterface
{
public function filterLoad(AssetInterface $asset)
{
//...
$lc = new Less\lessc();
编辑:基于我得到的建议的解决方案:
类路径= vendor/Less/lessc.php
vendor/Less/lessc.php
namespace Less;
class lessc {
vendor/assetic/src/Assetic/Filter/LessphpFilter.php 与上面相同。
在树枝模板内
{% stylesheets
'@MyBundle/Resources/public/less/styles.less'
filter='lessphp'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
app/autoloader.php
'Less' => __DIR__.'/../vendor'
I'm trying to autoload a class within symfony 2. The class lives in vendor/lessc/lessc.inc.php
It's not loading saying the lessc class is not found. What am I doing wrong?
From app/autoloader.php
$loader->registerNamespaces(array(
//...
'Less' => __DIR__.'/../vendor/lessc',
));
From lessc.inc.php
namespace Less;
class lessc {
//..
}
From vendor/assetic/src/Assetic/Filter/LessphpFilter.php which calls the lessc
class.
namespace Assetic\Filter;
use Assetic\Asset\AssetInterface;
use Less;
//...
class LessphpFilter implements FilterInterface
{
public function filterLoad(AssetInterface $asset)
{
//...
$lc = new Less\lessc();
Edit: Solution based on advice I got:
Class path = vendor/Less/lessc.php
vendor/Less/lessc.php
namespace Less;
class lessc {
vendor/assetic/src/Assetic/Filter/LessphpFilter.php is the same as above.
within a twig template
{% stylesheets
'@MyBundle/Resources/public/less/styles.less'
filter='lessphp'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
app/autoloader.php
'Less' => __DIR__.'/../vendor'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为名为“lessc”的类应该位于名为“lessc.php”的文件中,您应该更改这一行:
对于这一行:
您应该将“lessc”目录的名称更改为“Lessc”。
此外,您应该将资产过滤器移至“Less”命名空间内。
我希望它有帮助!
I think that your class named "lessc" should be in a file named "lessc.php", you should change this line:
for this one:
You should change the name of the "lessc" dir for "Lessc".
Besides, you should move your assetic filter inside your "Less" namespace.
I hope it helped!
Less
,您的类名称为如果您根文件夹
Lessc:示例:
Your Name Of Classes is
Lessc
If You Root Folder
Less
:Example: