李超

cc编程笔记本。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在ZF中的MVC结构中,目录结构是这样的:

application/
    controllers/
        IndexController.php
    models/
    views/
        scripts/
            index/
                index.phtml
        helpers/
        filters/
html/
    .htaccess
    index.php

在Apache中新建一个alias,目录指向html/下,.htaccess是apache的目录配置文件,里面存放了一些指令,可以改变apache的一些访问规则,比如404出错提示信息等等,我们这里要放的是urlrewrite,URL重定向信息。

将以下信息复制到.htaccess中:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
这些信息的意思是除了一些资源文件外,其他后缀名的文件全部重定向到index.php文件。


这样,index.php就接收了该站所有的请求,这时候index.php就被称为 bootstrap 文件,所谓bootstrap,就是一小段引导程序或者入口程序,index.php的内容如下:
<?php
require_once 'Zend/Controller/Front.php';
Zend_Controller_Front
::run('../application/controllers');

以上代码的意思是启动Zend_Controller_Front,ZendFront是前端控制器,负责解析URL,将请求分配给不同的控制器。

然后我们创建一个默认的控制器,在application/controllers目录下创建一个IndexController.php文件,内容如下:
<?php
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
    
public function indexAction()
    {
    }
}


一个控制器就建立好了,可以看到,控制器就是从Zend_Conttroller_Action继承的一个类,并且以Controller结尾,这是ZendFramework的MVC结构中的约定,而URL中的第一部分会被解释为控制器,并与我们这里所创建的控制器一一对应起来。
IndexConttroller类中有一个方法,叫做indexAction,这个方法与URL中的第二部分对应起来。
也就是说,上面建立的这个IndexController控制器的IndexAction方法,对应的URL就是: /index/index,访问这个URL就会调用indexAction方法。

在默认情况下,ViewRenderer是启用的。
posted on 2008-05-09 16:59  coderlee  阅读(1586)  评论(1编辑  收藏  举报