Dirkos
Dirkos

Reputation: 676

Concrete5 controller logic

I have the following use case that i need to retrieve some data from ip-api.com and put it in a session so that some specific customer data can be used later in the process in my application.

Problem is that i have no idea where to put the logic in Concrete5 itself. I create a class in application/src/IpApi right now and the class itself is not the problem. The main problem is how i can ensure that it is executed on every single pageview. A second option would be to create a block and add it in the header but i need the data to be parsed even before the frontpage is active so before the template engine gets rendered.

Where should i put it and how should i load it. My current theme is an extend of the elemental theme so its still pretty out of the box.

Thanks for the advice

Upvotes: 1

Views: 140

Answers (2)

Korvin Szanto
Korvin Szanto

Reputation: 4501

You can create a package and use the package controller's on_start function. I'd recommend doing the operation within an on_before_render event as well to ensure that you're not doing this on ajax requests and other things like that. It'd look something like this:

<?php
namespace Concrete\Package\SomePackage;

class Controller extends \Concrete\Core\Package\Package
{

    public function on_start()
    {
        $app = \Core::make('app');
        $app->make('director')->addEventListener(
            'on_before_render', 
            function() use ($app) {
                $session = $app->make('session');
                $flashBag = $session->getFlashBag();

                if (!$flashBag->has('my-custom-data')) {
                    $dataGetter = $app->make('YourCustomDataGetterClass');
                    $flashBag->set('my-custom-data', $dataGetter->getData());
                }
            }
        );
    }

}

Upvotes: 1

Dirkos
Dirkos

Reputation: 676

I found out that i should create a PageType first. After that the specific type controller can be created at application/controllers/page_types/<mytype.php>

From there the on_start method is your friend or whatever function you need ofcourse

Upvotes: 0

Related Questions