简体   繁体   English

Symfony2:我的第一个服务

[英]Symfony2: my first service

I'm struggling with the error: 我正在努力解决这个错误:

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.

But that class is defined in exactly that file! 但是那个类完全在那个文件中定义的!

/src/ElectricAnimal/CardsinthepostBundle/Services/MyService.php: /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;
    }

}

In my controller I have: 在我的控制器中我有:

<?php    
class DefaultController extends Controller
{

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

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

    }
}
?>

Additional info: 附加信息:

/src/ElectricAnimal/CardsinthepostBundle/DependencyInjection/ElectricAnimalCardsinthepostExtension.php: /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: /src/ElectricAnimal/CardsinthepostBundle/Resources/config/services.yml:

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

My answer: 我的答案:

MyService.php was using slightly the wrong namespace. MyService.php稍微使用了错误的命名空间。 Needed: 需要:

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

instead of 代替

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

Symfony2 naming drives me round the bend sometimes! Symfony2命名有时让我绕弯道!

Had already constructed question so hope useful for someone to have this answer. 已经构建了问题所以希望有人能够得到这个答案。

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

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