In blog Override a Symfony Controller we discuss about overriding Symfony controllers available in PrestaShop. Now suppose, we need to use classes in PrestaShop 1.7 Symfony controller, how we do that?
In this blog, we are going to learn how to use methods and function define in PrestaShop core classes, module main class file or from module classes.
There are following ways to use classes in overridden Symfony controllers :
- Use Method
- By Module Instance method
Let’s understand the entire process to override with the help of taking an example as given below.
Use Method
Just add below lines at the top of the Symfony Controller say DemoController.php
file after namespace to use PrestaShop classes.
namespace Webkul\Controller\Admin; use Configuration; use ModuleName; use Student; use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; use Symfony\Component\HttpFoundation\Request; class DemoController extends FrameworkBundleAdminController { $objConf = new Configuration(); <strong> or</strong> $objMod = new ModuleName(); <strong> or</strong> $objStu = new Student();
Here, use Configuration;
loads core Configuration class.use ModuleName;
load module main class file.use Student;
load Student class inside module classes folder.
Module Instance Method
This method is only used to load module main class function. Load module instance in Symfony controller as below :
$module = Module::getInstanceByName('modulename'); if (method_exists($module, 'getNewFunction')) { $result = $module->getNewFunction(); }
In this example we load module main class file and it’s method getNewFunction()
by creating module instance.
We can also implement use classes in PrestaShop 1.7 Symfony controller if we need to load method from other modules.
This is how we can use the classes in Symfony Controller in our module.
That’s all.
If you are facing any issues or doubts in the above process, please feel free to contact us through 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].
Be the first to comment.