Example #1
1
 /**
  * @param Event $event
  * @param Application $application
  * @return mixed
  */
 public function boot(Event $event, Application $application)
 {
     $config = $application->getConfig()->application;
     if (isset($config->view) && $config->view !== false) {
         $view = new View();
         $view->setViewsDir($config->view->viewsDir);
         $view->setLayoutsDir($config->view->layoutsDir);
         $view->setLayout($config->view->layout);
         $application->getDI()->setShared('view', $view);
     }
 }
Example #2
0
 public function registerServices(DiInterface $di)
 {
     global $config;
     $di->setShared('url', function () use($config) {
         $url = new UrlResolver();
         $url->setBaseUri($config->backend->baseUri);
         return $url;
     });
     $di->setShared('dispatcher', function () {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Multiple\\Backend\\Controllers");
         return $dispatcher;
     });
     $di->setShared('view', function () use($config) {
         $view = new View();
         $view->setViewsDir($config->backend->viewsDir);
         $view->setLayoutsDir('layouts/');
         $view->setPartialsDir('partials/');
         $view->registerEngines(array('.phtml' => function ($view, $di) use($config) {
             $volt = new VoltEngine($view, $di);
             $volt->setOptions(array('compiledPath' => $config->backend->cacheDir, 'compiledSeparator' => '_'));
             return $volt;
         }, '.volt' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
         return $view;
     });
 }
Example #3
0
 public function registerServices(DiInterface $di)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     $di['dispatcher'] = function () {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Modules\\Backend\\Controllers");
         return $dispatcher;
     };
     /**
      * Setting up the view component
      */
     $di['view'] = function () {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         $view->setLayoutsDir('../../common/layouts/backend/');
         $view->setTemplateAfter('main');
         return $view;
     };
     /**
      * Database connection is created based in the parameters defined in the configuration file
      */
     $di['db'] = function () use($config) {
         return new MySQLAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name));
     };
 }
Example #4
0
 /**
  * Register specific services for the module
  */
 function registerServices(\Phalcon\DiInterface $di)
 {
     //Registering a dispatcher
     $di->set('dispatcher', function () {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace('Modules\\Frontend\\Controllers');
         return $dispatcher;
     });
     $config = $di->getShared('config');
     $di->set('view', function () use($config, $di) {
         $view = new View();
         $router = $di->getShared('router');
         /*
          * @todo 给layouts等目录统一变量
          * */
         $view->setViewsDir(__DIR__ . '/views/');
         $view->setLayoutsDir('/../../../layouts/');
         // 多模块的话, 可能会有多个风格,所以需要切换layout,这里的Path是根据当前module的Path向上走的
         $view->setLayout('index');
         $view->registerEngines(array('.volt' => function ($view, $di) use($config) {
             $volt = new VoltEngine($view, $di);
             $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
             return $volt;
         }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
         return $view;
     }, true);
 }
Example #5
0
 public function registerServices(DiInterface $di)
 {
     $di['dispatcher'] = function () {
         $eventsManager = new \Phalcon\Events\Manager();
         //Attach a listener
         $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
             //controller or action doesn't exist
             if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
                 $dispatcher->forward(array('controller' => 'error', 'action' => 'error404'));
                 return false;
             }
             switch ($exception->getCode()) {
                 case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                 case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                     $dispatcher->forward(array('controller' => 'error', 'action' => 'error404'));
                     return false;
             }
         });
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Modules\\Frontend\\Controllers");
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     };
     $di['view'] = function () {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         $view->setLayoutsDir('../../common/layout_frontend/');
         return $view;
     };
 }
 private function setView($manager, $theme)
 {
     /* ==================================================
      * ตั้งค่าเรียกใช้งานไฟล์ View ทั้งหมด
      * Setting up the view component
      * ================================================== */
     $manager->set('view', function () use($theme) {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         /* ตำแหน่งเก็บไฟล์ views ทั้งหมด */
         $view->setLayoutsDir(sprintf('%s/%s/', $this->config->theme->themesDir, $theme));
         /* ตำแหน่งเก็บไฟล์ layouts ทั้งหมด */
         $view->setTemplateAfter('layouts/' . $this->layoutName);
         /* เลือกไฟล์ layout เริ่มต้น*/
         /* สร้างโฟล์เดอร์เก็บไฟล์ cache */
         $cacheDir = sprintf('%s/%s/%s/', APPLICATION_PATH, $this->config->application->cacheDir, $this->moduleName);
         if (!is_dir($cacheDir)) {
             mkdir($cacheDir);
         }
         $view->registerEngines(array('.phtml' => function ($view, $di) {
             $volt = new VoltEngine($view, $di);
             $volt->setOptions(array('compiledPath' => sprintf('%s/%s/%s/', APPLICATION_PATH, $this->config->application->cacheDir, $this->moduleName), 'compiledSeparator' => '_'));
             return $volt;
         }));
         return $view;
     });
 }
Example #7
0
 private function setView()
 {
     $view = new View();
     $view->setViewsDir($this->config->application->viewsDir);
     $view->setLayoutsDir('layouts/');
     $view->setLayout('index');
     $view->registerEngines(['.volt' => $this->setVolt($view), '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']);
     return $view;
 }
Example #8
0
 public function registerServices(DiInterface $di)
 {
     $di['dispatcher'] = function () {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Modules\\Backend\\Controllers");
         return $dispatcher;
     };
     $di['view'] = function () {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         $view->setLayoutsDir('../../common/layout_backend/');
         return $view;
     };
 }
 /**
  * Register specific services for the module
  */
 public function registerServices(\Phalcon\DiInterface $dependencyInjector)
 {
     /**
      * Read configuration
      */
     $config = (require_once MODULES_DIR . $this->module_name . DS . "config" . DS . "config.php");
     //Registering a dispatcher
     $dependencyInjector->set('dispatcher', function () {
         $dispatcher = new \Phalcon\Mvc\Dispatcher();
         //Attach a event listener to the dispatcher
         $eventManager = new \Phalcon\Events\Manager();
         //$eventManager->attach('dispatch', new \Acl($this->module_name));
         $dispatcher->setEventsManager($eventManager);
         $dispatcher->setDefaultNamespace("Mod\\ModMan\\Controllers\\");
         return $dispatcher;
     });
     /**
      * Setting up the view component
      */
     $dependencyInjector->set('view', function () use($config) {
         $view = new \Phalcon\Mvc\View();
         $view->setViewsDir($config->module->viewsDir);
         $view->setViewsDir(MODULES_DIR . $this->module_name . DS . 'views' . DS);
         $view->setLayoutsDir(MODULES_DIR . $this->module_name . DS . 'layouts' . DS);
         //$view->setTemplateAfter('index');
         $view->registerEngines(array('.volt' => function ($view, $di) {
             $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
             $volt->setOptions(array('compiledPath' => MODULES_DIR . $this->module_name . DS . 'views' . DS . '_compiled' . DS, 'stat' => true, 'compileAlways' => true));
             return $volt;
         }));
         return $view;
     });
     /**
      * Database connection is created based in the parameters defined in the configuration file
      * http://stackoverflow.com/questions/22197678/how-to-connect-multiple-database-in-phalcon-framework
      */
     $dependencyInjector->set('db', function () use($config) {
         return new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name));
     });
 }
Example #10
0
 /**
  *
  * @param \Phalcon\DI $di
  */
 public function registerServices($di)
 {
     Admin::instance();
     require __DIR__ . '/../../../../phad/phad.php';
     $di['phadConfig'] = function () {
         $config = (require __DIR__ . '/../../../../phad-config.php');
         return new Config($config);
     };
     $di['view'] = function () {
         $view = new ViewEngine();
         $view->setViewsDir(__DIR__ . '/Views/');
         $view->setLayoutsDir('Layouts/');
         $view->setPartialsDir('Partials/');
         return $view;
     };
     $di['viewSimple'] = function () {
         $view = new Simple();
         $view->setViewsDir(__DIR__ . '/Views/');
         return $view;
     };
     $di['flashSession'] = function () {
         $flashClasses = ['error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning'];
         return new FlashSession($flashClasses);
     };
     $di['session'] = function () {
         $session = new Session();
         $session->start();
         return $session;
     };
     $di['phadAuth'] = function () {
         return new Auth();
     };
     $di['assets'] = function () use($di) {
         $options = ['sourceBasePath' => __DIR__ . '/Assets/', 'targetBasePath' => __DIR__ . '/../../../../public/backend-assets/'];
         $assets = new AssetsManager($options);
         $assets->collection('backend_css')->setTargetPath('final.css')->setTargetUri('backend-assets/final.css')->addCss('bootstrap/css/bootstrap.min.css')->addCss('css/styles.css')->join(true)->addFilter(new AssetsNullFilter());
         $assets->collection('backend_js')->setTargetPath('final.js')->setTargetUri('backend-assets/final.js')->addJs('bootstrap/js/bootstrap.min.js')->addJs('js/custom.js')->join(true)->addFilter(new AssetsNullFilter());
         return $assets;
     };
 }
Example #11
0
 /**
  * @param \Phalcon\DI\FactoryDefault     $di
  */
 protected function registerViewService($di)
 {
     /**
      * Setting up the view component
      */
     $di['view'] = function () use($di) {
         /*
          * Obtengo el nombre del modulo para poder obner el archivo de configuracion
          */
         $module = $this->name;
         $this->layout_dir = '../../../../public/layouts/' . $di->get('modules')->{$module}->layout . '/';
         $view = new View();
         $view->setViewsDir($this->path . $this->view_dir);
         $view->setLayoutsDir($this->layout_dir);
         $view->setTemplateAfter($this->template);
         $view->setVar('project-setting', $di->get('config')->project->toArray());
         /*
          * Registra Constantes de Modulo
          */
         $view->setVar('module-setting', $di->get('modules')->{$module}->toArray());
         // Set the engine
         $view->registerEngines(array(".mustache" => function ($view, DI $di) {
             $module = $this->name;
             $partial_url = '../public/layouts/' . $di->get('modules')->{$module}->layout . '/partials';
             $partial_loader = new Mustache_Loader_FilesystemLoader($partial_url);
             $config = $di->get('config')->cache->frontend;
             if ($config->active == 0) {
                 $options = array('partials_loader' => $partial_loader);
             } else {
                 $options = array('cache' => '/var/www/var/cache/elephant', 'cache_file_mode' => $config->mode, 'partials_loader' => $partial_loader, 'strict_callables' => $config->callables);
             }
             $mustache = new Mustache($view, $di, $options);
             return $mustache;
         }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
         return $view;
     };
 }
Example #12
0
 /**
  * Registers services related to the module
  *
  * @param DiInterface $di        	
  */
 public function registerServices(DiInterface $di)
 {
     /**
      * Read configuration
      */
     $config = new Ini(APP_PATH . "/apps/teacher/config/config.ini");
     /**
      * Setting up the view component
      */
     $di['view'] = function () {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         // set layout
         $view->setLayoutsDir('../../../layouts/');
         $view->setTemplateAfter('main');
         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->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname));
     };
 }
Example #13
0
/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});
/**
 * Start the session the first time some component request the session service
 */
$di->set('session', function () {
    $session = new SessionAdapter();
    $session->start();
    return $session;
});
$di->set('view', function () use($config, $di) {
    $view = new View();
    $router = $di->getShared('router');
    /*
     * @todo 给layouts等目录统一变量
     * */
    $view->setViewsDir(__DIR__ . '/views/');
    $view->setLayoutsDir('/../../../layouts/');
    // 多模块的话, 可能会有多个风格,所以需要切换layout,这里的Path是根据当前module的Path向上走的
    $view->setLayout('adminCommon');
    $view->registerEngines(array('.volt' => function ($view, $di) use($config) {
        $volt = new VoltEngine($view, $di);
        $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
        return $volt;
    }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
    return $view;
}, true);
    $eventsManager = $manager->getShared('eventsManager');
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});
/* ==================================================
 * ตั้งค่าการเชื่อมต่อฐานข้อมูล
 * ================================================== */
$manager->set('db', function () {
    return new DbAdapter(array('host' => $this->config->database->host, 'username' => $this->config->database->username, 'password' => $this->config->database->password, 'dbname' => $this->config->database->dbname, 'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->config->database->charset)));
});
$manager->set('view', function () {
    $view = new View();
    $view->setViewsDir(__DIR__ . '/views/');
    /* ตำแหน่งเก็บไฟล์ views ทั้งหมด */
    $view->setLayoutsDir(sprintf('%s/%s/', $this->config->theme->themesDir, $this->config->theme->themeDefault));
    /* ตำแหน่งเก็บไฟล์ layouts ทั้งหมด */
    $view->setTemplateAfter('layouts/' . $this->config->theme->layoutDefault);
    /* เลือกไฟล์ layout เริ่มต้น*/
    /* สร้างโฟล์เดอร์เก็บไฟล์ cache */
    $cacheDir = sprintf('%s/%s/', APPLICATION_PATH, $this->config->application->cacheDir);
    if (!is_dir($cacheDir)) {
        mkdir($cacheDir);
    }
    $view->registerEngines(array('.phtml' => function ($view, $di) {
        $volt = new VoltEngine($view, $di);
        $volt->setOptions(array('compiledPath' => sprintf('%s/%s/', APPLICATION_PATH, $this->config->application->cacheDir), 'compiledSeparator' => '_'));
        return $volt;
    }));
    return $view;
});
Example #15
0
 /**
  *
  * @param type $options
  */
 protected function initView($options = [])
 {
     $config = $this->_di->get('config');
     $di = $this->_di;
     if (!file_exists($config->volt->path)) {
         mkdir($config->volt->path, 0777, true);
     }
     $this->_di->setShared('volt', function ($view, $di) use($config) {
         $volt = new Volt($view, $di);
         $volt->setOptions(['compiledPath' => $config->volt->path, 'compiledExtension' => $config->volt->extension, 'compiledSeparator' => $config->volt->separator, 'compileAlways' => (bool) $config->volt->compileAlways, 'stat' => (bool) $config->volt->stat]);
         $compiler = $volt->getCompiler();
         $compiler->addFunction('is_a', 'is_a');
         return $volt;
     });
     $this->_di->setShared('view', function () use($config) {
         $view = new View();
         $view->setViewsDir($config->application->viewsDir);
         $view->setMainView('index');
         $view->setLayoutsDir('layouts/');
         $view->setPartialsDir('partials/');
         $view->registerEngines(['.volt' => 'volt', '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']);
         return $view;
     });
 }
Example #16
0
});
/**
 * The URL component is used to generate all kinds of URLs in the application
 */
$di->setShared('url', function () use($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
});
/**
 * Setting up the view component
 */
$di->setShared('view', function () use($config) {
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);
    $view->setLayoutsDir('../../common/layouts/');
    $view->setTemplateAfter('main');
    $view->registerEngines(array('.volt' => function ($view, $di) use($config) {
        $volt = new VoltEngine($view, $di);
        $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_', 'stat' => true, 'compileAlways' => true));
        return $volt;
    }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
    return $view;
});
/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->setShared('db', function () use($config) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);
Example #17
0
 public static function run()
 {
     if (in_array(APPLICATION_ENV, array('development'))) {
         $debug = new \Phalcon\Debug();
         $debug->listen();
     }
     $di = new \Phalcon\DI\FactoryDefault();
     $config = (include APPLICATION_PATH . '/config/application.php');
     $di->set('config', $config);
     $loader = new \Phalcon\Loader();
     $loader->registerNamespaces($config->loader->namespaces->toArray());
     $loader->register();
     $loader->registerDirs(array(APPLICATION_PATH . "/plugins/"));
     $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "charset" => $config->database->charset));
     $di->set('db', $db);
     $view = new View();
     /* $view->disableLevel(array(
        View::LEVEL_BEFORE_TEMPLATE => true,
        View::LEVEL_LAYOUT          => true,
        View::LEVEL_AFTER_TEMPLATE  => true
        )); */
     define('MAIN_VIEW_PATH', '../../../views/');
     $view->setMainView(MAIN_VIEW_PATH . 'main');
     $view->setLayoutsDir(MAIN_VIEW_PATH . '/layouts/');
     $view->setPartialsDir(MAIN_VIEW_PATH . '/partials/');
     $volt = new \Application\Mvc\View\Engine\Volt($view, $di);
     $volt->setOptions(array('compiledPath' => APPLICATION_PATH . '/cache/volt/'));
     $volt->initCompiler();
     $viewEngines = array(".volt" => $volt);
     $view->registerEngines($viewEngines);
     $di->set('view', $view);
     $viewSimple = new \Phalcon\Mvc\View\Simple();
     $viewSimple->registerEngines($viewEngines);
     $di->set('viewSimple', $viewSimple);
     $url = new \Phalcon\Mvc\Url();
     $url->setBasePath('/');
     $url->setBaseUri('/');
     $application = new \Phalcon\Mvc\Application();
     $application->registerModules($config->modules->toArray());
     $router = new \Application\Mvc\Router\DefaultRouter();
     foreach ($application->getModules() as $module) {
         $className = str_replace('Module', 'Routes', $module['className']);
         if (class_exists($className)) {
             $class = new $className();
             $router = $class->init($router);
         }
     }
     $di->set('router', $router);
     $eventsManager = new \Phalcon\Events\Manager();
     $dispatcher = new \Phalcon\Mvc\Dispatcher();
     $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
         new ExceptionPlugin($dispatcher, $exception);
     });
     $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) {
         new LocalizationPlugin($dispatcher);
     });
     $eventsManager->attach("acl", function ($event, $acl) {
         if ($event->getType() == 'beforeCheckAccess') {
             echo $acl->getActiveRole(), $acl->getActiveResource(), $acl->getActiveAccess();
         }
     });
     $dispatcher->setEventsManager($eventsManager);
     $di->set('dispatcher', $dispatcher);
     $session = new \Phalcon\Session\Adapter\Files();
     $session->start();
     $di->set('session', $session);
     $acl = new \Application\Acl\DefaultAcl();
     $acl->setEventsManager($eventsManager);
     $di->set('acl', $acl);
     $assets = new \Phalcon\Assets\Manager();
     $di->set('assets', $assets);
     $flash = new \Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning'));
     $di->set('flash', $flash);
     $di->set('helper', new \Application\Mvc\Helper());
     $application->setDI($di);
     echo $application->handle()->getContent();
 }