一。。配置虚拟主机(为使用zf设置,其他的可以没这么多要求)
1.要求:
PHP 5.1.4或以上版本;
apache服务器,并正确配置了mod_rewrite扩展;(如果没有配置这一模块,打开httpd.conf 配置文档,添加LoadModule rewrite_module modules/mod_rewrite.so ,当然了你得确保有这一模块)
2.假设网站名为 test
3,监听端口设置
假设我们用8080端口,在apache的配置文件httpd.conf中打开这一监听端口,添加 Listen 8080 就ok了。如果这个端口被多个虚拟主机共享,那么你只能在你的本机上正常访问这个网站了,在局域网其他的机子上则没法正常访问你的网站,怎么办呢?换个监听端口就好了。
4,(域名)设置
访问网站还得有域名,来访问你的网站啊!比如我们的 http://test:8080 .让test 映射到127.0.0.1,方法很简单。在Windows下修改c:\windows\system32\drivers\etc\hosts 文件,添加 127.0.0.1 test , 在Linux平台下则修改 /etc/hosts文件。
5.apache 中配置虚拟主机
在httpd.conf配置文件添加:
<VirtualHost *:8080>
ServerAdmin admin@test.local #管理员email
ServerName test
DocumentRoot "D:/webserver/www/test/www" #你的网站index.php所在的位置
<Directory "D:/webserver/www/test/www">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
ServerSignature Off
</Directory>
</VirtualHost>
6,添加 htaccess 文件
在网站index.php所在的目录下添加 .htaccess 文件,在Windows下可以使用dos下的
edit .htaccess 命令来创建这个文件。
文件内容为
RewriteEngine on
RewriteRule !\.(js|ico|gif|bmp|jpg|png|css|xml|html|txt)$ index.php [NC]
为方便起见,配置没有采用多模块设计,只是使用了默认的模块(default)。文件结构布局如下所示:
app/
controllers/
IndexController.php
cache/ //smarty缓存目录(后续我会介绍将smarty整合进zf框架)
compile/ //smarty编译后文件存放目录
views/
scripts/
index/
index.phtml
lib/
Zend/
DB/
smarty/
application.php
config.ini
html/
cache/ //zf缓存目录
css/
js/
images/
.htaccess
index.php
目录如上所示,其中的扩展我会在后续总结。
入口文件:index.php
<?php
ini_set('include_path','../lib'); //如果有多个路径,注意在Linux系统下用 : (分号)分割
ini_set('display_errors','on'); //on显示错误 off 不显示错误
include 'Zend/Loader.php';
include 'application.php'; //许多初始化的文件都在这里
Zend_Session::start();
function __autoload($class) //自动加载类
{
if (class_exists($class))
return ;
$path = str_replace('_', DIRECTORY_SEPARATOR, $class);
if(! @require_once $path . '.php'){
Zend_Loader::loadClass($class);
}
}
$app = new application();
$app->init();
application.php
<?php
class application
{
private $_controller = null;
private $_config = null;
private $_pathConfig = null;
/**
* 构造函数
*/
public function __construct()
{
Zend_Loader::loadClass('Zend_Controller_Front');
Zend_Loader::loadClass('Zend_Config_Ini');
Zend_Loader::loadClass('Zend_Registry');
Zend_Loader::loadClass("Zend_View");
Zend_Loader::loadClass('Zend_Db');
}
/**
* 初始化模块
*/
public function init()
{
$this->_initConfig();
$this->_initController();
$this->_initView();
$this->_initDb();
$this->_controller->returnResponse(true);
$response = $this->_controller->dispatch();
if ($response->isException()) {
$exceptions = $response->getException();
}
else {
$response->sendHeaders();
$response->outputBody();
}
}
/**
* 初始化配置信息
*/
private function _initConfig()
{
$this->_config = new Zend_Config_Ini('config.ini','mysqli');
$this->_pathConfig = new Zend_Config_Ini('config.ini','path');
}
/**
* 初始化控制器
*/
private function _initController()
{
$this->_controller = Zend_Controller_Front::getInstance();
/**
* 自定义参数,是否检测控制器是否存在,true 为controller不存在时跳转到控制器不存在提示页面
*/
$this->_controller->setParam("detectControllerExists",true) ;
/**
* 打开异常处理,若此项为false的话,将不抛出异常
* 注意: 网站投入使用后关闭此项
*/
$this->_controller->throwExceptions(true);
$this->_controller->setControllerDirectory($this->_pathConfig->dir->controller);
$registry = Zend_Registry::getInstance();
$registry->set('app',$this->_controller);
}
/**
* 初始化视图
*/
private function _initView()
{
$view = new Zend_View();
$view->setBasePath($this->_pathConfig->dir->viewBase);
/**
* 添加htm文件的存放目录
*/
$view->addScriptPath($this->_pathConfig->dir->viewBase."/htm");
/**
* 添加php脚本的存放目录
*/
$view->addScriptPath($this->_pathConfig->dir->viewBase."/scripts");
$registry = Zend_Registry::getInstance();
$registry->set('view',$view);
}
/**
* 初始化数据库连接
*
*/
private function _initDb()
{
$DB = Zend_Db::factory($this->_config->db->adapter,$this->_config->db->config->toArray());
$DB->query("set names ".$this->_config->db->config->charset);
Zend_Registry::set("DB",$DB);
}
}
配置文件:config.ini
[mysqli]
db.adapter = Mysqli
db.config.host = IP
db.config.username = username
db.config.password = password
db.config.dbname = dbname
db.config.port = 3306
db.config.charset = gbk
[path]
dir.controller = ../app/controllers
dir.viewBase = ../app/views
dir.compile = ../app/compile
dir.cache = ../app/cache
smarty部分
一。将smarty的核心文件包放在lib文件夹下,文件包中要包括(internals/,plugins/,Config_File.class.php,Smarty.class.php,Smarty_Compiler.class.php,debug.tpl).
二. 在Zend/View下添加文件:Smarty.php ,文件的内容如下:
<?php
/**
* Zend_View_Interface
*/
require_once 'Zend/View/Interface.php';
/**
* Smarty
*/
require_once("smarty/Smarty.class.php");
/**
* 创建Smarty视图
*/
class Zend_View_Smarty implements Zend_View_Interface
{
/**
* Smarty object
* @var Smarty
*/
protected $_smarty;
/**
* Constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct($tmplPath = null, $extraParams = array())
{
$this->_smarty = new Smarty;
if (null !== $tmplPath) {
$this->setScriptPath($tmplPath);
}
foreach ($extraParams as $key => $value) {
$this->_smarty->$key = $value;
}
}
/**
* Return the template engine object
*
* @return Smarty
*/
public function getEngine()
{
return $this->_smarty;
}
/**
* Set the path to the templates
*
* @param string $path The directory to set as the path.
* @return void
*/
public function setScriptPath($path)
{
if (is_readable($path)) {
$this->_smarty->template_dir = $path;
return;
}
throw new Exception('Invalid path provided');
}
/**
* set smarty缓存
* @author lengfeng
*/
public function setCompilePath($path){
if (is_readable($path)) {
$this->_smarty->compile_dir = $path;
return;
}
throw new Exception('Invalid path provided');
}
/**
* set smarty 编译后文档
* @author lengfeng
*/
public function setCachePath($path){
if (is_readable($path)) {
$this->_smarty->cache_dir = $path;
return;
}
throw new Exception('Invalid path provided');
}
/**
* Retrieve the current template directory
*
* @return string
*/
public function getScriptPaths()
{
return array($this->_smarty->template_dir);
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function setBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function addBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}
/**
* Assign a variable to the template
*
* @param string $key The variable name.
* @param mixed $val The variable value.
* @return void
*/
public function __set($key, $val)
{
$this->_smarty->assign($key, $val);
}
/**
* Retrieve an assigned variable
*
* @param string $key The variable name.
* @return mixed The variable value.
*/
public function __get($key)
{
return $this->_smarty->get_template_vars($key);
}
/**
* Allows testing with empty() and isset() to work
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return (null !== $this->_smarty->get_template_vars($key));
}
/**
* Allows unset() on object properties to work
*
* @param string $key
* @return void
*/
public function __unset($key)
{
$this->_smarty->clear_assign($key);
}
/**
* Assign variables to the template
*
* Allows setting a specific key to the specified value, OR passing an array
* of key => value pairs to set en masse.
*
* @see __set()
* @param string|array $spec The assignment strategy to use (key or array of key
* => value pairs)
* @param mixed $value (Optional) If assigning a named variable, use this
* as the value.
* @return void
*/
public function assign($spec, $value = null)
{
if (is_array($spec)) {
$this->_smarty->assign($spec);
return;
}
$this->_smarty->assign($spec, $value);
}
/**
* Clear all assigned variables
*
* Clears all variables assigned to Zend_View either via {@link assign()} or
* property overloading ({@link __get()}/{@link __set()}).
*
* @return void
*/
public function clearVars()
{
$this->_smarty->clear_all_assign();
}
/**
* Processes a template and returns the output.
*
* @param string $name The template to process.
* @return string The output.
*/
public function render($name)
{
return $this->_smarty->fetch($name);
}
/**
* 设置是否生成缓存
* 如果没有参数,默认为true
*/
public function setCache($bool){
if (isset($bool)) {
$this->_smarty->caching = $bool;
return;
}
}
}
三.在app文件夹下创建cache ,compile 文件夹
四,在config.ini 配置文件中加入
dir.compile = ../app/compile
dir.cache = ../app/cache
三,四两步可以参见hi.baidu.com/billdkj/blog/item/bec0b4fc8db6c0fbfd037f34.html《使用zendfreamwork框架搭建网站(二)》
五, 在application.php 文件中添加
/**
* 初始化smarty视图
*
*/
private function _initSmartyView()
{
$view = new Zend_View_Smarty();
$view->setBasePath($this->_pathConfig->dir->viewBase);
$view->setScriptPath($this->_pathConfig->dir->viewBase."/scripts");
$view->setCompilePath($this->_pathConfig->dir->compile);
$view->setCachePath($this->_pathConfig->dir->cache);
$smarty=$view->getEngine();
$smarty->caching=false;
$smarty->debugging = true;
$smarty->compile_check = true;
$smarty->left_delimiter = "<{"; //定义标示符
$smarty->right_delimiter = "}>";
$registry = Zend_Registry::getInstance();
$registry->set('smartyview',$smarty); //smarty对象
$registry->set('sview',$view);
}
并在 函数 init()中加入 $this->_initSmartyView();
六:在Controller中调用
因为已经将对象注册,所以可以如下调用:
$view = Zend_Registry::getInstance()->get("smartyview"); //注意这是smarty对象,使用smarty的那些语法,比如 $view->assign("user","root");
$view = Zend_Registry::getInstance()->get("sview"); //这是zf的view对象,按zf中的那些方法用,不用改变。按这样,你如果要将以前写的代码改为用smaty,后台不用变了,只需要将视图文件改变就行了