简体   繁体   中英

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)

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

Case 2 (just use get() controller method) :

// 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". 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

"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.

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.

I've solved the problem with change my composer.json.

For can use $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 :

"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

Thank to colburton for this time and interest at my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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