Exemplo n.º 1
0
 /**
  * 注册服务
  * @param type $di
  */
 public function registerServices(\Phalcon\DiInterface $di = NULL)
 {
     $config = \TConfig::instance()->getModule($this->moduleId);
     if (php_sapi_name() == 'cli') {
         //处理CLI模式
         $di['dispatcher'] = function () use($config) {
             $dispatcher = new \Phalcon\Cli\Dispatcher();
             if (isset($config['defaultNamespace']) && isset($config['defaultNamespace']['task'])) {
                 $dispatcher->setDefaultNamespace($config['defaultNamespace']);
             }
             return $dispatcher;
         };
     } else {
         //处理WEB模式
         $di['dispatcher'] = function () use($config) {
             $dispatcher = new \Phalcon\Mvc\Dispatcher();
             if (isset($config['defaultNamespace']) && isset($config['defaultNamespace']['controller'])) {
                 $dispatcher->setDefaultNamespace($config['defaultNamespace']['controller']);
             }
             return $dispatcher;
         };
         //添加视图
         $di->set('view', function () use($config) {
             $view = new \Phalcon\Mvc\View();
             $view->setViewsDir($config['autoloadDir']['view']);
             $view->registerEngines(array('.html' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
             return $view;
         });
     }
 }
Exemplo n.º 2
0
 public static function instance()
 {
     if (self::$instance == NULL) {
         self::$instance = new self();
     }
     return self::$instance;
 }
Exemplo n.º 3
0
            return new Phalcon\Db\Adapter\Pdo\Mysql($dbReadConfig);
        });
    }
    //设置默认数据库连接
    $defaultDbKey = 'db' . ucfirst($config->balanceDb->default);
    $di->set('db', $di->get($defaultDbKey));
    $defaultDbKey = 'db' . ucfirst($config->balanceDb->default);
    $di->set('db_read', $di->get($defaultDbKey . '_read'));
    //加载DB负载均衡
    BalanceDb::config($config->balanceDb->toArray());
    //Redis负载均衡
    BalanceRedis::config($config->balanceRedis->toArray());
    //URL 工具类
    TUrl::config($config->url->toArray());
    //config 工具类
    TConfig::instance()->setAll($config->toArray());
    //实例化应用
    $application = new \Phalcon\Mvc\Application($di);
    //注册模块
    $modules = array();
    foreach ($config->modules as $key => $params) {
        if ($key == 'default') {
            continue;
        }
        $modules[$key] = array('className' => sprintf('Module\\%s\\Module', ucfirst($key)), 'path' => $params['path']);
    }
    $application->registerModules($modules);
    echo $application->handle()->getContent();
} catch (\Exception $e) {
    echo $e->getMessage();
}