WAMP下 搭建Zend Framework(Hello World)实例
一、环境搭建
1、下载安装Zend Framework,此步骤不再详细叙述
2、配置php.ini文件
打开php.ini文件,可以找到如下的代码:
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
修改为include_path = ".;d:\zend_framework\ZendFramework-1.12.3\library" (Zend Framework安装包中的文件目录library)
3、修改httpd.conf文件
1).开启Apache对rewrite的支持
#LoadModule rewrite_module modules/mod_rewrite.so #去掉
2).指定Virtual host的配置文件
# Virtual hosts
# Include conf/extra/httpd-vhosts.conf #去掉
3).支持.htaccess文件
AllowOverride None替换AllowOverride all
4、Win+r 打开运行->cmd,打开DOS窗口,进入事先建好的项目目录,如zend_framework,运行zf create project HelloWorld创建名为HelloWorld的项目,
5、上述产生的项目中在D:\zend_framework\HelloWold\docs出现README.txt,打开此文件就会看到如下代码:
<VirtualHost *:80> DocumentRoot "D:/zend_framework/HelloWold/public" ServerName .local # This should be omitted in the production environment SetEnv APPLICATION_ENV development <Directory "D:/zend_framework/HelloWold/public"> Options Indexes MultiViews FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
这里将这段文字拷贝到/conf/extra/httpd-vhosts.conf文件的末尾,并稍作修改,如下:
<VirtualHost *:8099> DocumentRoot "D:/zend_framework/HelloWorld" ServerName 'localhost' # This should be omitted in the production environment SetEnv APPLICATION_ENV development <Directory "D:/zend_framework/HelloWorld"> DirectoryIndex /public/index.php AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
此时的端口是8099,需在httpd.conf中添加Listen 8099
二、Hello World实例
1.D:\zend_framework\HelloWorld\public下的index.php代码如下 :
1 <?php 2 error_reporting(E_ALL|E_STRICT); 3 date_default_timezone_set('Asia/Shanghai'); 4 set_include_path('.' .PATH_SEPARATOR .'./library' .PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR .get_include_path()); 5 require_once "Zend/Loader/Autoloader.php"; 6 Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true); //设置Zend Framework 自动载入类文件 7 $registry = Zend_Registry::getInstance(); 8 //设置模板显示路径 9 $view = new Zend_View(); 10 $view->setScriptPath('../application/views/scripts/index'); 11 $registry['view'] = $view;//注册View 12 //设置控制器 13 $frontController = Zend_Controller_Front::getInstance(); 14 $frontController->setBaseUrl('/zendframework')//设置基本路径 15 ->setParam('noViewRenderer', true) 16 ->setControllerDirectory('../application/controllers') 17 ->throwExceptions(true) 18 ->dispatch();
2.D:\zend_framework\HelloWorld\application\controllers下的IndexController.php代码如下 :
1 <?php 2 class IndexController extends Zend_Controller_Action 3 { 4 function init() 5 { 6 $this->registry = Zend_Registry::getInstance(); 7 $this->view = $this->registry['view']; 8 $this->view->baseUrl = $this->_request->getBaseUrl(); 9 } 10 /* 11 * 输出Hello World 的Action(动作)! 12 */ 13 function indexAction() 14 { 15 //这里给变量赋值,在index.phtml模板里显示 16 $this->view->bodyTitle = "****Hello World****"; 17 echo $this->view->render('index.phtml');//显示模版 18 } 19 }
3.D:\zend_framework\HelloWorld\application\views\scripts\index下的index.phtml代码如下 :
1 <?php echo $this->bodyTitle; ?> <!-- 这里输出控制器里Action传过来的值:hello world -->
4.浏览器地址栏中输入:http://localhost:8099/运行后即可: