Reading list Switch to dark mode

    Creating Services in Prestashop 1.7

    Updated 24 February 2022

    In this blog, we are going to learn how to setup namespace and define services in Prestashop and also, how to get the service in symfony container.

    Service container contains services, which is a special PHP object that perform some task which is complete in itself.

    Let’s understand the entire process to create a service in prestashop with the help of taking an example as given below.

    First of all, we will setup a namespace PositiveIntegerService. In this class, we will define a method checkPositiveNumber() which will return true for the positive number and false for negative number.

    Create Symfony service

    Start your headless eCommerce
    Find out More
    // modules/mymodule/src/PositiveIntegerService.php
    namespace Webkul\MyModule;
    class PositiveIntegerService {
        private $number;
        * @param string $number
        public function __construct($number) {
            $this->number = $number;
        * check number is positive
        * @return bool
        public function checkPositiveNumber() {
            return ((int)$this->number) > 0 ? true : false;

    So, now you have successfully setup your namespace. Now you can define your services in the config/services.yml file of your module.

    # mymodule/config/services.yml
            public: true
            class: Webkul\MyModule\PositiveIntegerService
               - "12"

    So, now you can get your service from Symfony container in which you have to create the object of service.

    // modules/mymodule/src/Controller/WkTestServiceController.php
    namespace Webkul\MyModule\WkTestServiceController;
    use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
    class WkTestServiceController extends FrameworkBundleAdminController
        public function checkServiceAction()
            // get your Service
            $service = $this->get('Webkul.mymodule.PositiveIntegerService');
            return $service->checkPositiveNumber();

    In this example we have created a object of the service and call its method checkPositiveNumber().

    This is how we can setup the namespace and create the services in Prestashop modules and get the service in symfony container.

    That’s all.

    This is a basic example in which we just created a basic service. But we can create multiple namespaces and use them in symfony containers.

    In a module, Symfony services definition can be modified in a module, by overriding PrestaShop symfony services.

    If any issue or doubt please feel free to mention it in the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected].

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home