新手问题,我用子文件夹mybundle创建了一个名为myservices的包,
我创建了一个名为myawserv的服务,但当我调用$this->get('myAWS')->methodCall()
时,出现以下错误:
严重-致命错误:类“myservices\mybundle\myaws\myaws”不是
发现严重的未捕获的php异常
symfony\component\debug\exception\classnotfoundexception:“已尝试
从命名空间“myservices\mybundle\myaws”加载类“myaws”。做
您忘记了另一个命名空间的“use”语句吗?在
/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;
}
}
使用包创建的根文件(php app/console generate:bundle)
//Filesource : MyServices/MyBundle/MyServicesMyBundle.php
<?php
namespace MyServices\MyBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class MyServicesMyBundle extends Bundle
{
}
以及服务.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));
}
}
我也试过清理缓存。
很抱歉发这么长的信,提前谢谢你。
最佳答案
问题在于:
// 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。