2

I am new to Magento 2 and trying to do a simple observer and event function for frontend (global to all module)

My understanding is that controller_action_predispatch will be run before anything else.

Expectation: When I run any frontend site, eg: http://www.magentosite.com/xxx, an alert box will be displayed before any other action. when the user clicks ok on the alert box, only the page will be displayed.

But I can't see an alert box there. Hope someone can hint me which part did I implement wrongly or did I missing execute any command?

Update: Do I need to register my module (eg: add registration.php file)??? because I only actually have events.xml file and TestObserver.php inside the module name only currently?

app/code/Vendorname/Modulename/etc/frontend/events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="test" instance="Vendorname\Modulename\Observer\TestObserver" />
    </event>
</config>

app/code/Vendorname/Modulename/Observer/TestObserver.php

<?php
    namespace Vendorname\Modulename\Observer\TestObserver;

    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;

    class TestObserver implements ObserverInterface
    {
        public function execute(Observer $observer)
        {
            echo "<script>javascript: alert('test msgbox')></script>";
        }
    }
?>

then run

php bin/magento cache:clean

php bin/magento cache:flush
2
  • namespace Vendorname\Modulename\Observer\TestObserver it into namespace Vendorname\Modulename\Observer; Commented Jan 11, 2018 at 3:48
  • Yes, you need to register your module. Module must have registration.php and module.xml Or if you have only one or two files you can do with other custom modules. Commented Jan 11, 2018 at 5:26

5 Answers 5

2

Here is your final module:

app/code/Vendorname/Modulename/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendorname_Modulename',
    __DIR__
);

app/code/Vendorname/Modulename/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendorname_Modulename" setup_version="1.0.0">
    </module>
</config>

app/code/Vendorname/Modulename/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="test" instance="Vendorname\Modulename\Observer\TestObserver" />
    </event>
</config>

app/code/Vendorname/Modulename/Observer/productSaveAfter.php

<?php

namespace Vendorname\Modulename\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class TestObserver implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        echo "<script>javascript: alert('test msgbox')></script>";
        echo "Testing"; exit;
    }
}

Now run this commands

php bin/magento setup:upgrade

Remove /var for magento 2 and remove /var and /generated for Magento 2.2

0
0

Your namespace is wrong. It should look like:

<?php

namespace Vendorname\Modulename\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class TestObserver implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        echo "<script>javascript: alert('test msgbox')></script>";
    }
}

Also, don't close out your class with a closing php tag ?>

I am not sure your js is going to fire as it going to be echo'd out to the page before anything else. If you must echo something to the page, just do something you will see, such as echo 'Hola!'; die;

To answer the remaining question, you need to create an actual module and this would consist of a minimum of the following files:

Vendorname/Modulename/composer.json
Vendorname/Modulename/registration.php
Vendorname/Modulename/etc/module.xml
0

Replace your observer with following:

app/code/Vendorname/Modulename/Observer/TestObserver.php

<?php
namespace Vendorname\Modulename\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class TestObserver implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        echo 'any message'; 
        echo "<script>javascript: alert('test msgbox')></script>";
        die();
    }
}
0

In magento2's, a module should have below files:

  • registration.php
  • etc/module.xml
  • composer.json

But other seems ok in my end

0

Your Observer looks fine but I think your module is incomplete or it has typos

Use Mage2Gen to generator your basic module and apply your observer execute method

See the following example module

https://mage2gen.com/load/347970d6-a607-4773-b65a-467d6528bfce

Apply this method

public function execute(Observer $observer)
{
    echo "<script>javascript: alert('test msgbox')></script>";
    echo "Testing"; exit;
}

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.