Zend_Controller Quick Start

Zend_Controller Quick Start

P.S:學習Zend1.12,閱讀文檔時翻譯,關鍵詞語並沒有翻譯,這樣比較好理解(2017.02.15)

介紹

Zend_controller 是Zend框架MVC系統的核心。MVC代表Model-View-Controller,是一種設計模式,目的是從視圖邏輯(display logic) 中分離出應用邏輯(application logic)。
Zend_Controller_Front實現的是Front Controller模式(參見P of EAA),所有的請求都會被front controller截獲,並被分派(dispatch)到對應URL的Action Controller。
Zend_Controller擁有可擴展的設計理念,通過子類繼承現有類,來實現基於the controller family of classes的多種接口(interfaces)和抽象類(abstract classes),或者通過寫plugins or action helpers來增加系統的功能性。

Quick Start

如果你需要更多深層的信息,看接下來的sections。如果你只是想快速建立並運行,請繼續讀。

Create the Filesystem Layout

第一步是創建你的文件系統佈局(file system layout)。典型的佈局如下

application/
    controllers/
        IndexController.php
    models/
    views/
        scripts/
            index/
                index.phtml
        helpers/
        filters/
public/(這裏名字可設置,源文檔是http)
    .htaccess
    index.php

Set the Document Root

在你的web server中,設置document root 爲public目錄。。。

Create the Rewrite Rules

修改public/.htaccess 文件如下(默認的.htaccess寫法,沒有它就不能正常訪問)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Note:Learn about mod_rewrite
上述的rewrite規則允許訪問vhost下所有文件。如果有哪些文件你不希望這樣做,你可能想要更嚴格的rules。去Apache官網學習更多關於mod_write

如果使用IIS7.0,使用以下rewrite配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
     <system.webServer>
         <rewrite>
             <rules>
                 <rule name="Imported Rule 1" stopProcessing="true">
                     <match url="^.*$" />
                     <conditions logicalGrouping="MatchAny">
                         <add input="{REQUEST_FILENAME}"
                             matchType="IsFile" pattern=""
                             ignoreCase="false" />
                         <add input="{REQUEST_FILENAME}"
                             matchType="IsDirectory"
                             pattern="" ignoreCase="false" />
                     </conditions>
                     <action type="None" />
                 </rule>
                 <rule name="Imported Rule 2" stopProcessing="true">
                     <match url="^.*$" />
                     <action type="Rewrite" url="index.php" />
                 </rule>
             </rules>
         </rewrite>
     </system.webServer>
</configuration>

以上的Rules將會按照要求去路由請求(route requests)存在的資源(存在的符號鏈接(symlinks)、非空文件、非空文件夾),和一些其他的通往front controller 的 requests 。

Note:以上的rewrite rules是Apache的;如果是其他web server的例子,參見router documentation

創建Bootstrap File(引導程序文件)

Bootstrap文件是route所有request的頁面——public/index.php。你可以選擇用編輯器打開public/index.php並添加如下:

Zend_Controller_Front::run('/path/to/app/controllers');

這將實例化和調用(dispatch)那些route request到action controller的front controller。

創建默認的Action controller

在討論Action controller之前,你應該先理解request是如何在zend框架中route的。默認情況下URL path的第一段是通向controller的,第二段通向action。例如,給出URL http://framework.zend.com/roadmap/components,path就是roadmap/components,會導向controller roadmap和action組件。如果沒有提供action,默認提供index爲action,如果沒有提供controller,提供index爲默認controller(遵守Apache規定自動導向DirectoryIndex)。
Zend_Controller的dispatcher將會把controller value 和maps帶到一個class中。默認情況下,這會把controller標題封裝(Title-case),尾部跟上一個Controller。因此,剛纔的例子中,我們的controller會導向類RoadmapController
同樣的,action value 會導向controller類中的一個method。默認情況下,這個value是小寫的,還會在後面跟上Action。因此上個例子中,這個action組件變爲componentsAction(),最終的method成爲RoadmapController::componentsAction()
So,接下來,現在讓我們創建一個默認action controller 和action method。鑑於之前的Note,默認的controller和action都稱爲index。打開文件application/controllers/IndexController.php,並添加如下:

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

默認情況下,ViewRenderer(視圖渲染器) action helper 是可用的。意思就是說簡單定義一個action method和對應的view script,你將立刻得到提供的content。默認情況下(怎麼老是這句)Zend_View是在MVC中作爲視圖層的。這個ViewRenderer做了一些magic的事,使用了controller name(e.g.,index)和現在的action name(e.g.,index)去決定使用什麼模板(template)。默認情況下,模板(template)在.phtml擴展名的結尾,也就是說在上面的例子中,模板(template)index/index.phtml將被渲染(renderered)。另外,ViewRenderer自動假設目錄views/爲同級controller目錄的基礎視圖目錄,實際的view腳本存放在views/scripts/子目錄。因此,我們可以在application/views/scripts/index/index.phtml裏找到我們的模板。

創建View Script

在之前提到的章節(quick start)裏,view腳本都放在application/views/scripts/;對應默認controller和action的view腳本在application/views/scripts/index/index.phtml。創建並寫點HTML:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>My first Zend Framework App</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

就是個hello world。

創建Error Controller

默認情況下,error handler plugin(錯誤處理程序組件)是已經註冊好了的。這個組件是個用來處理error的controller。默認情況下,它假設ErrorController是一個默認模塊並帶有如下errorAction()方法:

class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
    }
}

按照已經假定好的目錄佈局,這個文件應該放在application/controllers/ErrorController.php。你將需要創建個view視圖在application/views/scripts/error/error.phtml;樣例內容可能是這樣:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Error</title>
</head>
<body>
    <h1>An error occurred</h1>
    <p>An error occurred; please try again later.</p>
</body>
</html>

瀏覽你的網站

圍繞着你第一個建立的controller和view,你可以啓動你的瀏覽器去瀏覽你的網站。假設example.com是你的域名,如下所有的URLs都可以訪問:

現在,你可以去創建更多的controllers和action方法了。Congratulations!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章