Phalcon提供的這個開發工具主要是用來輔助開發,比方生成一些程序的基本框架。生成控制器模型等。使用這個工具我們僅僅須要一個簡單的命令就可以生成應用的基本框架。
很重要: 要使用這個工具我們必需要安裝Phalcon 0.5版本號以上的擴展才行。這里我們推薦使用PHP5.3.6或更高版本號的PHP. 假設你喜歡使用web版而非console版本號的程序,那么在這里 blog post 能夠看到很多其它的內容。
獲取可用的命令(Getting Available Commands)¶
我們能夠在虛擬控制台上輸入例如以下命令:phalcon commands
$ phalcon commands
Phalcon DevTools (1.2.3)
Available commands:
commands (alias of: list, enumerate)
controller (alias of: create-controller)
model (alias of: create-model)
all-models (alias of: create-all-models)
project (alias of: create-project)
scaffold
migration
webtools
生成項目框架(Generating a Project Skeleton)¶
我們能夠使用Phalcon開發輔助工具生成預先定義的項目架構。 默認情況下,phalcon開發輔助工具會根據apache的mod_rewrite規則來生成程序的骨架. 要創建項目我們僅僅須要在我們的 webserver根文件夾下輸入例如以下命令:
$ pwd
/Applications/MAMP/htdocs
$ phalcon create-project store
運行命令后會生成例如以下的文檔結構的項目:

我們能夠在命令上加 –help 以顯示幫助信息(以下的幫助中的中文是翻譯時加上去的):
Phalcon DevTools (1.2.3)
- Help:
- Creates a project 創建項目
- Usage:
- project [name] [type] [directory] [enable-webtools]
- Arguments: 參數
- help Shows this help text 顯示此幫助信息
- Example 樣例
- phalcon project store simple
- 選項:
-
--name 新項目的名字 --enable-webtools 是否使用webtools開發輔助組件[可選] --directory=s 在何處創建項目[可選] --type=s 應用的種類(微型,簡單,多模塊,console等) --template-path 指定模板路徑[可選] --use-config-ini 使用ini文件作為配置保存文件[可選] --trace 出錯時是否顯示框架的trace信息[可選] --help 顯示幫助
訪問新生成項目的地址顯演示樣例如以下:

生成控制器(Generating Controllers)¶
我們能夠使用phalcon create-controller –name test或phalcon controller –name test來生成名為test的控制器. 當然要使用此命令當前的運行命令文件夾必須為已存在的phalcon項目內.
$ phalcon create-controller --name test
上面的命令會生成例如以下代碼:
<?php
class TestController extends Phalcon\Mvc\Controller
{
public function indexAction()
{
}
}
數據庫配置(Preparing Database Settings)¶
當我們使用phalcon的輔助開發工具生成項目時,則生成的配置信息會被放在 app/config/config.ini 文件內。
我們必需要正確的配置連接信息才可生成模型或主要的CRUD操作。 能夠在config.ini中進行改動配置信息:
[database]
adapter = Mysql
host = "127.0.0.1"
username = "root"
password = "secret"
dbname = "store_db"
[phalcon]
controllersDir = "../app/controllers/"
modelsDir = "../app/models/"
viewsDir = "../app/views/"
baseUri = "/store/"
生成模型(Generating Models)¶
使用phalcon開發輔助工具我們能夠有若干種方式來生成模型。
我人能夠有選擇的生成若干個模型或是所有生成。
亦能夠指定生成公有屬性或是生成setter和getter方法。
- Options:
-
--name=s 表名 --schema=s schema名[可選] --namespace=s 模型命名空間[可選] --get-set 設置字段訪問屬性為私有 並加入setters/getters方法[可選] --extends=s 指定擴展類名[可選] --doc 輔助IDE的自己主動完畢功能[可選] --directory=s 項目的根文件夾[可選] --force 重寫模型[可選] --trace 出錯時顯示框架trace信息[可選] --mapcolumn 生成字段映射的代碼[可選]
最簡單的生成模型的方式:
$ phalcon model products
$ phalcon model --name tablename
全部的字段設置為公有:
<?php
class Products extends \Phalcon\Mvc\Model
{
/**
* @var integer
*/
public $id;
/**
* @var integer
*/
public $types_id;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $price;
/**
* @var integer
*/
public $quantity;
/**
* @var string
*/
public $status;
}
我們能夠在生成模型時指定 –get-set 參數以實現對字段的保護, 這樣我們能夠在setter/getter方法里運行一些業務邏輯。
<?php
class Products extends \Phalcon\Mvc\Model
{
/**
* @var integer
*/
protected $id;
/**
* @var integer
*/
protected $types_id;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $price;
/**
* @var integer
*/
protected $quantity;
/**
* @var string
*/
protected $status;
/**
* Method to set the value of field id
* @param integer $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Method to set the value of field types_id
* @param integer $types_id
*/
public function setTypesId($types_id)
{
$this->types_id = $types_id;
}
...
/**
* Returns the value of field status
* @return string
*/
public function getStatus()
{
return $this->status;
}
}
還有一個很好的特性即是在我們多次生成模型時,原有的對模型的更改依舊會存在。
這樣我們就能夠不用操心對模型的屬性進行的改動會被后來再次運行的模型生成命令所覆蓋。
以下的截圖顯示了這是怎樣工作的:
生成主要的 CRUD(Scaffold a CRUD)¶
使用phalcon開發輔助工具我們能夠直接高速的生成一個模型的CRUD操作。 假設我們想高速的生成模型的CRUD操作僅僅須要使用phalcon輔助開發工具的中scaffold命令就可以。
代碼生成后,你能夠依據自己的須要改動生成的代碼。
非常多開發人員可能不會去使用這個功能。事實上這東西有時不是太好用,非常多時候開發人員往往會手動的書寫相關代碼。使用scaffold產生的代碼能夠 幫助我們理解框架是怎樣工作的當然也能夠幫助我們制作出高速原型來。
以下的截圖展示了基於products表的scaffold:
$ phalcon scaffold --table-name test
scaffold生成器會在相關的目錄中生成若干個文件。 以下是所生成文件的概覽:
文件 | 作用 |
---|---|
app/controllers/ProductsController.php | Products控制器 |
app/models/Products.php | Products模型 |
app/views/layout/products.phtml | Products控制器布局 |
app/views/products/new.phtml | View for the action “new” |
app/views/products/edit.phtml | View for the action “edit” |
app/views/products/search.phtml | View for the action “search” |
app/views/products/edit.phtml | View for the action “edit” |
在生成的Products控制器中。我們能夠看到一個搜索表單和一個生成新product的鏈接:

在創建頁面我們能夠生成經過驗證的Products記錄。 Phalcon會自己主動的驗證數據庫中的非空字段。

運行搜索后,分頁組件會顯示分頁后的結果。 我們在結果列表的前面放置Edit或Delete鏈接。以實現對應的操作。

工具的 Web 界面(Web Interface to Tools)¶
另外。假設喜歡我們還能夠在生成項目時通過加入參數以實如今項目中使用Phalcon開發工具的web接口。
以下的視頻中展示了怎樣工作的:
集成工具到 PhpStorm(Integrating Tools with PhpStorm IDE)¶
以下的視頻中展示了怎樣在 PhpStorm IDE 中集成輔助開發工具。 這個配置步驟也適用於其他的PHP IDE.