We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

what phalcon class extends to execute initialize method

hello !

What phalcon class extends to execute initialize method first?

Class test extends \Phalcon\........ 
{
    public function initialize(){
} 

Try \Phalcon\Mvc\User\Component



8.1k
edited Sep '14

via Phalcon\Dispatcher You can try


<?php

class TestController extends \Phalcon\Mvc\Controller
{

  //This action will be executed after instance created
  public function initialize()
  {
    echo __FUNCTION__, "\n";
  }

  public function onConstruct()
  {
    echo __FUNCTION__, "\n";
  }

  public function anyAction()
  {
    echo __FUNCTION__, "\n";
  }

}

$d = new TestController();
$d->anyAction();

Without Phalcon\Dispatcher function initialize it's never called, because it's an event fire.

Out is ;

onConstruct
anyAction


1.5k
edited Sep '14

Other option is use INVO structure

<?php

    use Phalcon\Mvc\Controller;

    class ControllerBase extends Controller
    {
        protected function initialize()
        {
            $this->tag->prependTitle('Title | ');
        }
    }

Then your class

    Class test extends ControllerBase
    {
        public function initialize(){
            //something here 
        }
    }