[ZendFramework 2] 頁面接值

在Zend Framework 2頁面接值可使用Zend\Mvc\Controller\Plugin\Params
Zend Controller Plugins
Controller Plugins 預設會被預先載入,所以不需要額外用use載入。

$this->params()->fromRoute(); // Route Match
$this->params()->fromHeader(); // Header
$this->params()->fromQuery(); // Get
$this->params()->fromPost(); // Post
$this->params()->fromFiles(); // Files

 

[ZendFramework] ZendFramework 2 安裝

取得ZF2的libs有兩個方式,一是使用composer,二是從Zend的github clone

 

利用composer安裝
※注意,若以下任何指令失敗並出現「Permission Denied」,請配合sudo操作。

# 先用curl取得composer.phar
curl -sS https://getcomposer.org/installer | php

# 用composer建立專案 (專案名稱以zf2-test為例)
php composer.phar create-project --repository-url="https://packages.zendframework.com" -s dev zendframework/skeleton-application ./zf2-test
cd zf-test/
php composer.phar update

 

若執行 php composer.phar update 出現 「Warning: This development build of composer is over 30 days old. It is recommended to update it by running “composer.phar self-update” to get the latest version.」,則改用self-update

php composer.phar self-update

 

若出現執行逾時的狀況,可加長執行時間

# 出現執行逾時會出現以下訊息
[RuntimeException]
The process timed out.

# 設定執行時間上限為3600秒
COMPOSER_PROCESS_TIMEOUT=3600 php composer.phar install
COMPOSER_PROCESS_TIMEOUT=3600 php composer.phar update

 

從GitHub安裝
※Windows平台建議使用此方法安裝(雖然composer官方有Windows的Installer,but…直接從GitHub拉回來比較省事啦XD)
※Windows使用者請記得將php.exe的路徑加入系統環境變數內,否則你的cmd會不認識php
※Windows使用者若沒有bash環境可用,請自行將第二行的mv視為rename

git clone https://github.com/zendframework/ZendSkeletonApplication.git
mv ZendSkeletonApplication zf2-test
cd zf2-test
php composer.phar self-update
php composer.phar install
php composer.phar update

 

設定Apache
在apache設定檔中加入以下內容,將DNS指到zf2-test/public

<VirtualHost *:80>
    ServerName zf-test.localhost
    DocumentRoot /path/to/zf2-test/public

    <Directory /path/to/zf-test/public>
        DirectoryIndex index.php
        AllowOverride All
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>

        <IfVersion >= 2.3 >
            Require all granted
        </IfVersion>
    </Directory>
</VirtualHost>

 

打開瀏覽器輸入zf-test.localhost,即可看到default page

若沒有DNS的話,也可以開一個port指過去zf2-test/public

NameVirtualHost *:8080
Listen 8080
<VirtualHost *:8080>
    DocumentRoot /path/to/zf2-test/public

    <Directory /path/to/zf2-test/public>
        DirectoryIndex index.php
        AllowOverride All
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>

        <IfVersion >= 2.3 >
            Require all granted
        </IfVersion>
    </Directory>
</VirtualHost>

 

打開瀏覽器輸入localhost:8080,即可看到default page