What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hello all, can someone please advise me on the right way to add helper files in the modules? For ex

U048HPYF4UA
U048HPYF4UA Posts: 2 🧑🏻‍🚀 - Cadet
edited November 2022 in Help

Hello all,

can someone please advise me on the right way to add helper files in the modules? For example adding a MyModuleHelper.php with static helper functions in a module named MyModule. How would you do that? Thanks in advance, Sven

Comments

  • Alberto Reyer
    Alberto Reyer Lead Spryker Solution Architect / Technical Director Posts: 690 🪐 - Explorer

    Build a service for that and get it as a dependency where ever you use it. Calling something with static will make it hard to test and couple all using modules to exact these helper implementation.

    An example for a service would be the \Spryker\Service\UtilText\UtilTextService

    If your "helper" is only used inside the module it is part of create a class and inject it were needed into the constructor through the factory:

    <?php
    
    namespace Pyz\Zed\YourBundle\Business;
    
    interface HelperInterface {}
    
    class Helper implements HelperInterface {}
    
    class Using
    {
        public function __construct(HelperInterface $helper)
        {
            $this->helper = $helper;
        }
    
        public function doSomething()
        {
            $this->helper->doSomething();
        }
    }
    
    class YourBundleBusinessFactory
    {
        public function createUsing()
        {
            return new Using($this->createHelper());
        }
    
        public function createHelper()
        {
            return new Helper();
        }
    } 
    
  • Alberto Reyer
    Alberto Reyer Lead Spryker Solution Architect / Technical Director Posts: 690 🪐 - Explorer

    If you use a service your factory just looks a little different as you need to get the dependencies from the dependency provider:


  • U048HPYF4UA
    U048HPYF4UA Posts: 2 🧑🏻‍🚀 - Cadet

    Thank you very much @UL6DGRULR that helps a lot