[TOC]
# Phalcon開發者工具
這些工具是生成框架代碼的有用腳本的集合。可以使用簡單的命令生成應用程序的核心組件,從而可以使用Phalcon輕松開發應用程序。
>[danger] 如果您更喜歡使用Web版本而不是控制臺,則此[博客文章](https://blog.phalconphp.com/post/dont-like-command-line-and-consoles-no-problem)提供了更多信息。
## 下載
您可以從[Github](https://github.com/phalcon/phalcon-devtools)下載包含開發人員工具的跨平臺軟件包。
## 安裝
`Phalcon開發者工具的安裝`章節有有關如何在不同平臺上安裝開發人員工具的詳細說明。
## 可用命令
您可以通過輸入`phalcon commands`獲取Phalcon工具中可用命令的列表
```bash
$ phalcon commands
Phalcon DevTools (3.0.0)
Available commands:
commands (alias of: list, enumerate)
controller (alias of: create-controller)
module (alias of: create-module)
model (alias of: create-model)
all-models (alias of: create-all-models)
project (alias of: create-project)
scaffold (alias of: create-scaffold)
migration (alias of: create-migration)
webtools (alias of: create-webtools)
```
## 生成項目架構
您可以使用Phalcon工具為Phalcon框架的應用程序生成預定義的項目框架。默認情況下,項目框架生成器將使用`mod_rewrite`用于Apache。在Web服務器文檔根目錄中鍵入以下命令:
```bash
$ pwd
/Applications/MAMP/htdocs
$ phalcon create-project store
```
生成了以下推薦的項目結構:

您可以添加參數`--help`以獲取有關某個腳本用法的幫助:
```bash
$ phalcon project --help
Phalcon DevTools (3.0.0)
Help:
Creates a project
Usage:
project [name] [type] [directory] [enable-webtools]
Arguments:
help Shows this help text
Example
phalcon project store simple
Options:
--name Name of the new project
--enable-webtools Determines if webtools should be enabled [optional]
--directory=s Base path on which project will be created [optional]
--type=s Type of the application to be generated (cli, micro, simple, modules)
--template-path=s Specify a template path [optional]
--use-config-ini Use a ini file as configuration file [optional]
--trace Shows the trace of the framework in case of exception. [optional]
--help Shows this help
```
從Web服務器訪問項目將顯示:

## 生成控制器
命令`create-controller` 生成控制器骨架結構。在已有Phalcon項目的目錄中調用此命令很重要。
```bash
$ phalcon create-controller --name test
```
腳本生成以下代碼:
```php
<?php
use Phalcon\Mvc\Controller;
class TestController extends Controller
{
public function indexAction()
{
}
}
```
## 數據庫預設置
使用開發人員工具生成項目時。配置文件可以在`app/config/config.php` 中找到。要生成模型或腳手架,您需要更改用于連接數據庫的設置。
更改`config.php`文件中的數據庫部分:
```php
<?php
defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');
return new \Phalcon\Config([
'database' => [
'adapter' => 'Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'secret',
'dbname' => 'test',
'charset' => 'utf8',
],
'application' => [
'appDir' => APP_PATH . '/',
'controllersDir' => APP_PATH . '/controllers/',
'modelsDir' => APP_PATH . '/models/',
'migrationsDir' => APP_PATH . '/migrations/',
'viewsDir' => APP_PATH . '/views/',
'pluginsDir' => APP_PATH . '/plugins/',
'libraryDir' => APP_PATH . '/library/',
'cacheDir' => BASE_PATH . '/cache/',
// This allows the baseUri to be understand project paths that are not in the root directory
// of the webpspace. This will break if the public/index.php entry point is moved or
// possibly if the web server rewrite rules are changed. This can also be set to a static path.
'baseUri' => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]),
]
]);
```
<a name='generating-models'></a>
## 生成模型
有幾種方法可以創建模型。您可以從默認數據庫連接或某些選擇性創建所有模型。模型可以具有字段表示的公共屬性,也可以使用setter/getter。
```bash
Options:
--name=s Table name
--schema=s Name of the schema. [optional]
--namespace=s Model's namespace [optional]
--get-set Attributes will be protected and have setters/getters. [optional]
--extends=s Model extends the class name supplied [optional]
--excludefields=l Excludes fields defined in a comma separated list [optional]
--doc Helps to improve code completion on IDEs [optional]
--directory=s Base path on which project will be created [optional]
--force Rewrite the model. [optional]
--trace Shows the trace of the framework in case of exception. [optional]
--mapcolumn Get some code for map columns. [optional]
--abstract Abstract Model [optional]
```
生成模型的最簡單方法是:
```bash
$ phalcon model products
```
```bash
$ phalcon model --name tablename
```
所有表字段都聲明為public,以便直接訪問。
```php
<?php
use Phalcon\Mvc\Model;
class Products extends Model
{
/**
* @var integer
*/
public $id;
/**
* @var integer
*/
public $typesId;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $price;
/**
* @var integer
*/
public $quantity;
/**
* @var string
*/
public $status;
}
```
通過添加 `--get-set` ,您可以生成帶有受保護變量和公共setter/getter方法的字段。這些方法可以幫助setter/getter方法中的業務邏輯實現。
```php
<?php
use Phalcon\Mvc\Model;
class Products extends Model
{
/**
* @var integer
*/
protected $id;
/**
* @var integer
*/
protected $typesId;
/**
* @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 typesId
*
* @param integer $typesId
*/
public function setTypesId($typesId)
{
$this->typesId = $typesId;
}
// ...
/**
* Returns the value of field status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
}
```
模型生成器的一個很好的特性是它保持開發人員在代碼生成之間做出的更改。這允許添加或刪除字段和屬性,而不必擔心丟失對模型本身所做的更改。以下截屏視頻將向您展示它的工作原理:
<div align="center">
<iframe src="https://player.vimeo.com/video/39213020" width="500" height="266" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
## 腳手架CRUD
腳手架是生成應用程序的一些主要部分的快速方法。如果要在單個操作中為新資源創建模型,視圖和控制器,腳手架就是工作的工具。
生成代碼后,必須對其進行自定義以滿足您的需求。許多開發人員完全避免使用腳手架,選擇從頭開始編寫所有或大部分源代碼。生成的代碼可以作為更好地理解框架如何工作或開發原型的指南。下面的代碼顯示了基于表`products`的腳手架:
```bash
$ phalcon scaffold --table-name products
```
腳手架生成器將在您的應用程序中構建多個文件以及一些文件夾。以下是將要生成的內容的快速概述:
| 文件 | 目的 |
| ---------------------------------------- | ------------------------------ |
| `app/controllers/ProductsController.php` | Products 控制器 |
| `app/models/Products.php` | Products 模型 |
| `app/views/layout/products.phtml` | Products 控制器布局 |
| `app/views/products/new.phtml` | `new`動作視圖 |
| `app/views/products/edit.phtml` | `edit`動作視圖 |
| `app/views/products/search.phtml` | `search`動作視圖 |
瀏覽最近生成的控制器時,您將看到一個搜索表單和一個用于創建新產品的鏈接:

`創建頁面`允許您創建在Products模型上應用驗證的產品。如果需要任何字段,Phalcon將自動驗證非空字段產生警告。

執行搜索后,可以使用尋呼機組件顯示分頁結果。使用每個結果前面的“編輯”或“刪除”鏈接來執行此類操作。

## 工具的Web界面
此外,如果您愿意,可以從Web界面使用Phalcon Developer Tools。查看以下截屏視頻,了解其工作原理:
<div align="center">
<iframe src="https://player.vimeo.com/video/42367665" width="500" height="266" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen mark="crwd-mark"></iframe>
</div>
## 將工具與PhpStorm IDE集成
下面的截屏視頻演示了如何將開發人員工具與[PhpStorm IDE](http://www.jetbrains.com/phpstorm/)集成。配置步驟可以很容易地適應PHP的其他IDE。
<div align="center">
<iframe width="560" height="315" src="https://www.youtube.com/embed/UbUx_6Cs6r4" frameborder="0" allowfullscreen mark="crwd-mark"></iframe>
</div>
<a name='conclusion'></a>
## 結論
Phalcon開發人員工具提供了一種為應用程序生成代碼的簡便方法,減少了開發時間和潛在的編碼錯誤。
- 常規
- Welcome
- 貢獻
- 生成回溯
- 測試重現
- 單元測試
- 入門
- 安裝
- Web服務器設置
- WAMP
- XAMPP
- 教程
- 基礎教程
- 教程:創建一個簡單的REST API
- 教程:V?kuró
- 提升性能
- 教程:INVO
- 開發環境
- Phalcon Compose (Docker)
- Nanobox
- Phalcon Box (Vagrant)
- 開發工具
- Phalcon開發者工具的安裝
- Phalcon開發者工具的使用
- 調試應用程序
- 核心
- MVC應用
- 微應用
- 創建命令行(CLI)應用程序
- 依賴注入與服務定位
- MVC架構
- 服務
- 使用緩存提高性能
- 讀取配置
- 上下文轉義
- 類加載器
- 使用命名空間
- 日志
- 隊列
- 數據庫
- 數據庫抽象層
- Phalcon查詢語言(PHQL)
- ODM(對象文檔映射器)
- 使用模型
- 模型行為
- ORM緩存
- 模型事件
- 模型元數據
- 模型關系
- 模型事務
- 驗證模型
- 數據庫遷移
- 分頁
- 前端
- Assets管理
- 閃存消息
- 表單
- 圖像
- 視圖助手(標簽)
- 使用視圖
- Volt:模板引擎
- 業務邏輯
- 訪問控制列表(ACL)
- 注解解析器
- 控制器
- 調度控制器
- 事件管理器
- 過濾與清理
- 路由
- 在session中存儲數據
- 生成URL和路徑
- 驗證
- HTTP
- Cookies管理
- 請求環境
- 返回響應
- 安全
- 加密/解密
- 安全
- 國際化
- 國際化
- 多語言支持