Exemple #1
0
<?php

//Registering the collectionManager service
$di->set('collectionManager', function () {
    $eventsManager = new Phalcon\Events\Manager();
    // Attach an anonymous function as a listener for "model" events
    $eventsManager->attach('collection', function ($event, $model) {
        if (get_class($model) == 'Robots') {
            if ($event->getType() == 'beforeSave') {
                if ($model->name == 'Scooby Doo') {
                    echo "Scooby Doo isn't a robot!";
                    return false;
                }
            }
        }
        return true;
    });
    // Setting a default EventsManager
    $modelsManager = new Phalcon\Mvc\Collection\Manager();
    $modelsManager->setEventsManager($eventsManager);
    return $modelsManager;
}, true);
            $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
            $volt->setOptions(array("compiledPath" => APP_PATH . "/app/compiled-templates/", "compiledExtension" => ".compiled", "compiledSeparator" => '_'));
            return $volt;
        }));
        return $view;
    });
    // Setting up the database connection
    $di->set('db', function () use($config) {
        $database = $config->database;
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array('host' => $database->host, 'username' => $database->username, 'password' => $database->password, 'dbname' => $database->name, 'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')));
    });
    // Setting up the mongodb connection
    $di->set('mongo', function () use($config) {
        $mongodbConfig = $config->mongodb;
        $mongo = new \MongoClient($mongodbConfig->url);
        return $mongo->{$mongodbConfig->db};
    });
    //Registering the collectionManager service
    $di->set('collectionManager', function () {
        // Setting a default EventsManager
        $modelsManager = new Phalcon\Mvc\Collection\Manager();
        $modelsManager->setEventsManager(new Phalcon\Events\Manager());
        return $modelsManager;
    }, true);
    // Handle the request
    $application = new \Phalcon\Mvc\Application();
    $application->setDI($di);
    echo $application->handle()->getContent();
} catch (\Phalcon\Exception $e) {
    echo "PhalconException: ", $e->getMessage();
}