命名空间没找到类? Symfony2的

时间:2015-08-06 05:38:49

标签: php symfony

这里有新手问题,我创建了一个名为MyServices的Bundle,子文件夹为MyBundle,

我创建了一个名为myAWServ的服务,但是当我调用$this->get('myAWS')->methodCall()时,我收到以下错误:

  

CRITICAL - 致命错误:类'MyServices \ MyBundle \ myAWS \ myAWS'不   发现CRITICAL - 未捕获的PHP异常   Symfony \ Component \ Debug \ Exception \ ClassNotFoundException:“Attempted   从命名空间“MyServices \ MyBundle \ myAWS”加载类“myAWS”。难道   你忘记了另一个命名空间的“use”语句吗?“at   /home/sergio/Desktop/hello_symfony_heroku/app/cache/dev/appDevDebugProjectContainer.php   第1755行

我已经对所有文件进行了一百次审核,但无法找到文件所遵循的问题:

<?php
//File Location : MyServices/MyBundle/Controller/myAWS.php
namespace MyServices\MyBundle\myAWS;

class myAWS
{
    public function __construct($greeting)
    {
        $this->greeting = $greeting;
    }

    public function greet($name)
    {
        return $this->greeting . ' ' . $name;
    }
}

使用bundle创建的Root文件(php app / console generate:bundle)

//Filesource : MyServices/MyBundle/MyServicesMyBundle.php
<?php

namespace MyServices\MyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MyServicesMyBundle extends Bundle
{
}

和services.yml

parameters:
    myAWS.class: MyServices\MyBundle\myAWS\myAWS

services:
    myAWSer:
        class: "%myAWS.class%"
        arguments: [teste]

我已经在AppKernel中加载了它             新的MyServices \ MyBundle \ MyServicesMyBundle(),

目前我正在做一个简单的电话

<?php

namespace MyServices\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction($name)
    {
        die($this->get('myAWSer')->greet($name));

    }
}

我也尝试清除缓存。 对不起,很长的帖子,谢谢你。

1 个答案:

答案 0 :(得分:5)

问题在于:

// File Location : MyServices/MyBundle/Controller/myAWS.php
namespace MyServices\MyBundle\myAWS;

您的文件路径为MyServices/MyBundle/Controller/myAWS.php,但您必须遵循命名空间,因此正确的路径应为MyServices/MyBundle/myAWS/myAWS.php。您还应该查看psr-4 specifications for autoloading