简体   繁体   English

添加定制服务-Symfony; Sylius

[英]Add custom Services - Symfony ; Sylius

I don't understand Why I can't create custom services. 我不明白为什么我不能创建自定义服务。 I get errors with the both technic. 两种技术都出现错误。 I don't find anything about that in your doc. 我在您的文档中找不到任何关于此的信息。

在此处输入图片说明

# 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) {
    }

}

Case 1 : (Pass PhpHelper in the __construct function) 情况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*/

Error 1 : Catchable Fatal Error: Argument 1 passed to JDF\\CsvTreatmentBundle\\Controller\\ImportController::__construct() must be an instance of JDF\\Utils\\PhpHelper, none given, called in C:\\kitutilitaire\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver.php on line 202 and defined 500 Internal Server Error - ContextErrorException 错误1:可捕获的致命错误:传递给JDF \\ CsvTreatmentBundle \\ Controller \\ ImportController :: __ construct()的参数1必须是JDF \\ Utils \\ PhpHelper的实例,没有给出,在C:\\ kitutilitaire \\ vendor \\ symfony \\ symfony \\中调用src \\ Symfony \\ Component \\ HttpKernel \\ Controller \\ ControllerResolver.php在第202行并定义了500 Internal Server Error-ContextErrorException

Case 2 (just use get() controller method) : 情况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*/

Error 2 : Attempted to load class "PhpHelper" from namespace "JDF\\Utils". 错误2:尝试从名称空间“ JDF \\ Utils”加载类“ PhpHelper”。 Did you forget a "use" statement for another namespace? 您是否忘记了另一个名称空间的“使用”语句?

Stack Trace 堆栈跟踪

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

EDIT : composer.json autoload 编辑: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"]
},

Thank in advence for your help. 感谢您的帮助。

Controllers do not get any injection by default. 默认情况下,控制器不进行任何注入。 They have $this->container always available to get to all your services. 他们总是提供$this->container来获取您的所有服务。

So nothing more to do than: 因此,除了:

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

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

}

FYI: The cache file appDevDebugProjectContainer is auto generated and of no significance to your problem. 仅供参考:缓存文件appDevDebugProjectContainer是自动生成的,对您的问题不重要。

I've solved the problem with change my composer.json. 我已经通过更改composer.json解决了问题。

For can use $this->container->get('jdf.utils.phphelper'); 对于可以使用$this->container->get('jdf.utils.phphelper'); the all good code is : 所有好的代码是:

# 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*/

AND IMPORTANT : The composer.json : 重要提示:composer.json:

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

And the CLI command : php composer.phar dump-autoload 和CLI命令: php composer.phar dump-autoload

Thank to colburton for this time and interest at my issue. 感谢colburton这段时间和对我的问题的关注。

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

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