Zend Framework 框架搭建

经过手工方法搭建Zend Framework的MVC框架结构。首先看一下zend framework mvc的目录结构php

             

1. 在根目录下面建立 public ,并在 public 下建立 index.php引导文件。代码以下:   css

<?php
set_include_path("../library".PATH_SEPARATOR.get_include_path());   //设定路径
require_once 'Zend/Application.php';    //调用zend类库
$application=new Zend_Application('project','../application/configs/application.ini');
$application->bootstrap()->run();

 2. 在 public 目录下建立URL重写文件 .htaccess,代码以下:html

RewriteEngine on
RewriteRule!\.(js|ico|gif|jpg|png|css)$ index.php

 将不能映射到磁盘上的文件都重定向至  index.phpbootstrap

3.  Zend Framework 配置信息保存在扩展名为.ini或者xml文件下。在application 目录下建立 configs/application.ini  文件,代码以下:浏览器

[project]
bootstrap.path="../application/Bootstrap.php"    //启动文件路径
bootstrap.class="Bootstrap"     //启动类名称
phpSettings.display_errors=1     //错误类型
phpSettings.date.timezone="Asia/Shanghai"    //时间区域
resources.frontController.controllerDirectory="../application/controllers"     //控制器路径

 4. 步骤3使用application.ini指定了启动类Bootstrap,本步在application目录下编写启动类,代码以下:mvc

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
    public function __initAutoload(){
        $moduleAutoloader=new Zend_Application_Module_Autoloader(array('namespace'=>'','basePath'=>'application'));
        return $moduleAutoloader;
    }
}

 5. 完成以上动做便可建立控制器。如下在applicatoin/controllers 目录下建立IndexController控制器:app

<?php
class indexController extends Zend_Controller_Action {
    public function indexAction(){
        $this->view->assign("title",'Zend Framework 环境初始搭建');
        $this->view->assign("body",'欢迎您搭建zend framework成功');
    }
}

 6.建立控制器后,还须要建立视图,视图文件位置在views/scripts 文件夹下。scripts文件夹下须要建立与控制器相对应的目录,如下是建立 index/index.phtml 视图代码(其中目录index对应indexController控制器):框架

<html>
    <head>
        <meta charset="utf-8"/>
        <title><?php echo $this->escape($this->title); ?></title>
    </head>
    <body>
        <?php echo $this->escape($this->body); ?>
    </body>    
</html>

 7. 打开浏览器,在地址栏中输入以下url进行访问:网站

    http://127.0.0.1ui

    http://127.0.0.1/index

    http://127.0.0.1/index/index

    以上示例是在IIS中设置网站根目录为public。zendframework默认是访问 indexController 的index 方法。

相关文章
相关标签/搜索