1

I have created an observer listening to the event controller_action_predispatch on a clean Magento 2.4.2 instance, and I tried to get the module name and full action name from within the observer. It worked well for all pages except the product pages.

For example, for the registration page:

$observer->getEvent()->getRequest()->getModuleName() // it gives "customer"
$observer->getEvent()->getRequest()->getFullActionName() // it gives "customer_account_create"

and for shopping cart page:

$observer->getEvent()->getRequest()->getModuleName() // it gives "checkout"
$observer->getEvent()->getRequest()->getFullActionName() // it gives "checkout_cart_index"

but when I tried to capture the above info for the product pages

$observer->getEvent()->getRequest()->getModuleName() // it gives ""
$observer->getEvent()->getRequest()->getFullActionName() // it gives "__" (2 underscores)
5
  • can you check something for me? does the observer retrieve the proper values if you disable the FPC?
    – Diana
    Mar 10, 2021 at 7:00
  • @DianaBotean oh, this time I could log the same empty response at the very beginning, followed by the expected "catalog_product_view" when FPC is disabled. (So there seems to be a redirection?) So this is the expected behavior? or simply a bug?
    – d.yuk
    Mar 10, 2021 at 7:07
  • the request is not processed the same way when it's served from the cache. it would basically just grab the content from the cache and not go to the server and build the page from scratch.
    – Diana
    Mar 10, 2021 at 7:16
  • 1
    @DianaBotean oh, I just realized something. It seems like it actually is not related to the FPC. I didn't mention the purpose of my observer in the original thread. I am targeting URLs other than product pages, and try to redirect access to all those pages to 404. But i found that all access to product pages would go through an "empty controller action" (?) first, before it could reach the catalog_product_view controller action. That's why it seems like it is giving me a different response than other pages. Anyway thanks for your help that gave me insights to what went wrong.
    – d.yuk
    Mar 10, 2021 at 7:22
  • no problem :) however, i added an answer below, maybe it helps you in your quest :) good luck!
    – Diana
    Mar 10, 2021 at 7:26

2 Answers 2

2

By the looks of it, the pages that work with your approach are pages which are never cached, however the product page is and the request is processed differently (as confirmed by you that the data is properly retrieved when you disable FPC).

I found in the Magento core the idea of using a plugin that could help you. You will basically attach a plugin on the class that processes the request relative to page cache. So check this snippet from vendor/magento/module-page-cache/etc/di.xml where this plugin is registered:

<type name="Magento\Framework\App\PageCache\Cache">
    <plugin name="fpc-type-plugin" type="Magento\PageCache\Model\App\PageCachePlugin"/>
</type>

You could create a plugin of your own and inside it, add a beforeLoad() method relative to Magento\Framework\App\PageCache\Cache's load() method. There you will probably to check with a debugger which is the data you can access and perform your logic based on the page/ URL you are requesting.

0

Try this

public function __construct(
    ...
    \Magento\Framework\App\Request\Http $request
) {
    ...
    $this->_request = $request;
}

 $this->_request->getFullActionName()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.