Zend Framework(二)基礎MVC結構

來源:互聯網
上載者:User

在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是啟用的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.