2012-02-26 33 views
3

我與錯誤掙扎:Symfony2的:我的第一個服務

The autoloader expected class "ElectricAnimal\CardsinthepostBundle\Services\MyService" to be 
defined in file 
"/vhosts/domains/cardsinthepost.com/public/app/../src/ElectricAnimal/CardsinthepostBundle/Services/MyService.php". 
You probably have a typo in the namespace or the class name. 

但該類都完全按照文件中定義

/src/ElectricAnimal/CardsinthepostBundle/Services/MyService.php:

<?php 

// Bundle/ElectricAnimalCardsinthepost/Services/MyService.php 
namespace Bundle\ElectricAnimalCardsinthepost\Services; 

class MyService 
{ 

    public function __construct() 
    { 

    } 

    public function sum($n1, $n2) { 
     return $n1 + $n2; 
    } 

} 

在我的控制,我有:

<?php  
class DefaultController extends Controller 
{ 

    public function indexAction() 
    { 
     $number = $this->get('my_service')->sum(12, 37); 

     return new Response('<pre>' . $number . '</pre>'); 

    } 
} 
?> 

附加信息:

/src目錄/ ElectricAnimal/CardsinthepostBundle /DependencyInjection/ElectricAnimalCardsinthepostExtension.php:

<?php 

namespace ElectricAnimal\CardsinthepostBundle\DependencyInjection; 

use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\Config\FileLocator; 
use Symfony\Component\HttpKernel\DependencyInjection\Extension; 
use Symfony\Component\DependencyInjection\Loader; 

/** 
* This is the class that loads and manages your bundle configuration 
* 
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} 
*/ 
class ElectricAnimalCardsinthepostExtension extends Extension 
{ 
    /** 
    * {@inheritDoc} 
    */ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 

     $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 
     $loader->load('services.yml'); 
    } 
} 

/src/ElectricAnimal/CardsinthepostBundle/Resources/config/services.yml:

services: 
    my_service: 
     class: ElectricAnimal\CardsinthepostBundle\Services\MyService 

回答

0

我的回答:

MyService.php用稍微錯了命名空間。需要:的

// Bundle/ElectricAnimal/Cardsinthepost/Services/MyService.php 
namespace ElectricAnimal\CardsinthepostBundle\Services; 

代替

// Bundle/ElectricAnimalCardsinthepost/Services/MyService.php 
namespace Bundle\ElectricAnimalCardsinthepost\Services; 

Symfony2的命名使我圓有時彎曲!

已經構建的問題,所以希望有人有這個答案。