Zend Framework配置:Hello World

来源:互联网 发布:沙皇尼古拉一世数据 编辑:程序博客网 时间:2024/05/02 01:20

由于项目需要用到Zend Framework框架,所以现在开始深入学习这个框架。第一课总是,输出Hello World。

配置Zend Framework运行的PHP环境

首先确认你的PHP环境,Zend Framework 要求 PHP版本不低于5.1.4,但强烈建议使用 5.2.3 或更高版本,因为在这两个版本之间有许多重大安全和性能方面的改善和提高。

PHP环境配置好了之后,请打开php.ini文件,确认PDO扩展是否打开。如果没有请把extension=php_pdo.dll之前的;号给去掉。

打开APACHE文件夹里面的httpd.conf文件,查找到apache的mod_rewrite模块,确认LoadModule rewrite_module modules/mod_rewrite.so是否打开。如果没有请去掉它前面的#号。

查找到httpd.conf文件,如果AllowOverride为None的话,请一定把None都改成all,这样你写.htaccess这样的文件才会起到作用。

重新启动你的APACHE服务器,这样我们的PHP环境就可以运用Zend Framewrok了。

配置Zend Framework项目

项目文件夹如下:

下面介绍下需要修改的文件名与其代码。

.htaccess代码:

[php] view plaincopy
  1. RewriteEngine on  
  2. RewriteRule .* index.php  
  3.   
  4. php_flag magic_quotes_gpc off  
  5. php_flag register_globals off  
  6. php_flag short_open_tag on  

index.php(网站入口)文件及说明:

[php] view plaincopy
  1. <?php  
  2.     /* 
  3.      * Date: 2010.11.19 
  4.      * Author:Gonn By www.nowamagic.net 
  5.      * Email:gonnsai@163.com 
  6.      * QQ:252211974 
  7.      * Blog:http://www.nowamagic.net 
  8.      */  
  9.     error_reporting(E_ALL|E_STRICT);  
  10.     date_default_timezone_set('Asia/Shanghai');  
  11.       
  12.     set_include_path('.' .PATH_SEPARATOR .'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR . get_include_path());  
  13.     //require_once 'Zend/Loader.php';  
  14.     //Zend_Loader::registerAutoload();      //设置Zend Framework 自动载入类文件  
  15.     require_once "Zend/Loader/Autoloader.php";  
  16.     Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);  
  17.     $registry = Zend_Registry::getInstance();   
  18.     $view = new Zend_View();  
  19.     $view->setScriptPath('./application/views/scripts/');    //设置模板显示路径  
  20.     $registry['view'] = $view;  //注册View  
  21.     //设置控制器  
  22.     $frontController =Zend_Controller_Front::getInstance();  
  23.     $frontController->setBaseUrl('/zendframework')   //设置基本路径  
  24.                     ->setParam('noViewRenderer', true)  
  25.                     ->setControllerDirectory('./application/controllers')  
  26.                     ->throwExceptions(true)  
  27.                     ->dispatch();  
  28. ?>  



IndexController.php文件及说明:

[php] view plaincopy
  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.       
  12.     /* 
  13.      * 输出Hello World 的Action(动作)! 
  14.      */  
  15.     function indexAction()   
  16.     {   
  17.         //这里给变量赋值,在index.phtml模板里显示  
  18.         $this->view->bodyTitle = '<h1>Hello World!</h1>';  
  19.         echo $this->view->render('index.phtml');//显示模版    
  20.     }   
  21. }  
  22. ?>  


index.phtml模板文件说明:

[html] view plaincopy
  1. <?=$this->bodyTitle; ?><!-- 这里输出控制器里Action传过来的值:hello world -->  


 

在浏览器输入:http://localhost/zendframework/,即可输出Hello World。

PS:关于错误提示Zend_Loader::Zend_Loader::registerAutoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead

从1.8.0版本开始不推荐使用Zend_Loader::autoload,Zend_Loader::autoload会在2.0.0版本中移除,推荐使用Zend_Loader_Autoloader来替代Zend_Loader::autoload.

如果将

[php] view plaincopy
  1. require_once('Zend/Loader.php');   
  2. Zend_Loader::registerAutoload();  


改成:

[php] view plaincopy
  1. require_once 'Zend/Loader/Autoloader.php';  
  2. Zend_Loader_Autoloader::getInstance();  


 

会提示Fatal error: Class 'Templater' not found in /var/www/phpweb20/htdocs/index.php on line 35

我想应该是加载类失败吧,因为路径里面明明就有'Templater'类,那问题应该还是出现在Zend_Loader_Autoloader中。

只要改为

[php] view plaincopy
  1. require_once "Zend/Loader/Autoloader.php";  
  2. Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);  

 

就OK了!
0 0
原创粉丝点击