D
D
Denis Goncharenko2015-10-17 22:41:45
Phalcon
Denis Goncharenko, 2015-10-17 22:41:45

How to make an initialization function in Phalcon?

The question is, there are some actions that should occur when any controller is triggered, for example, setting the default page title. The developers in the documentation suggest using the following scheme:

<?php

use Phalcon\Mvc\Controller;

class ControllerBase extends Controller
{
    protected function initialize()
    {
        $this->tag->setTitle('INVO');
    }

    // ...
}

<?php

class ProductsController extends ControllerBase
{
    public function initialize()
    {
        parent::initialize();
        $this->tag->prependTitle('Управление типами ваших продуктов | ');
    }

    // ...
}

I really don’t want to write in each controller, especially if there are several sections of the site and each has its own controller:
<?php

    public function initialize()
    {
        parent::initialize();
    }

It seems to me that there is a more harmonious solution, I do not think that such a code tautology is good.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KorsaR-ZN, 2015-10-17
@denis79513

Yes, it is possible in different ways, for example, to make the AwarePageMetainfo interface, with a method, for example, getTitle (). Then implement this interface in the controllers you need, then write the "beforeExecuteRoute" dispatch event listener, in it you get the active controller, check the implementation of the desired interface, and then set the page meta-information.
And then OOP enters in each controller will have its own implementation of the interface. Well, I think the idea is clear)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question