Symfony 自动加载命名空间的困难

发布于 2024-12-28 10:30:37 字数 1366 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心奴独伤 2025-01-04 10:30:37

我认为名为“lessc”的类应该位于名为“lessc.php”的文件中,您应该更改这一行:

'Less'             => __DIR__.'/../vendor/lessc'

对于这一行:

'Less'             => __DIR__.'/../vendor'

您应该将“lessc”目录的名称更改为“Lessc”。
此外,您应该将资产过滤器移至“Less”命名空间内。

我希望它有帮助!

I think that your class named "lessc" should be in a file named "lessc.php", you should change this line:

'Less'             => __DIR__.'/../vendor/lessc'

for this one:

'Less'             => __DIR__.'/../vendor'

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!

白昼 2025-01-04 10:30:37

Less,您的类名称为

如果您根文件夹

Add Classes Name In Root Namespace Folder 

Lessc:示例:

use Less\Lessc;

use Less\Lessc as rootClassesOfNamespaces;

Your Name Of Classes is Lessc

If You Root Folder Less:

Add Classes Name In Root Namespace Folder 

Example:

use Less\Lessc;

use Less\Lessc as rootClassesOfNamespaces;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文