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

beforeExecuteRoute micro mvc controller not working

Hi, Im trying to get beforeExecuteRoute function to fire on a controller loaded into a micro mvc application but no matter what i try it doesnt want to work. Here are the relevant pieces of code from the controllers, is there something i need somewhere else to initialise the before execute route?

Charities controller

    <?php
    namespace LoaflyAPI\Controllers;
    use \LoaflyAPI\Exceptions\HTTPException,
    \LoaflyAPI\Models\Charities as Charities,
    \LoaflyAPI\Models\Appeals as Appeals;

    class CharitiesController extends RESTController{

    public function beforeExecuteRoute() {
        var_dump("Before Charities");
    }

    public function initialize() {
        var_dump("Initialize");
    }

    public function get(){
        if($this->isSearch){
            $results = $this->search();
        } else {
            $charities = Charities::find();
            $results = array();
            foreach ($charities as $charity) {
                $results[] = $charity->getJsonArray();
            }
        }

        return $this->respond($results);
    }
    <omitted>
    }   

Rest Controller,

    <?php
    namespace LoaflyAPI\Controllers;
    use \LoaflyAPI\Exceptions\HTTPException;

    class RESTController extends \LoaflyAPI\Controllers\BaseController{
    <ommitted>
    }

Base Controller

    <?php
    namespace LoaflyAPI\Controllers;
    class BaseController extends \Phalcon\Mvc\Controller{

    }


98.9k

How are you loading the handlers for that controller?

edited Jun '14

okay my index.php has

$di->set('collections', function(){
    return include('./routes/routeLoader.php');
});
    <omitted>
foreach($di->get('collections') as $collection){
    $app->mount($collection);
}

routeloader.php has

return call_user_func(function(){

$collections = array();
$collectionFiles = scandir(dirname(__FILE__) . '/collections');

foreach($collectionFiles as $collectionFile){
    $pathinfo = pathinfo($collectionFile);

    //Only include php files
    if($pathinfo['extension'] === 'php'){

        // The collection files return their collection objects, so mount
        // them directly into the router.
        $collections[] = include(dirname(__FILE__) .'/collections/' . $collectionFile);
    }
}

return $collections;

and the actual collections for the route is

return call_user_func(function(){

    $charityCollection = new \Phalcon\Mvc\Micro\Collection();

    $charityCollection
        ->setPrefix('/v1/charities')
        ->setHandler('\LoaflyAPI\Controllers\CharitiesController')
        ->setLazy(true);

    // Set Access-Control-Allow headers.
    $charityCollection->options('/', 'optionsBase');
    $charityCollection->options('/{id}', 'optionsOne');

    $charityCollection->get('/', 'get');
    $charityCollection->head('/', 'get');

    $charityCollection->get('/{id:[0-9]+}', 'getOne');
    $charityCollection->head('/{id:[0-9]+}', 'getOne');
    $charityCollection->post('/', 'post');
    $charityCollection->delete('/{id:[0-9]+}', 'delete');
    $charityCollection->put('/{id:[0-9]+}', 'put');
    $charityCollection->patch('/{id:[0-9]+}', 'patch');

    return $charityCollection;
});


98.9k

I think the functionality you are expecting is not implemented. Phalcon\Mvc\Micro does not look for events in the controller such as beforeExecuteRoute or beforeExecuteRoute because Phalcon\Mvc\Micro lacks of a dispatcher. You can use the full MVC implementation which has a dispatcher.

If you want to use events in Phalcon\Mvc\Micro you can implement a middleware: https://docs.phalcon.io/en/latest/reference/micro.html#middleware-events

I have the same problem and your answer gave me the solution, thanks :)

I think the functionality you are expecting is not implemented. Phalcon\Mvc\Micro does not look for events in the controller such as beforeExecuteRoute or beforeExecuteRoute because Phalcon\Mvc\Micro lacks of a dispatcher. You can use the full MVC implementation which has a dispatcher.

If you want to use events in Phalcon\Mvc\Micro you can implement a middleware: https://docs.phalcon.io/en/latest/reference/micro.html#middleware-events