简体   繁体   English

命名空间没找到类? Symfony2的

[英]Namespace not found class? symfony2

Newbie problem here, I've created a Bundle named MyServices with Sub folder MyBundle, 这里有新手问题,我用Sub文件夹MyBundle创建了一个名为MyServices的Bundle,

I've created a Service named myAWServ but as I'm calling $this->get('myAWS')->methodCall() I'm getting the following error : 我创建了一个名为myAWServ的服务但是因为我正在调用$this->get('myAWS')->methodCall()我收到以下错误:

CRITICAL - Fatal Error: Class 'MyServices\\MyBundle\\myAWS\\myAWS' not found CRITICAL - Uncaught PHP Exception Symfony\\Component\\Debug\\Exception\\ClassNotFoundException: "Attempted to load class "myAWS" from namespace "MyServices\\MyBundle\\myAWS". Did you forget a "use" statement for another namespace?" CRITICAL - 致命错误:类'MyServices \\ MyBundle \\ myAWS \\ myAWS'未找到CRITICAL - 未捕获PHP异常Symfony \\ Component \\ Debug \\ Exception \\ ClassNotFoundException:“尝试从命名空间”MyServices \\ MyBundle \\ myAWS“加载类”myAWS“。您是否忘记了另一个命名空间的“use”语句?“ at /home/sergio/Desktop/hello_symfony_heroku/app/cache/dev/appDevDebugProjectContainer.php line 1755 在/home/sergio/Desktop/hello_symfony_heroku/app/cache/dev/appDevDebugProjectContainer.php第1755行

I've reviewed all files a hundred times and can't find the problem the files are as it follow: 我已经审查了所有文件一百次,并且无法找到文件所遵循的问题:

<?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;
    }
}

The Root file created with the bundle (php app/console generate:bundle) 使用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
{
}

and the services.yml 和services.yml

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

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

I've got it loading in the AppKernel new MyServices\\MyBundle\\MyServicesMyBundle(), 我已经在AppKernel中加载了新的MyServices \\ MyBundle \\ MyServicesMyBundle(),

Currently im doing a simple call 目前我正在做一个简单的电话

<?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));

    }
}

I've also tried clearing the cache. 我也试过清除缓存。 Sorry for the long post, and thank you in advance. 对不起,很长的帖子,谢谢你提前。

The problem is here: 问题出在这里:

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

Your file's path is MyServices/MyBundle/Controller/myAWS.php but you must follow your namespace, so the correct path should be MyServices/MyBundle/myAWS/myAWS.php . 您的文件路径是MyServices/MyBundle/Controller/myAWS.php但您必须遵循您的命名空间,因此正确的路径应该是MyServices/MyBundle/myAWS/myAWS.php You should also check out the psr-4 specifications for autoloading . 您还应该查看自动加载psr-4规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM