PrestaShop / PrestaShop

PrestaShop is the universal open-source software platform to build your e-commerce solution.
https://www.prestashop-project.org/
Other
8.23k stars 4.81k forks source link

Cannot use symfony services in front end hook #29013

Open musaffar-patel opened 2 years ago

musaffar-patel commented 2 years ago

Prerequisites

Describe the bug and add screenshots

When attempting to use any symfony service in a front end hook an error is generated,

For example:

    public function hookDisplayFooterProduct(array $params): string
    {
        $service = $this->get('twig');
    }

generates an error: You have requested a non-existent service "twig".

This applies to any services, such as router or custom module services. They seem to work ok in back office hooks such as getContent()

Expected behavior

It should be possible to use symfony services as router or twig in front end hooks in the module class.

Steps to reproduce

  1. Create a custom module
  2. implement a front end hook such as hookDisplayFooterProduct
  3. Attempt to utililse a symfony service, for example: $router = $this->get('router');
  4. According to the developer docs this should work.

PrestaShop version(s) where the bug happened

1.7.8.6

PHP version(s) where the bug happened

No response

If your bug is related to a module, specify its name and its version

No response

hibatallahAouadni commented 2 years ago

Hello @musaffar-patel

Could you please provide a custom module that implement the hookDisplayFooterProduct hook in FO to help us reproducing the issue ASAP :pray: Waiting for your feedback.

Thanks!

musaffar-patel commented 2 years ago

Attached ...

samplemodule.zip

LouisAUTHIE commented 2 years ago

I’m afraid that you go wrong, the only services available in the front are doctrine related ones cf https://devdocs.prestashop.com/1.7/modules/concepts/services/

tom-combet commented 2 years ago

Yep, Twig is not usable in front-office yet.

musaffar-patel commented 2 years ago

That's a shame, it means half my module uses Symfony services such as routing, twig etc for the back office, while front end related must use legacy code. Nevertheless, thanks for clarifying this.

LouisAUTHIE commented 2 years ago

Indeed it will be better with symfony on the front, but they don’t want to migrate everything at the same time. It should have been done with the new 1.7 themes but they decided to keep smarty because they wanted to keep the modules compatibles with the old front. Smarty and twig at the same time is not possible. Be patient and it will come

tom-combet commented 2 years ago

You can still declare your services (and those from the core too) for the front: https://devdocs.prestashop.com/1.7/modules/concepts/services/#environments. And I managed to use CQRS in front too, by building my own CommandBus and injecting the necessary handlers.

But yea, for bundles like twig, it's not possible atm and I think it's even not worth to try. We have to wait. 😢

musaffar-patel commented 2 years ago

@tom-combet thanks for the suggestion. It was mainly twig and the router I was hoping to use,

Makes sense to migrate to Symfony incrementally. Will settle for using only in the BO for now.

mickaelandrieu commented 2 years ago

maybe using SymfonyContainer::getInstance() to rely on the global container ? Look into FoP modules, I may have contributed an exemple...

musaffar-patel commented 2 years ago

@mickaelandrieu Thanks for the suggestion, I already attempted this and this method again only works in the back office hooks.

musaffar-patel commented 2 years ago

Another related issue I found is that, if a module uses Twig in the back office but smarty in the front office hooks, the following errors are then produced in the BO:

Warning: filemtime(): stat failed for E:\websites\prestashop\178\wwwpg\classes\PrestaShopAutoload.php(143) : eval()'d code

So it seems it's not plain sailing for modules to yet begin using Symfony code, at least the twig templating service anyway.

LouisAUTHIE commented 2 years ago

Chez your folder rights

sLorenzini commented 2 years ago

Hello @musaffar-patel,

I reproduce the issue with PrestaShop version 1.7.8.6/1.7.7.8. After the module installation you provide, the error is displayed:

Capture d’écran 2022-07-11 à 15 00 28

I'll add this to the backlog so it can be fixed.

Please be aware that some issues might take a very long time to be resolved. If this one is important to you and you cannot wait for it to be fixed on the project’s own time, we strongly suggest you consider contacting a professional to help you.

If you fix the issue on your end, please contribute it back to the project. Remember that the more people contribute, the better PrestaShop becomes for everyone.

Thank you

tom-combet commented 2 years ago

@sLorenzini I think it's not a real issue atm since the front migration to twig is not planned yet?

musaffar-patel commented 2 years ago

Chez your folder rights

@LouisAUTHIE it's not folder rights, folder rights are correct. This only happens when I have a front end hook using smarty and a BO hook using twig. If I remove the Front end hook which uses smarty, the twig BO hooks work again.

matks commented 1 year ago

Hello guys, for your information Symfony is coming to the front end 😄 https://github.com/PrestaShop/PrestaShop/pull/32719

musaffarpatel commented 1 year ago

@matks This is positive news. While on the topic of Symfony, would you happen to know if it's possible to use Symfony services such as forms, grids, routes etc in the "displayAdminProductsExtra" hook (modules tab when editing a product) in the latest version of Prestashop?

tom-combet commented 1 year ago

@matks This is positive news. While on the topic of Symfony, would you happen to know if it's possible to use Symfony services such as forms, grids, routes etc in the "displayAdminProductsExtra" hook (modules tab when editing a product) in the latest version of Prestashop?

I did it, where are you struggling? Your module can retrieve services with get method and Symfony is available in back-office, so you have everything to make it work.

musaffarpatel commented 1 year ago

@tom-combet I couldn't get a simple route to work under the displayAdminProductsExtra hook. Do you have a example module I could look at?

tom-combet commented 1 year ago

@musaffarpatel Contact me on PrestaShop Slack if you don't mind, so we don't spoil this issue thread 😉 I feel like it kinda becomes a support conversation.

musaffarpatel commented 1 year ago

@tom-combet sure, sent