RPC Calls

An RPC call will be made for every view rendered, allowing you to execute additional PHP code on any view owned by another package.

Create Listener

For every view rendered, a RPC call will be made to the routing key syrus.parse.PATH, with forward slashes being replaced with underscores within the path. For example, if parsing the template file products/phone.html, an RPC call would be made to the routing key: syrus.parse.products_phone.

Using that example, to execute additional PHP code on the /products/phone view, you would need to create a listener with the create listener CLI command. If you don't already have a package created to develop with, you may create one with the command:

apex package create demo

You may then create a listener with the command:

apex create listener demo parse-templates --routing-key syrus.parse

This will create a new file at /src/Demo/Listeners/ParseTemplate.php. For example, to have a method that is executed every time someone visits the home page of the web site, you may use:

<?php
declare(strict_types=1);

namespace App\Demo\Listeners;

use Apex\Cluster\Interfaces\{MessageRequestInterface, FeHandlerInterface};
use Apex\App\Interfaces\ListenerInterface;

/**
 * Demo listener
 */
class ParseTemplates implements ListenerInterface
{

    /**
     * Routing key this class listens to.
     */
    public static string $routing_key = 'syrus.parse';

    /**
     * index page
     */
    public function index(MessageRequestInterface $msg, FeHandlerInterface $handler)
    {

        // Get params from request
        $app = $msg->getParams();
        $uuid = $app->getUuid();

        // Assign variables to view
        $handler->assign('some_var', 'Dogs are awesome!');
    }
}

With the above in place, every time someone visits the home page of the website, the above code will execute. It will assign one variable to the view, so ˜some_var˜ will be replaced with Dogs are awesome~. Utilizing listeners in this manner allows you to easily modify the content of views that are not owned by your package, such as the login and homepages.