{"id":323652,"date":"2022-02-24T09:32:21","date_gmt":"2022-02-24T09:32:21","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=323652"},"modified":"2022-02-24T13:53:27","modified_gmt":"2022-02-24T13:53:27","slug":"creating-services-in-prestashop-1-7","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/","title":{"rendered":"Creating Services in Prestashop 1.7"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>Service container contains services, which is a special PHP object that perform some task which is complete in itself.<\/p>\n\n\n\n<p>Let\u2019s understand the entire process to create a service in prestashop with the help of taking an example as given below.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Create Symfony service<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\n\/\/ modules\/mymodule\/src\/PositiveIntegerService.php\nnamespace Webkul\\MyModule;\nclass PositiveIntegerService {\n    private $number;\n    \/**\n    * @param string $number\n    *\/\n    public function __construct($number) {\n        $this-&gt;number = $number;\n    }\n    \/**\n    * check number is positive\n    * @return bool\n    *\/\n    public function checkPositiveNumber() {\n        return ((int)$this-&gt;number) &gt; 0 ? true : false;\n    }\n}<\/pre>\n\n\n\n<p>So, now you have successfully setup your namespace. Now you can define your services in the config\/services.yml file of your module.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># mymodule\/config\/services.yml\nservices:\n    _defaults:\n        public: true\n    Webkul.mymodule.PositiveIntegerService:\n        class: Webkul\\MyModule\\PositiveIntegerService\n        arguments:\n           - \"12\"<\/pre>\n\n\n\n<p>So, now you can get your service from Symfony container in which you have to create the object of service.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\n\/\/ modules\/mymodule\/src\/Controller\/WkTestServiceController.php\nnamespace Webkul\\MyModule\\WkTestServiceController;\nuse PrestaShopBundle\\Controller\\Admin\\FrameworkBundleAdminController;\nclass WkTestServiceController extends FrameworkBundleAdminController\n{\n    public function checkServiceAction()\n    {\n        \/\/ get your Service\n        $service = $this-&gt;get('Webkul.mymodule.PositiveIntegerService');\n        return $service-&gt;checkPositiveNumber();\n    }\n}<\/pre>\n\n\n\n<p>In this example we have created a object of the service and call its method <strong><code>checkPositiveNumber()<\/code><\/strong>.<\/p>\n\n\n\n<p>This is how we can setup the namespace and create the services in Prestashop modules and get the service in symfony container.<\/p>\n\n\n\n<p>That\u2019s all.<\/p>\n\n\n\n<p>Note:<br>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.<\/p>\n\n\n\n<p>In a module, Symfony services definition can be modified in a module, by overriding PrestaShop symfony services.<\/p>\n\n\n\n<p>If any issue or doubt please feel free to mention it in the comment section. <\/p>\n\n\n\n<p>I would be happy to help.<\/p>\n\n\n\n<p>Also, you can explore our&nbsp;<a href=\"https:\/\/webkul.com\/prestashop-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">PrestaShop Development Services<\/a>&nbsp;and a large range of quality&nbsp;<a href=\"https:\/\/store.webkul.com\/PrestaShop-Extensions.html\">PrestaShop Modules<\/a>.<\/p>\n\n\n\n<p>For any doubt contact us at&nbsp;<a href=\"mailto:support@webkul.com\" target=\"_blank\" rel=\"noreferrer noopener\">support@webkul.com<\/a>.<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s understand the entire process to create a service <a href=\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":417,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-323652","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Services in Prestashop 1.7 - Webkul Blog<\/title>\n<meta name=\"description\" content=\"How to setup namespace for services and declare Symfony services and get the services in symfony container in Prestashop 1.7\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Services in Prestashop 1.7 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"How to setup namespace for services and declare Symfony services and get the services in symfony container in Prestashop 1.7\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-24T09:32:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-24T13:53:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Priyanka\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Priyanka\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\"},\"author\":{\"name\":\"Priyanka\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d81657917c9d2449fce06c946d537a20\"},\"headline\":\"Creating Services in Prestashop 1.7\",\"datePublished\":\"2022-02-24T09:32:21+00:00\",\"dateModified\":\"2022-02-24T13:53:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\"},\"wordCount\":282,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\",\"url\":\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\",\"name\":\"Creating Services in Prestashop 1.7 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2022-02-24T09:32:21+00:00\",\"dateModified\":\"2022-02-24T13:53:27+00:00\",\"description\":\"How to setup namespace for services and declare Symfony services and get the services in symfony container in Prestashop 1.7\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating Services in Prestashop 1.7\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d81657917c9d2449fce06c946d537a20\",\"name\":\"Priyanka\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/218221f8558f5eb0cd7387ce7adfef57060d332578f3a2328e784468cdc569b5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/218221f8558f5eb0cd7387ce7adfef57060d332578f3a2328e784468cdc569b5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Priyanka\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/priyanka-ps810\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating Services in Prestashop 1.7 - Webkul Blog","description":"How to setup namespace for services and declare Symfony services and get the services in symfony container in Prestashop 1.7","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/","og_locale":"en_US","og_type":"article","og_title":"Creating Services in Prestashop 1.7 - Webkul Blog","og_description":"How to setup namespace for services and declare Symfony services and get the services in symfony container in Prestashop 1.7","og_url":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2022-02-24T09:32:21+00:00","article_modified_time":"2022-02-24T13:53:27+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Priyanka","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Priyanka","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/"},"author":{"name":"Priyanka","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d81657917c9d2449fce06c946d537a20"},"headline":"Creating Services in Prestashop 1.7","datePublished":"2022-02-24T09:32:21+00:00","dateModified":"2022-02-24T13:53:27+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/"},"wordCount":282,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/","url":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/","name":"Creating Services in Prestashop 1.7 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2022-02-24T09:32:21+00:00","dateModified":"2022-02-24T13:53:27+00:00","description":"How to setup namespace for services and declare Symfony services and get the services in symfony container in Prestashop 1.7","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/creating-services-in-prestashop-1-7\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Creating Services in Prestashop 1.7"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d81657917c9d2449fce06c946d537a20","name":"Priyanka","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/218221f8558f5eb0cd7387ce7adfef57060d332578f3a2328e784468cdc569b5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/218221f8558f5eb0cd7387ce7adfef57060d332578f3a2328e784468cdc569b5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Priyanka"},"url":"https:\/\/webkul.com\/blog\/author\/priyanka-ps810\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/323652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/417"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=323652"}],"version-history":[{"count":12,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/323652\/revisions"}],"predecessor-version":[{"id":323765,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/323652\/revisions\/323765"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=323652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=323652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=323652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}