E
E
Elkaz2014-01-04 11:28:51
Phalcon
Elkaz, 2014-01-04 11:28:51

Phalcon beforeexecute?

Hello.
I have a question about Phalcon, to be more precise - methods that should work before and after the action is launched.
There is the following:

class ControllerBase extends \Phalcon\Mvc\Controller
{

    public function beforeExecuteRoute($dispatcher)
    {
               echo 'beforeExecute';
        }
    }


class SomeController extends ControllerBase {  public function index(){ echo 'Test'; }  }

i have a micro application so the method name is not indexAction
When i run the application example.com/some
i get only test and expect to get beforeExecute test
Where am i going wrong?
Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikolay Eliseev, 2014-01-04
@nelis

There will be no output in this function (event) - this is a feature.
You can check whether an event occurs or not, like this:

class ControllerBase extends \Phalcon\Mvc\Controller
{
     public $p='N';
    public function beforeExecuteRoute($dispatcher)
    {
    $this->p='Y';
        }
    }


class SomeController extends ControllerBase {  public function index(){ echo 'Test '.$this->p; }  }

K
kompi, 2014-01-04
@kompi

beforeExecuteRoute always outputs data for the default view controller. Any other controllers have their own context, which overrides the default context.
For output in any view:

public function beforeExecuteRoute()
    {
        $this->view->setVar('param', 'ExecuteRoute');
    }

Accordingly, in the desired view, call $param.

K
kompi, 2014-01-04
@kompi

You can try an alternative:

$app = new \Phalcon\Mvc\Micro();
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('dispatch:beforeExecuteRoute', 
    function($event, $dispatcher, $exception) {
           throw new \Phalcon\Exception('Fired up!');
    });
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
$app->setService('dispatch', $dispatcher);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question