【问题标题】:Add custom Services - Symfony ; Sylius添加自定义服务 - Symfony;西柳斯
【发布时间】:2017-07-19 16:09:22
【问题描述】:

我不明白为什么我不能创建自定义服务。我在这两种技术上都遇到了错误。我在您的文档中没有找到任何关于此的内容。

# app/config/services.yml
services:

    jdf.utils.phphelper:
        class: JDF\Utils\PhpHelper



// src/JDF/Utils/PhpHelper.php

namespace JDF\Utils;

class PhpHelper
{

    /**
     * [pdebug description]
     * @param  string  $var         The string to beautiful show
     * @param  string  $msg         Description of the $var
     * @param  integer $displayNone 
     * @return echo pre print_r $var string
     */
    public function pdebug ($var, $msg = '', $displayNone = 0) {
    }

}

案例1:(在__construct函数中传递PhpHelper)

// src/JDF/CsvTreatmentBundle\Controller/ImportController

namespace JDF\CsvTreatmentBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use JDF\Utils\PhpHelper;
use Psr\Log\LoggerInterface;

/**
* 
*/
class ImportController extends Controller {

    function __construct(
                            PhpHelper $PhpHelper
                        ) {
    }

    public function indexAction() {
        //$test = $this->container->get('jdf.utils.phphelper');
        return new Response('<hr>');
    }

} /*End of class*/

错误 1: 可捕获的致命错误:传递给 JDF\CsvTreatmentBundle\Controller\ImportController::__construct() 的参数 1 必须是 JDF\Utils\PhpHelper 的实例,没有给出,在 C:\kitutilitaire\vendor\symfony\symfony\src\Symfony 中调用\Component\HttpKernel\Controller\ControllerResolver.php 在第 202 行并定义 500 内部服务器错误 - ContextErrorException

案例 2(只需使用 get() 控制器方法):

// src/JDF/CsvTreatmentBundle\Controller/ImportController

    namespace JDF\CsvTreatmentBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;

    use JDF\Utils\PhpHelper;
    use Psr\Log\LoggerInterface;

    /**
    * 
    */
    class ImportController extends Controller {

        function __construct(
                                //PhpHelper $PhpHelper
                                // LoggerInterface $logger
                            ) {
        }

        public function indexAction() {

            $test = $this->container->get('jdf.utils.phphelper');
            // $logger = $this->container->get('logger');

            return new Response('<hr>');
        }

    } /*End of class*/

错误 2: 尝试从命名空间“JDF\Utils”加载类“PhpHelper”。 您是否忘记了另一个命名空间的“使用”语句?

堆栈跟踪

in var\cache\dev\appDevDebugProjectContainer.php at line 3555  -
         */
        protected function getJdf_Utils_PhphelperService()
        {
            return $this->services['jdf.utils.phphelper'] = new \JDF\Utils\PhpHelper();
        }
        /**

编辑:composer.json 自动加载

"autoload": {
    "psr-4": {
        "AppBundle\\": "src/AppBundle/",
        "JDF\\CsvTreatmentBundle\\": "src/JDF/CsvTreatmentBundle/",
        "JDF\\Utils\\": "src/JDF/Utils/PhpHelper"
    },
    "classmap": ["app/AppKernel.php", "app/AppCache.php"]
},

提前感谢您的帮助。

【问题讨论】:

    标签: php symfony sylius


    【解决方案1】:

    默认情况下,控制器不会进行任何注入。他们有$this-&gt;container 随时可以使用您的所有服务。

    所以别无他法:

    class ImportController extends Controller {
        public function indexAction() {
            $test = $this->container->get('jdf.utils.phphelper');
            // $logger = $this->container->get('logger');
    
            return new Response('<hr>');
        }
    
    }
    

    仅供参考:缓存文件appDevDebugProjectContainer 是自动生成的,对您的问题没有意义。

    【讨论】:

    • 如何在 services.yml 中定义服务?我已经编写了我的 services.yml(第一个代码展示)
    • @Xenofexs:你是对的,我的错。我已经更新了答案。
    • 感谢您的更新,但这是我在案例 2 中所做的(已编辑,我已删除非自愿行)使用此代码,我收到错误 2(尝试从命名空间加载类“PhpHelper”) JDF\Utils”。您是否忘记了另一个命名空间的“use”语句?)还有行错误:return $this-&gt;services['jdf.utils.phphelper'] = new \JDF\Utils\PhpHelper();
    • 我已经发布了整个班级!。重点是被移除的构造函数。
    • 这是composer.json ...无论如何感谢您的时间:)。
    【解决方案2】:

    我已经通过更改我的 composer.json 解决了这个问题。

    对于可以使用$this-&gt;container-&gt;get('jdf.utils.phphelper');,所有好的代码是:

    # app/config/services.yml
    services:
    
        jdf.utils.phphelper:
            class: JDF\Utils\PhpHelper
    // src/JDF/Utils/PhpHelper.php
    
    namespace JDF\Utils;
    
    class PhpHelper {} 
    
    // src/JDF/CsvTreatmentBundle\Controller/ImportController
    
    namespace JDF\CsvTreatmentBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    
    use JDF\Utils\PhpHelper;
    
    /**
    * 
    */
    class ImportController extends Controller {
    
        public function indexAction() {
    
            $test = $this->container->get('jdf.utils.phphelper');
    
            return new Response('<hr>');
        }
    
    } /*End of class*/
    

    重要提示:composer.json:

    "autoload": {
        "psr-4": {
            "JDF\\CsvTreatmentBundle\\": "src/JDF/CsvTreatmentBundle/",
            "JDF\\Utils\\": "src/JDF/Utils/"
        },
        "classmap": ["app/AppKernel.php", "app/AppCache.php"]
    },
    

    还有 CLI 命令:php composer.phar dump-autoload

    感谢 colburton 的这段时间和对我的问题的兴趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-03
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多