symfony - 服务不存在?交响乐2

标签 symfony service

我正在尝试让这个简单的通知服务正常工作,但我一点也不高兴。我以前从未使用过 symfony 中的服务,所以我可能会忽略一些非常基本的东西,但是这对我来说似乎都是正确的,所以我有点在这里用头撞墙。

我已经包含了与服务有关的所有内容,非常感谢您的帮助!

堆栈跟踪:

[1] Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "game.notify".
    at n/a
        in D:\web\www\mygame\app\bootstrap.php.cache line 1968

    at Symfony\Component\DependencyInjection\Container->get('game.notify')
        in D:\web\www\mygame\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php line 252

    at Symfony\Bundle\FrameworkBundle\Controller\Controller->get('game.notify')
        in D:\web\www\mygame\src\Game\MainBundle\Controller\PageController.php line 10

    at Game\MainBundle\Controller\PageController->indexAction()
        in  line 

    at call_user_func_array(array(object(PageController), 'indexAction'), array())
        in D:\web\www\mygame\app\bootstrap.php.cache line 2843

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in D:\web\www\mygame\app\bootstrap.php.cache line 2817

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in D:\web\www\mygame\app\bootstrap.php.cache line 2946

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in D:\web\www\mygame\app\bootstrap.php.cache line 2248

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in D:\web\www\mygame\web\app_dev.php line 28

通知 Controller :

位于:Game/MainBundle/Controller/NotifyController.php

<?php
namespace Game\MainBundle\Controller;

class NotifyController
{
    private $defaults
        = array(
            "type" => "flash",
        ),
        $flashes = array();
    /**
    * @param \Symfony\Component\HttpFoundation\Session\Session $session
    */
    public function __construct(\Symfony\Component\HttpFoundation\Session\Session $session)
    {
        $this->session = $session;
    }
    /**
    * Depending on the supplied type argument, add the values
    * to the session flashBag or $this->flashes
    *
    * @param string $name
    * @param array $arguments
    */
    public function add($name, array $arguments = array())
    {
        $arguments += $this->defaults;
        // If the type is flash then add the values to the session flashBag
        if ($arguments["type"] === "flash") {
            $this->session->getFlashBag()->add($name, $arguments);
        }
        // Otherwise if its instant then add them to the class variable $flashes
        elseif ($arguments["type"] === "instant") {
            // We want to be able to have multiple notifications of the same name i.e "success"
            // so we need to add each new set of arguments into an array not overwrite the last
            // "success" value set
            if (!isset($this->flashes[$name])) {
                $this->flashes[$name] = array();
            }
            $this->flashes[$name][] = $arguments;
        }
    }

    /**
    * Check the flashBag and $this->flashes for existence of $name
    *
    * @param $name
    *
    * @return bool
    */
    public function has($name)
    {
        if($this->session->getFlashBag()->has($name)){
            return true;
        } else {
            return isset($this->flashes[$name]);
        }
    }
    /**
    * Search for a specific notification and return matches from flashBag and $this->flashes
    *
    * @param $name
    *
    * @return array
    */
    public function get($name)
    {
        if($this->session->getFlashBag()->has($name) && isset($this->flashes[$name])){
            return array_merge_recursive($this->session->getFlashBag()->get($name), $this->flashes[$name]);
        } elseif($this->session->getFlashBag()->has($name)) {
            return $this->session->getFlashBag()->get($name);
        } else {
            return $this->flashes[$name];
        }
    }
    /**
    * Merge all flashBag and $this->flashes values and return the array
    *
    * @return array
    */
    public function all()
    {
        return array_merge_recursive($this->session->getFlashBag()->all(), $this->flashes);
    }

}

NotifyExtension.php

位于:Game/MainBundle/DependencyInjection/NotifyExtension.php

<?php

namespace Game\MainBundle\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 NotifyExtension 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');
    }
}

配置.php

位于:Game/MainBundle/DependencyInjection/Configuration.php

<?php

namespace Game\MainBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your app/config files
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('game_main');

        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.

        return $treeBuilder;
    }
}

服务.yml 位于:Game/MainBundle/Resources/Config/services.yml

parameters:
    game.notify.class:  Game\MainBundle\Controller\NotifyController

services:
    game.notify:
        class: "%game.notify.class%"
        arguments:
            session: @session

PageController.php

位于:Game/MainBundle/Controller/PageController.php

<?php
namespace Game\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class PageController extends Controller
{
    public function indexAction()
    {
        $notify = $this->get("game.notify");
        $notify->add("test", array("type" => "instant", "message" => "This is awesome"));
        if ($notify->has("test")) {
            return array("notifications" => $notify->get("test"));
        }
        return $this->render('GameMainBundle:Page:index.html.twig');
    }
}

最佳答案

根据您对我的第一条评论的回答,由于不遵循扩展类的命名约定,您的服务似乎永远不会被加载。

如果您的 bundle 有 GameMainBundle,那么您的扩展应该有 GameMainExtension。

更多信息请点击:http://symfony.com/doc/current/cookbook/bundles/best_practices.html

加载 services.yml 后,您可能仍然会遇到一些问题。将您的服务称为 Controller 有点不标准。但看看会发生什么。

关于symfony - 服务不存在?交响乐2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20333519/

相关文章:

android - 服务停止时如何执行操作?

Android 在后台服务中打开 WebView 并捕获屏幕截图

linux - systemd 一个接一个地启动服务停止问题

java - 网络服务 : error in code or connection

android - 使用什么在后台执行网络操作?

forms - Symfony2 无效形式且无错误

php - 为什么我的级联不工作? - 违反完整性约束

php - Symfony2 形式转 JSON 结构

php - 避免注入(inject)服务容器而不是单个服务的技术原因是什么?

symfony - 带有自定义 JSON 主体的 Api 平台自定义 Controller