项目笔记 zend framework学习之digitalus(1.8.1)代码分析

来源:互联网 发布:淘宝网上买处方药流程 编辑:程序博客网 时间:2024/06/11 11:58

digitalus下载地址: http://www.digitaluscms.com/

 

访问方式: http://host_name/module/controller_name/action_name/param1/ value1/param2/ value2…

module模块名称

controller_name控制器名称

action_name 动作名称(函数除Action之外的名称)

14:29:27

一、index.php
//错误显示,如果注掉就不显示错误
error_reporting(E_ALL | E_STRICT);

//基本目录,一些模板目录中使用
defined('BASE_PATH')
    || define('BASE_PATH', realpath(dirname(__FILE__)));
       
// 定义应用目录

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));

 

// 定义应用环境,APPLICATION_ENV是预设的系统环境变量,它将用于Application.ini中区分应用程序运行环境,这里预设3个值:development, testing, production,分别表示开发环境,测试环境及实际运行环境

defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// 确认ZF的库
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    'E:/apmserv/APMServ5.2.6/www/htdocs/haojie_xml/library',
    get_include_path(),
)));

 

/** Zend_Application */
require_once 'Zend/Application.php'; 

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

/*

Zend_Application()用于解析application.ini,用APPLICATION_ENV来指定读取application.ini的那个模块(比如product)

*/
$application->bootstrap()
            ->run();

 

二.application.ini

 [production]
;=========== php ini配置
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

 

;=========== 库文件路径
;includePaths.library = APPLICATION_PATH "/../library"

 

;=========== 类自动加载的前缀
autoloaderNamespaces.0 = "Digitalus_"

 

;=========== bootstrap类的路径及类名
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

 

;=========== 自定义资源的路径及其前缀
;pluginPaths.Kbs_Application_Resource                            = APPLICATION_PATH "/../library/Kbs/Application/Resource/"

 

;=========== front controller配置

;resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV
resources.frontController.throwExceptions = false
resources.frontController.defaultModule = "admin"
resources.frontController.defaultControllerName = "index"
resources.frontController.defaultAction = "index"

;============启动的时候会自动加载下面的类
resources.frontController.plugins.init = "Digitalus_Controller_Plugin_Initializer" 
resources.frontController.plugins.init = "Digitalus_Controller_Plugin_Auth"

 

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

 

三。Bootstrap.php

  protected function _initAutoload()
    {
        // Ensure front controller instance is present
        $this->bootstrap('frontController');
        // Get frontController resource
        $this->_front = $this->getResource('frontController');

        // Add autoloader empty namespace
        $autoLoader =  new Zend_Loader_Autoloader_Resource(array(
            'basePath'      => APPLICATION_PATH,
            'namespace'     => '',
            'resourceTypes' => array(
                'form' => array(
                    'path'      => 'admin/forms/',
                    'namespace' => 'Admin_Form_',
                ),
                'model' => array(
                    'path'      => 'models/', //模块路径
                    'namespace' => 'Model_'    //制定默认模块前缀
                ),
            ),
        ));
        // Return it, so that it can be stored by the bootstrap
        return $autoLoader;
    }

 

1。加载xml文件

 protected function _initConfig()
    {
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/data/config.xml', APPLICATION_ENV);

        // 注册$config
        $registry = Zend_Registry::getInstance();
        $registry->set('config', $config);
        // Return it, so that it can be stored by the bootstrap
        return $config;
    }

 2。连接数据库

 protected function _initDb()
    {
        $this->bootstrap('config');
        // Get config resource
        $config = $this->getResource('config');
       
        // Setup database
        $db = Zend_Db::factory($config->database->adapter, $config->database->toArray());
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");
        Zend_Db_Table::setDefaultAdapter($db);
        // Return it, so that it can be stored by the bootstrap
        return $db;
    }

3。初始化Controller

 protected function _initControllers()
    {
        $this->_front->addControllerDirectory(APPLICATION_PATH . '/admin/controllers', 'admin');
       
    }

 

3。加载缓存,以文件的形式在服务器上保存,减少对数据库和服务器的压力

 protected function _initCache()
    {
        // Cache options
        $frontendOptions = array(
           'lifetime' => 1200,                      // Cache lifetime of 20 minutes
           'automatic_serialization' => true,
        );
        $backendOptions = array(
            'lifetime' => 3600,                     // Cache lifetime of 1 hour
            'cache_dir' => BASE_PATH . '/cache/',   // Directory where to put the cache files
        );
        // Get a Zend_Cache_Core object
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
        Zend_Registry::set('cache', $cache);
       
        // 查看一个缓存是否存在:
  if(!$result = $cache->load('myresult')) {
      // 缓存不命中
      $cache->save('aaaaaaaaaaaaaa', 'myresult');
  } else {
      // cache hit! shout so that we know
      echo "This one is from cache!/n/n";

  }
       

        // Return it, so that it can be stored by the bootstrap
        return $cache;
    }

 

 

 

 

$cache = Zend_Registry::get('cache');
     if(!$result = $cache->load('myresult')) {
      $cache->save('indexAction', 'myresult');
  } else {
   
      echo $cache->load('myresult');
      $cache->remove('myresult'); //删除缓存
  }

 

四。在TrafficLog.php中有日期格式化的例子

        $date = new Zend_Date();
        $data['timestamp'] = $date->get(zend_date::TIMESTAMP);
        $data['day'] = $date->get(zend_date::WEEKDAY_DIGIT);
        $data['week'] = $date->get(zend_date::WEEK);
        $data['month'] = $date->get(zend_date::MONTH);
        $data['year'] = $date->get(zend_date::YEAR);
        $data['page'] = $_SERVER['REQUEST_URI'];
        $data['ip'] = $_SERVER['REMOTE_ADDR'];

原创粉丝点击