鍍金池/ 教程/ PHP/ 開始構(gòu)建一個(gè)框架應(yīng)用程序
開始構(gòu)建一個(gè)框架應(yīng)用程序
模塊
路由和控制器
表單和動(dòng)作
結(jié)尾
開始使用 Zend Framework 2
樣式和翻譯
數(shù)據(jù)和模型

開始構(gòu)建一個(gè)框架應(yīng)用程序

為了構(gòu)建我們的應(yīng)用程序,我們先從 GitHub 獲取 ZendSkeletonApplication 源碼。使用 Composer (http://getcomposer.org) 從頭開始創(chuàng)建一個(gè)依賴 Zend Framework 的新項(xiàng)目:

php composer.phar create-project --stability="dev" zendframework/skeleton-application path/to/install

注意

另外一個(gè)下載 ZendSkeletonApplication 的方法是使用 GitHub。進(jìn)入 https://github.com/zendframework/ZendSkeletonApplication 然后點(diǎn)擊 “Zip” 這個(gè)按鈕。這將會(huì)下載一個(gè)文件名類似于 ZendSkeletonApplication-master.zip 或其他相似的文件。

將這個(gè)文件解壓到你保持所有虛擬主機(jī)的地方,并將生成的目錄重命名為 zf2-tutorial。

ZendSkeletonApplication 設(shè)置使用 Composer 來解決它的依賴。這種情況下,這個(gè)依賴就是 Zend Framework 2 它本身。

下載 Zend Framework 2 到我們的應(yīng)用,只要簡單的輸入:

php composer.phar self-update
php composer.phar install

zf2-tutorial 文件夾中,這需要一些時(shí)間。你應(yīng)該可以看到一個(gè)像這樣的輸出:

Installing dependencies from lock file
- Installing zendframework/zendframework (dev-master)
Cloning 18c8e223f070deb07c17543ed938b54542aa0ed8

Generating autoload files

注意

如果你看到這樣的消息:

[RuntimeException]
  The process timed out.

這是說明連接太慢導(dǎo)致無法及時(shí)將全部包下載下來,然后 Composer 超時(shí)。要避免這個(gè)問題,替換一下運(yùn)行命令:

php composer.phar install

用下面這個(gè)代替:

COMPOSER_PROCESS_TIMEOUT=5000 php composer.phar install

注意

使用 Wamp 的 Windows 用戶:

  1. 給 Windows 安裝 Composer。檢查 Composer 是否正確安裝,運(yùn)行:

      composer
  2. 給 Windows 安裝 Git。同時(shí)需要添加 Git 的路徑到 Windows 的環(huán)境配置中。檢查 Git 是否正確安裝,運(yùn)行:

      git
  3. 現(xiàn)在安裝 zf2 使用命令:

      composer create-project -s dev zendframework/skeleton-application path/to/install

我們現(xiàn)在可以移步到 Web 服務(wù)器設(shè)置步驟。

使用 Apache Web Server

你現(xiàn)在需要給應(yīng)用程序建立一個(gè) Apache 虛擬主機(jī)并且編輯你的 host 文件,讓 http://zf2-tutorial.localhost 指定到 zf2-tutorial/public 目錄為 index.php 提供服務(wù)。

設(shè)置虛擬主機(jī)通常是在 httpd.confextra/httpd-vhosts.conf 中。如果你使用的是 extra/httpd-vhosts.conf,需要確保這個(gè)文件包含在你的 httpd.conf 主文件中。一些 Linux 的發(fā)行版本(例如 Ubuntu)會(huì)包裹 Apache 文件夾,配置文件會(huì)存儲(chǔ)在 /etc/apache2 中,并且它會(huì)在 /etc/apache2/sites-enabled 目錄下為每個(gè)虛擬主機(jī)創(chuàng)建一個(gè)文件。這種情況下,你應(yīng)該把虛擬主機(jī)(下面那一塊)放到 /etc/apache2/sites-enabled/zf2-tutorial文件中。

確保已經(jīng)定義 NameVirtualHost,并且設(shè)置成類似于 “*:80”,然后按照這些原則定義一個(gè)虛擬主機(jī):

<VirtualHost *:80>
     ServerName zf2-tutorial.localhost
     DocumentRoot /path/to/zf2-tutorial/public
     SetEnv APPLICATION_ENV "development"
     <Directory /path/to/zf2-tutorial/public>
         DirectoryIndex index.php
         AllowOverride All
         Order allow,deny
         Allow from all
     </Directory>
</VirtualHost>

確保你已經(jīng)更新了 /etc/hosts 或者 c:\windows\system32\drivers\etc\hosts 文件以至于讓 zf2-tutorial.localhost 映射到 127.0.0.1。這樣之后網(wǎng)站就可以通過 http://zf2-tutorial.localhost 訪問了。

127.0.0.1            zf2-tutorial.localhost localhost

重啟 Apache。

如果你正確的完成以上步驟,你將看到類似于這樣的:

http://wiki.jikexueyuan.com/project/zend2-user-guide/images/user-guide.skeleton-application.hello-world.png" alt="" />

測試你的 .htaccess 文件是有效的,導(dǎo)航到 http://zf2-tutorial.localhost/1234 然后你應(yīng)該會(huì)看到:

http://wiki.jikexueyuan.com/project/zend2-user-guide/images/user-guide.skeleton-application.404.png" alt="" />

如果你看到一個(gè)標(biāo)準(zhǔn)的 Apache 404 錯(cuò)誤,繼續(xù)下一步前你應(yīng)該修復(fù) .htaccess 的用法。如果你使用的是 IIS 的 URL Rewrite Module,導(dǎo)入下面的內(nèi)容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [NC,L]

現(xiàn)在你擁有一個(gè)正常工作的框架應(yīng)用程序,我們可以開始給我們的應(yīng)用添加一些細(xì)節(jié)內(nèi)容。

使用 Built-in PHP CLI Server

如果你使用的是 PHP 5.4 或以上版本-你可以使用 Built-in PHP CLI Server(cli-server)。使用這個(gè),你只需要在根目錄開啟這個(gè)服務(wù):

php -S 0.0.0.0:8080 -t public/ public/index.php

這將讓網(wǎng)站在所有網(wǎng)絡(luò)接口的 8080 端口有效,使用 public/index.php 處理路由。這就意味著通過 http://localhost:8080 或者 http://<your-local-IP>:8080 都可以訪問到你的網(wǎng)站。

如果你正確完成以上步驟,你可以看到和上面 Apahce 顯得的同樣的結(jié)果。

測試你的路由是有效的,導(dǎo)航到 http://localhost:8080/1234,你可以看到和上面 Apahce 顯得的同樣的錯(cuò)誤頁面。

注意

內(nèi)置的 CLI server 僅僅只是用于部署。

錯(cuò)誤報(bào)告

當(dāng)你使用 Apache 的時(shí)候,你可以隨意的使用 VirtualHost 里面的 APPLICATION_ENV 設(shè)置讓 PHP 輸出所有瀏覽器上的錯(cuò)誤。在開發(fā)應(yīng)用的時(shí)候使用這個(gè)將會(huì)有很大用處。

編輯 zf2-tutorial/public/ 目錄下的 index.php,用下面的內(nèi)容更改它:

<?php

 /**
  * Display all errors when APPLICATION_ENV is development.
  */
 if ($_SERVER['APPLICATION_ENV'] == 'development') {
     error_reporting(E_ALL);
     ini_set("display_errors", 1);
 }

 /**
  * This makes our life easier when dealing with paths. Everything is relative
  * to the application root now.
  */
 chdir(dirname(__DIR__));

 // Decline static file requests back to the PHP built-in webserver
 if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
     return false;
 }

 // Setup autoloading
 require 'init_autoloader.php';

 // Run the application!
 Zend\Mvc\Application::init(require 'config/application.config.php')->run();