欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

php之ZendFramewor篇(四)Zend Framework和Smarty在php中的配置

本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接: 
http://www.firedragonpzy.com.cn/index.php/archives/1882

<?php error_reporting (E_ALL & ~E_DEPRECATED & ~E_NOTICE);
date_default_timezone_set ( 'Asia/Shanghai' );
//define('ROOT_PATH',($ROOT_PATH =dirname(__FILE__))? $ROOT_PATH : '..');
// Define path to application directory
defined('APPLICATION_PATH')  || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
 defined('APPLICATION_ENV')  || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(  realpath(APPLICATION_PATH . '/../library'),  get_include_path(),  ))); //set_include_path ( '.' . PATH_SEPARATOR . '../application/controllers/' . '../application/models/' . '../application/dao/' . PATH_SEPARATOR . get_include_path () );
//装载ZF的类加载器
 require_once 'Zend/Loader/Autoloader.php';
//自动加载类
Zend_Loader_Autoloader::getInstance()--->setFallbackAutoloader(true);
 //得到注册实例
 $register = Zend_Registry::getInstance();

//加载application配置文件,配置数据库
 $config = new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini", null, true);
 Zend_Registry::set('config', $config);
 $dbAdapter = Zend_Db::factory($config->database->db->adapter, $config->database->db->config->toArray());
 Zend_Db_Table::setDefaultAdapter($dbAdapter);
//注意:一定要开启此句,否则出现乱码,有关乱码的问题,详情请参考:php编码问题:http://www.firedragonpzy.com.cn/index.php/archives/2243
$dbAdapter->query ( 'SET names utf8' );
 Zend_Registry::set('db', $dbAdapter);

//日志
 require_once 'Zend/Log.php';
 require_once 'Zend/Log/Writer/Stream.php';
 $logger = new Zend_Log(new Zend_Log_Writer_Stream('.' . $config->log->logger->path, 'a+') );
 Zend_Registry::set('logger', $logger);

//视图
 $view = new Zend_View();
 $view->setScriptPath('./templates/');
 $registry ['view'] = $view;

//smarty
 include_once ("../library/Smarty/Smarty.class.php");
 $smarty = new Smarty();
 $smarty->caching = false;//是否使用缓存,项目在调试期间,不建议启用缓存
 $smarty->template_dir = "./templates/";//设置模板目录
 $smarty->compile_dir = "./data/smarty/";//设置编译目录
 $smarty->cache_dir = "./data/smarty/cache_c/";//缓存文件夹
 //$smarty->left_delimiter = "right_delimiter = "}>";
$registry->set ( 'smarty', $smarty );//注意:如果此行报错: Call to a member function set() on a non-object则替换为:Zend_Registry::set('smarty', $smarty);

//前端控制器
 $front = Zend_Controller_Front::getInstance ();
 $front->setBaseUrl ( '/' );
 $front->setParam ( 'useDefaultControllerAlways', true );
 $front->setParam ( 'noViewRenderer', true );
 $front->setControllerDirectory ( '../application/controllers' );
 $front->throwExceptions ( true );

//会话
 Zend_Session::start();
 /** Zend_Application */
 require_once 'Zend/Application.php';

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

摘自程序员之家解释:
首先是设置了include_path,include_path就是php中调用include的时候文件寻找的地址。
下面就是 require_once ‘Zend/Loader/Autoloader.php’,装载ZF的类加载器。
在Zend/Loader/Autoloader.php文件内,读入了Zend/Loader.php, 这个php定义了Zend_Loader这个类,这个类包含了loadClass,loadFile, isReadable(文件是否可读)等函数实例化Zend_Loader_Autoloader的过程就是调用其构造函数(这里是使用了单例模式)的过程它的构造函数中的spl_autoload_register(array(__CLASS__, ‘autoload’));将Zend_Loader_Autoloader:autoload作为类自动加载函数。还做了一个操作将_internalAutoloader赋值了自身的_autoload

推荐阅读:Zend Framework中的AutoLoad机制

Tags: , ,

发表评论