2011-09-01 10:32:12 robin

PHP Framework Symfony 的安裝與入門

21 Mar 2008 - 11:53 in PHP by MoyaTseng

需要的 PHP extensions

  1. Ctype
    1. /usr/ports/textproc/php5-ctype
  2. 提供 XML parsing 用途
    1. /usr/ports/textproc/php5-dom
    2. /usr/ports/textproc/php5-xsl
  3. 連結資料庫 PostgreSQL
    1. /usr/ports/databases/php5-pgsql

安裝 symfony

  1. 安裝 pear
    cd /usr/ports/devel/pear
    make all install clean
    
  2. 增加 pear channel
    pear channel-discover pear.symfony-project.com
    
  3. 察看可用的 symfony libraries
    pear remote-list -c symfony
    
  4. 安裝 symfony framework
    pear install symfony/symfony
    
  5. 確認安裝好的 symfony 版本
    pear install symfony/symfony
    
  6. 安裝好的東西
    • /usr/local/share/pear/data/symfony/ contains the main libraries.
    • /usr/local/share/pear/symfony/ contains the skeleton of symfony applications; default modules; and configuration, i18n data, and so on.
    • /usr/local/share/doc/pear/symfony/ contains the documentation.
    • /usr/local/share/pear/symfony/test/ contains unit tests.

封閉開發環境

  1. 我們使用 authz_unixgroup_module 提供存取網站的認證功能
    cd /usr/ports/www/mod_authz_unixgroup
    make all install clean
    
  2. Ports 的這個 module 預設不開啟,每次更新都要修改 httpd.conf 很麻煩,為了一勞永逸,所以增加 /usr/local/etc/apache22/extra/httpd-extra.conf 檔案,在裡面加入
    LoadModule authz_unixgroup_module libexec/apache22/mod_authz_unixgroup.so
    
  3. 啟用支援外掛程式認證機制的 module
    cd /usr/ports/www/mod_authnz_external
    make all install clean
    
  4. 這個 module 需要用到外掛的程式 pwauth
    cd /usr/ports/security/pwauth
    make all install clean
    
  5. 把以下的設定,加入到設定檔中,即可開啟 pwauth/external/unixgroup 的認證機制
      AddExternalAuth wlog_pwauth /usr/local/bin/pwauth
      SetExternalAuthMethod wlog_pwauth pipe
    
      
        
    
          AuthType Basic
          AuthName "wlog authorize"
          AuthBasicProvider external
          AuthExternal wlog_pwauth
    
          AuthzUnixgroup on
          Require group [unix group]
        
      
    

初始化系統

  1. 建立專案路徑
    cd /usr/local/www/
    mkdir wlog
    cd wlog
    
  2. 初始化專案 symfony init-project wlog 會建立一下檔案
    >> dir+      /usr/local/www/wlog/log
    >> dir+      /usr/local/www/wlog/web
    >> file+     /usr/local/www/wlog/web/robots.txt
    >> dir+      /usr/local/www/wlog/web/js
    >> dir+      /usr/local/www/wlog/web/css
    >> file+     /usr/local/www/wlog/web/css/main.css
    >> dir+      /usr/local/www/wlog/web/images
    >> dir+      /usr/local/www/wlog/web/uploads
    >> dir+      /usr/local/www/wlog/web/uploads/assets
    >> file+     /usr/local/www/wlog/web/.htaccess
    >> dir+      /usr/local/www/wlog/plugins
    >> dir+      /usr/local/www/wlog/batch
    >> dir+      /usr/local/www/wlog/doc
    >> dir+      /usr/local/www/wlog/test
    >> dir+      /usr/local/www/wlog/test/bootstrap
    >> file+     /usr/local/www/wlog/test/bootstrap/unit.php
    >> file+     /usr/local/www/wlog/test/bootstrap/functional.php
    >> dir+      /usr/local/www/wlog/test/functional
    >> dir+      /usr/local/www/wlog/test/unit
    >> dir+      /usr/local/www/wlog/lib
    >> dir+      /usr/local/www/wlog/lib/model
    >> dir+      /usr/local/www/wlog/apps
    >> dir+      /usr/local/www/wlog/config
    >> file+     /usr/local/www/wlog/config/properties.ini
    >> file+     /usr/local/www/wlog/config/config.php
    >> file+     /usr/local/www/wlog/config/databases.yml
    >> file+     /usr/local/www/wlog/config/propel.ini
    >> file+     /usr/local/www/wlog/config/rsync_exclude.txt
    >> file+     /usr/local/www/wlog/config/schema.yml
    >> dir+      /usr/local/www/wlog/cache
    >> dir+      /usr/local/www/wlog/data
    >> dir+      /usr/local/www/wlog/data/model
    >> dir+      /usr/local/www/wlog/data/sql
    >> file+     /usr/local/www/wlog/symfony
    >> tokens    /usr/local/www/wlog/config/properties.ini
    >> tokens    /usr/local/www/wlog/config/propel.ini
    >> tokens    /usr/local/www/wlog/config/propel.ini
    >> tokens    /usr/local/www/wlog/config/config.php
    >> chmod 777 /usr/local/www/wlog/cache
    >> chmod 777 /usr/local/www/wlog/log
    >> chmod 777 /usr/local/www/wlog/web/uploads
    >> chmod 777 /usr/local/www/wlog/symfony
    >> chmod 777 web/uploads/assets
    
  3. 初始化第一個功能模組,網站的首頁 symmfony init-app main 會建立以下檔案
    >> dir+      /usr/local/www/wlog/apps/main/modules
    >> dir+      /usr/local/www/wlog/apps/main/lib
    >> file+     /usr/local/www/wlog/apps/main/lib/myUser.class.php
    >> dir+      /usr/local/www/wlog/apps/main/templates
    >> file+     /usr/local/www/wlog/apps/main/templates/layout.php
    >> dir+      /usr/local/www/wlog/apps/main/i18n
    >> dir+      /usr/local/www/wlog/apps/main/config
    >> file+     /usr/local/www/wlog/apps/main/config/i18n.yml
    >> file+     /usr/local/www/wlog/apps/main/config/logging.yml
    >> file+     /usr/local/www/wlog/apps/main/config/config.php
    >> file+     /usr/local/www/wlog/apps/main/config/routing.yml
    >> file+     /usr/local/www/wlog/apps/main/config/view.yml
    >> file+     /usr/local/www/wlog/apps/main/config/security.yml
    >> file+     /usr/local/www/wlog/apps/main/config/filters.yml
    >> file+     /usr/local/www/wlog/apps/main/config/app.yml
    >> file+     /usr/local/www/wlog/apps/main/config/cache.yml
    >> file+     /usr/local/www/wlog/apps/main/config/factories.yml
    >> file+     /usr/local/www/wlog/apps/main/config/settings.yml
    >> tokens    /usr/local/www/wlog/apps/main/config/settings.yml
    >> file+     /usr/local/www/wlog/web/index.php
    >> file+     /usr/local/www/wlog/web/main_dev.php
    >> tokens    /usr/local/www/wlog/web/index.php
    >> tokens    /usr/local/www/wlog/web/main_dev.php
    >> chmod 777 /usr/local/www/wlog/cache
    >> chmod 777 /usr/local/www/wlog/log
    >> chmod 777 /usr/local/www/wlog/web/uploads
    >> chmod 777 /usr/local/www/wlog/symfony
    >> chmod 777 web/uploads/assets
    >> dir+      /usr/local/www/wlog/test/functional/main
    
  4. 設定 apache22 可以存取這個專案
    1. 一般設定
      
        ServerName [virtual hostname]
        DocumentRoot "/usr/local/www/wlog/web"
        DirectoryIndex index.php
        Alias /sf /usr/local/share/pear/data/symfony/web/sf
        # 以下設定為開啟 symfony 專案的目錄允許 remote web access
        
          Order allow,deny
          Allow from All
          AllowOverride All
        
        # 以下設定為允許取用 symfony 的基本構成的物件 web access 需求
        
          Order allow,deny
          Allow from All
        
      
      
    2. 包含存取限定的設定
      
      
        ServerName wlog.iyard.org
        DocumentRoot "/usr/local/www/wlog/web"
        DirectoryIndex index.php
        Alias /sf /usr/local/share/pear/data/symfony/web/sf
      
        AddExternalAuth wlog_pwauth /usr/local/bin/pwauth
        SetExternalAuthMethod wlog_pwauth pipe
      
        
      
          Order allow,deny
          Allow from All
          AllowOverride All
      
          
      
            AuthType Basic
            AuthName "wlog authorize"
            AuthBasicProvider external
            AuthExternal wlog_pwauth
      
            AuthzUnixgroup on
            Require group [unix group]
          
      
        
      
        
          Order allow,deny
          Allow from All
        
      
      
      
  5. 要讓 apache22 支援 rewrite module
    LoadModule rewrite_module libexec/apache22/mod_rewrite.so
    
  6. 因為是第一個 application,所以 main.php 不會存在,會直接以 index.php 為入口。如果有需要的話,可以先把 index.php rename 為 main.php 即可。
  7. 測試環境版本的程式 URL 為:http://[hostname]/main_dev.php,可以在 log 目錄下看到執行的 debug 訊息,提供參考。

建立 Database Schema 原型

  1. 選擇採用先建立資料庫,由資料庫產生 schema 設定檔案的方式來設定會比較簡單
  2. 建立資料庫及帳號
    # createuser -S -D -R -U pgsql -P wlog
    # createdb -U pgsql -O wlog wlog
    
  3. 設定連線存取權限 [pgsql data dir]/pg_hba.conf
    # TYPE  DATABASE    USER   CIDR-ADDRESS    METHOD
    local   wlog        wlog                   md5
    host    wlog        wlog   [Access IP]/32  md5
    
  4. 重新載入設定檔案
    /usr/local/etc/rc.d/postgresql reload
    
  5. 在這邊以建立一個會員系統為範例,會員註冊帳號基本需求,一個帳號、序號及密碼
    # psql -U wlog wlog
    psql> create table users (user_id serial primary key not null, user_login varchar(20) not null unique, user_passwd varchar(80) not null, user_create_time timestamp);
    
  6. 編輯 /usr/local/www/symfony/config/propel.ini 的設定,為將資料庫的 Schema 倒出來做準備
    propel.database            = pgsql
    propel.database.createUrl  = pgsql://[account]:[password]@[hostname]/
    propel.database.url        = pgsql://[account]:[password]@[hostname]/[database]
    
  7. 倒出資料庫的指令,執行完畢後會產生 config/schema.yml 檔案
    symfony propel-build-schema
    

建立基本程式樣板

  1. 基本模組的建立,會在 lib/model 下建立一些提供 MVC 架構的原型程式相關的基本功能 model
    symfony propel-build-model
    
  2. 建立出來的檔案包括
    lib/model/om/BaseUsersPeer [builder: SfPeerBuilder]
    lib/model/om/BaseUsers [builder: SfObjectBuilder]
    lib/model/map/UsersMapBuilder [builder: SfMapBuilderBuilder]
    lib/model/UsersPeer [builder: SfExtensionPeerBuilder]
    lib/model/Users [builder: SfExtensionObjectBuilder]
    
  3. 接著要建立 MVC 架構下的 controller 程式
    symfony propel-generate-crud main users Users
    
  4. 會建立一批 controller (actions), view (templates) 的檔案
    >> dir+      /usr/local/www/wlog/apps/main/modules/users/actions
    >> file+     /usr/local/www/wlog/apps/main/modules/users/actions/actions.class.php
    >> dir+      /usr/local/www/wlog/apps/main/modules/users/templates
    >> file+     /usr/local/www/wlog/apps/main/modules/users/templates/listSuccess.php
    >> file+     /usr/local/www/wlog/apps/main/modules/users/templates/editSuccess.php
    >> file+     /usr/local/www/wlog/apps/main/modules/users/templates/showSuccess.php
    >> tokens    /usr/local/www/wlog/apps/main/modules/users/actions/actions.class.php
    >> tokens    /usr/local/www/wlog/apps/main/modules/users/actions/actions.class.php
    >> tokens    /usr/local/www/wlog/apps/main/modules/users/templates/listSuccess.php
    >> tokens    /usr/local/www/wlog/apps/main/modules/users/templates/editSuccess.php
    >> tokens    /usr/local/www/wlog/apps/main/modules/users/templates/showSuccess.php
    >> file+     /usr/local/www/wlog/test/functional/main/usersActionsTest.php
    >> tokens    /usr/local/www/wlog/test/functional/main/usersActionsTest.php
    
  5. 清除既有的 cache 檔案,這樣功能才會正常,不然 symfony 會優先使用 cache 的檔案,其中 main 是 model 的名字
    symfony cc main config