Modifying the Concrete5 Startup Routine from a Package

In addition to installing things like block types and themes, packages can inject custom code into the Concrete5 bootstrap routine. This can be very useful. For example, a statistics package might want to use the on_page_view event to track when pages are viewed and do something with that information. Hooking into the event is easy:

\Events::addListener(
    'on_page_view',
    function ($e) {
        $c = $e->getPageObject();
        // do something with the current page object
    });
);

To make this event listener code run during the Concrete5 bootstrap routine, simply create a method in the controller.php named "on_start()" and add it there:

public function on_start()
{
    \Events::addListener(
        'on_page_view',
        function ($e) {
            $c = $e->getPageObject();
           // do something with the current page object
        }
   );
}

Now, this code will run every time Concrete5 runs – but only if this particular package is installed.

Loading Conversation