为了应付蛋疼的作业,学了一周的Zend,既然学了,不能浪费啊,将经验总结下吧。其中可能有不正确或者完全是本人自己的想法,可能跟官方的标准做法不同,望各位指正。 首先是Zend的安装
然后就可以开始配置运行环境了
接下来介绍下各个目录
然后就是一个关键的问题,怎样从Controller的Action中向view中传值以及实现一个页面的完整流程了
<?php class IndexController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // action body //我们在这里加一句 $this->view->content = "Hello World!!!"; } }
<style> a:link, a:visited { color: #0398CA; } span#zf-name { color: #91BE3F; } div#welcome { color: #FFFFFF; background-image: url(http://framework./images/bkg_header.jpg); width: 600px; height: 400px; border: 2px solid #444444; overflow: hidden; text-align: center; } div#more-information { background-image: url(http://framework./images/bkg_body-bottom.gif); height: 100%; } </style> <div id="welcome"> <h1>Welcome to the <span id="zf-name">Zend Framework!</span></h1> <h3>This is your project's main page</h3> <? echo $this->content;?><!--在这里输出,注意,这里的$this指的是action中的$this->view--> <div id="more-information"> <p><img src="http://framework./images/PoweredBy_ZF_4LightBG.png" /></p> <p> Helpful Links: <br /> <a href="http://framework./">Zend Framework Website</a> | <a href="http://framework./manual/en/">Zend Framework Manual</a> </p> </div> </div>
接下来介绍下根目下的俩个文件: 首先是.htacess
RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
<?php // 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(), ))); /** 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();
<?php // 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(), realpath(APPLICATION_PATH . '/forms'),//设置了两个目录用来放专门的独立文件 realpath(APPLICATION_PATH . '/models'),//把这个目录也包含进来了,因为不知道还有什么其他方法~~>_<~~ ))); require_once 'Zend/Db.php'; $params = array( 'host' => '127.0.0.1', 'username' => 'root', 'password' => '', 'dbname' => 'market', 'charset' => 'UTF8', 'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8' ), ); $db = Zend_Db::factory('PDO_MYSQL', $params); //require_once 'Zend/Registry.php'; //Zend_Registry::set('db', $db); require_once 'Zend/Db/Table.php'; Zend_Db_Table::setDefaultAdapter($db);//我是用继承Zend_Db_Table的方法实现model的,因此直接将$db设置给Zenf_Db_Table,你也可以用Zend_Registry注册变量的方法 /** Setup layout */ require_once 'Zend/Layout.php'; Zend_Layout::startMvc(APPLICATION_PATH . '/views');//MVC中的被我称为母版页的初始化程序,它默认为layout.phtml /** 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(); 还有些杂碎的提醒:
<?php class LoginForm extends Zend_Form { public function init() { $this->setMethod('post'); $this->addElement('text', 'email', array( 'validators' => array( array('regex', false, '/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/i') ), 'required' => true, 'filters' => array('StringToLower'), 'label' => '邮箱:', 'decorators' => array( array('ViewHelper', array('helper' => 'formText')), array('Label', array( 'class' => 'label', 'tag' => 'div', )), array('HtmlTag', array( 'tag' => 'div', 'class' => 'element', )), ), )); $this->addElement('password', 'password', array( 'required' => true, 'label' => '密码:', 'decorators' => array( array('ViewHelper', array('helper' => 'formPassword')), array('Label', array( 'class' => 'label', 'tag' => 'div', )), array('HtmlTag', array( 'tag' => 'div', 'class' => 'element', )), ), )); $this->addElement('submit', '提交', array( 'decorators' => array( array('ViewHelper', array('helper' => 'formSubmit')), array('HtmlTag', array( 'tag' => 'div', 'class' => 'submit', )), ), )); } } ?>Zend_Db_Table的例子: <?php class UserModel extends Zend_Db_Table { protected function _setup() { $this->_name = 'user'; $this->_primary = 'uid'; parent::_setup(); } public function emailExist($email) { $select = parent::select()->where('email = ?', $email); $result = $this->getAdapter()->fetchOne($select); if (!empty($result)) return true; else return false; } } ?> 好了,就先介绍到这,再说一次,上面的例子都是参考,可能不太规范, 网上的zend的学习资料很多,手册也挺详细的,毕竟是php的官方都推荐的,是个不错的框架,也方便使用,推荐大家想快速开发网站的时候用。http://www./linuxbiancheng/14003.html |
|