Viewing file: Module.php (4.77 KB) -rwxrwxrwx Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php namespace Modules\Frontend;
use Modules\Models\CdAdvertising; use Phalcon\Loader; use Phalcon\Mvc\View; use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; use Phalcon\Mvc\ModuleDefinitionInterface; use Phalcon\Mvc\Dispatcher; use Phalcon\DI\FactoryDefault; use Phalcon\Mvc\Url as UrlResolver; use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
class Module implements ModuleDefinitionInterface { /** * Registers the module auto-loader */ public function registerAutoloaders(\Phalcon\DiInterface $di=null) {
$loader = new Loader();
$loader->registerNamespaces(array( 'Modules\Frontend\Controllers' => __DIR__ . '/controllers/', 'Modules\Models' => __DIR__ . '/../../models/', 'Modules\Models\Entities' => __DIR__ . '/../../models/entities/', 'Modules\Models\Services' => __DIR__ . '/../../models/services/', 'Modules\Models\Repositories' => __DIR__ . '/../../models/repositories/' ));
$loader->register(); }
public function registerServices(\Phalcon\DiInterface $di) { /** * Read configuration */ $config = include dirname(dirname(dirname(__DIR__))) . "/apps/config/config.php";
/** * Error 404 */ $di->set( 'dispatcher', function() use ($di) {
$evManager = $di->getShared('eventsManager');
$evManager->attach( "dispatch:beforeException", function($event, $dispatcher, $exception) { if ($event->getType() == 'beforeNotFoundAction') { $dispatcher->forward(array( 'module'=>'frontend', 'controller' => 'error', 'action' => 'show404' )); return false; } if ($event->getType() == 'beforeException') { switch ($exception->getCode()){ case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array( 'module'=>'frontend', 'controller' => 'error', 'action' => 'show404' )); return false; } } } ); $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($evManager); $dispatcher->setDefaultNamespace('Modules\Frontend\Controllers'); return $dispatcher; }, true ); $di->set('advertisingJson', function() { $json = dirname(dirname(dirname(__DIR__)))."/public/json/advertising.json"; $get_file = file_get_contents($json); if(!file_exists($json) || empty($get_file)){ $file = fopen($json,"wb"); $advertising = CdAdvertising::find(); $content = array(); foreach($advertising as $values){ $content[$values->getOrderAdv()]=array( "image"=>$values->getNameImage(), "url"=>$values->getUrl() ); } $advertising_json = json_encode($content); fwrite($file,$advertising_json); fclose($file); return $advertising_json; }else{ return $get_file; } });
$di->set('view', function() use ($di) { $view = new View(); $view->setViewsDir(__DIR__ . '/views/');
$volt = new View\Engine\Volt($view, $di); $compiler = $volt->getCompiler();
$compiler->addFunction('strtotime', 'strtotime'); $view->registerEngines(array( ".volt" => $volt ));
return $view; });
/** * Database connection is created based in the parameters defined in the configuration file */ $di['db'] = function () use ($config) { return new DbAdapter(array( "host" => $config->database->mysql->host, "username" => $config->database->mysql->username, "password" => $config->database->mysql->password, "dbname" => $config->database->mysql->dbname, 'charset' =>'utf8' )); }; }
}
|