Phalcon 开发工具Phalcon Developer Tools

halcon提供的这个开发工具主要是用来辅助开发,比如生成一些程序的基本框架,生成控制器模型等。使用这个工具我们只需要一个简单的命令即可生成应用的基本框架。

These tools are a collection of useful scripts to generate skeleton code. Core components of your application can be generated with a simple command, allowing you to easily develop applications using Phalcon.

Important: Phalcon Framework version 0.5.0 or greater is needed to use developer tools. It is highly recommended to use PHP 5.4 or greater. If you prefer to use the web version instead of the console, this blog post offers more information.

下载Download

我们可以从 Github 上下载或克隆下来这个跨平台的开发辅助工具。

You can download or clone a cross platform package containing the developer tools from Github.

安装Installation

下面详尽的说明了如何在不同的操作系统平台上安装这个辅助开发工具:

These are detailed instructions on how to install the developer tools on different platforms:

获取可用的命令Getting Available Commands

我们可以在虚拟控制台上输入如下命令:phalcon commands

You can get a list of available commands in Phalcon tools by typing: phalcon commands

  1. $ phalcon commands
  2. Phalcon DevTools (1.2.3)
  3. Available commands:
  4. commands (alias of: list, enumerate)
  5. controller (alias of: create-controller)
  6. model (alias of: create-model)
  7. all-models (alias of: create-all-models)
  8. project (alias of: create-project)
  9. scaffold
  10. migration
  11. webtools

生成项目框架Generating a Project Skeleton

我们可以使用Phalcon开发辅助工具生成预先定义的项目架构。 默认情况下,phalcon开发辅助工具会使用apache的mod_rewrite来生成程序的骨架. 要创建项目我们只需要在我们的 web服务器根目录下输入如下命令:

You can use Phalcon tools to generate pre-defined project skeletons for your applications with Phalcon framework. By default the project skeleton generator will use mod_rewrite for Apache. Type the following command on your web server document root:

  1. $ pwd
  2. /Applications/MAMP/htdocs
  3. $ phalcon create-project store

执行命令后会生成如下的文档结构的项目:

The above recommended project structure was generated:

../_images/tools-2.png

我们可以在命令上加 –help 以显示帮助信息(下面的帮助中的中文是翻译时加上去的):

You could add the parameter –help to get help on the usage of a certain script:

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

Options:

—nameName of the new project
—enable-webtools
 Determines if webtools should be enabled [optional]
—directory=sBase path on which project will be created [optional]
—type=sType of the application to be generated (micro, simple, modules)
—template-path
 Specify a template path [optional]
—use-config-ini
 Use a ini file as configuration file [optional]
—traceShows the trace of the framework in case of exception. [optional]
—helpShows this help

我们访问新生成项目的地址显示如下:

Accessing the project from the web server will show you:

../_images/tools-6.png

生成控制器Generating Controllers

我们可以使用phalcon create-controller –name test或phalcon controller –name test来生成名为test的控制器. 当然要使用此命令当前的执行命令目录必须为已存在的phalcon项目内.

The command “create-controller” generates controller skeleton structures. It’s important to invoke this command inside a directory that already has a Phalcon project.

  1. $ phalcon create-controller --name test

上面的命令会生成如下代码:

The following code is generated by the script:

  1. <?php
  2. class TestController extends Phalcon\Mvc\Controller
  3. {
  4. public function indexAction()
  5. {
  6. }
  7. }

数据库配置Preparing Database Settings

当我们使用phalcon的辅助开发工具生成项目时,则生成的配置信息会被放在 app/config/config.ini 文件内。 我们必须要正确的配置连接信息才可生成模型或基本的CRUD操作。 可以在config.ini中进行修改配置信息:

When a project is generated using developer tools. A configuration file can be found in app/config/config.ini To generate models or scaffold, you will need to change the settings used to connect to your database.

Change the database section in your config.ini file:

  1. [database]
  2. adapter = Mysql
  3. host = "127.0.0.1"
  4. username = "root"
  5. password = "secret"
  6. dbname = "store_db"
  7. [phalcon]
  8. controllersDir = "../app/controllers/"
  9. modelsDir = "../app/models/"
  10. viewsDir = "../app/views/"
  11. baseUri = "/store/"

生成模型Generating Models

使用phalcon开发辅助工具我们可以有若干种方式来生成模型。 我人可以有选择的生成若干个模型或是全部生成。 亦可以指定生成公有属性或是生成setter和getter方法。

There are several ways to create models. You can create all models from the default database connection or some selectively. Models can have public attributes for the field representations or setters/getters can be used.

Options:

—name=sTable name
—schema=sName of the schema. [optional]
—namespace=sModel’s namespace [optional]
—get-setAttributes will be protected and have setters/getters. [optional]
—extends=sModel extends the class name supplied [optional]
—docHelps to improve code completion on IDEs [optional]
—directory=sBase path on which project will be created [optional]
—forceRewrite the model. [optional]
—traceShows the trace of the framework in case of exception. [optional]
—mapcolumnGet some code for map columns. [optional]

最简单的生成模型的方式:

The simplest way to generate a model is:

  1. $ phalcon model products
  1. $ phalcon model --name tablename

所有的字段设置为公有:

All table fields are declared public for direct access.

  1. <?php
  2. class Products extends \Phalcon\Mvc\Model
  3. {
  4. /**
  5. * @var integer
  6. */
  7. public $id;
  8. /**
  9. * @var integer
  10. */
  11. public $types_id;
  12. /**
  13. * @var string
  14. */
  15. public $name;
  16. /**
  17. * @var string
  18. */
  19. public $price;
  20. /**
  21. * @var integer
  22. */
  23. public $quantity;
  24. /**
  25. * @var string
  26. */
  27. public $status;
  28. }

我们可以在生成模型时指定 –get-set 参数以实现对字面的保护, 这样我们可以在setter/getter方法里执行一些业务逻辑。

By adding the –get-set you can generate the fields with protected variables and public setter/getter methods. Those methods can help in business logic implementation within the setter/getter methods.

  1. <?php
  2. class Products extends \Phalcon\Mvc\Model
  3. {
  4. /**
  5. * @var integer
  6. */
  7. protected $id;
  8. /**
  9. * @var integer
  10. */
  11. protected $types_id;
  12. /**
  13. * @var string
  14. */
  15. protected $name;
  16. /**
  17. * @var string
  18. */
  19. protected $price;
  20. /**
  21. * @var integer
  22. */
  23. protected $quantity;
  24. /**
  25. * @var string
  26. */
  27. protected $status;
  28. /**
  29. * Method to set the value of field id
  30. * @param integer $id
  31. */
  32. public function setId($id)
  33. {
  34. $this->id = $id;
  35. }
  36. /**
  37. * Method to set the value of field types_id
  38. * @param integer $types_id
  39. */
  40. public function setTypesId($types_id)
  41. {
  42. $this->types_id = $types_id;
  43. }
  44. ...
  45. /**
  46. * Returns the value of field status
  47. * @return string
  48. */
  49. public function getStatus()
  50. {
  51. return $this->status;
  52. }
  53. }

另一个非常好的特性即是在我们多次生成模型时,原有的对模型的更改依然会存在。 这样我们就可以不用担心对模型的属性进行修会被后来再次执模型的生成命令所覆盖。下面的截图显示了这是如何工作的:

A nice feature of the model generator is that it keeps changes made by the developer between code generations. This allows the addition or removal of fields and properties, without worrying about losing changes made to the model itself. The following screencast shows you how it works:

生成基本的 CRUDS caffold a CRUD

使用phalcon开发辅助工具我们可以直接快速的生成一个模型的CRUD操作。 如果我们想快速的生成模型的CRUD操作只需要使用phalcon辅助开发工具的中scaffold命令即可。

Scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.

代码生成后,你可以根据自己的需要修改生成的代码。很多开发者可能不会去使用这个功能,其实这东西有时不是太好用,很多时候开发者往往会手动的书写相关代码。使用scaffold产生的代码可以 帮助我们理解框架是如何工作的当然也可以帮助我们制作出快速原型来。 下面的截图展示了基于products表的scaffold:

Once the code is generated, it will have to be customized to meet your needs. Many developers avoid scaffolding entirely, opting to write all or most of their source code from scratch. The generated code can serve as a guide to better understand of how the framework works or develop prototypes. The screenshot below shows a scaffold based on the table “products”:

  1. $ phalcon scaffold --table-name test

scaffold生成器会在相关的文件夹中生成若干个文档。 下面是所生成文件的概览:

The scaffold generator will build several files in your application, along with some folders. Here’s a quick overview of what will be generated:

FilePurpose
app/controllers/ProductsController.phpThe Products controller
app/models/Products.phpThe Products model
app/views/layout/products.phtmlController layout for Products
app/views/products/new.phtmlView for the action “new”
app/views/products/edit.phtmlView for the action “edit”
app/views/products/search.phtmlView for the action “search”

在生成的Products控制器中,我们可以看到一个搜索表单和一个生成新product的链接:

When browsing the recently generated controller, you will see a search form and a link to create a new Product:

../_images/tools-10.png

在创建页面我们可以生成经过验证的Products记录。 Phalcon会自动的验证数据库中的非空字段。

The “create page” allows you to create products applying validations on the Products model. Phalcon will automatically validate not null fields producing warnings if any of them is required.

../_images/tools-11.png

执行搜索后,分页组件会显示颁后的结果。 我们在结果列表的前面放置Edit或Delete链接,以实现相应的操作。

After performing a search, a pager component is available to show paged results. Use the “Edit” or “Delete” links in front of each result to perform such actions.

../_images/tools-12.png

工具的 Web 界面Web Interface to Tools

另外,如果你喜欢我们还可以在生成项目时通过添加参数以实现在项目中使用Phalcon开发工具的web接口。 下面的视频中展示了如何工作的:

Also, if you prefer, it’s possible to use Phalcon Developer Tools from a web interface. Check out the following screencast to figure out how it works:

集成工具到 PhpStorm Integrating Tools with PhpStorm IDE

下面的视频中展示了如何在 PhpStorm IDE 中集成辅助开发工具。 这个配置步骤也适用于其它的PHP IDE.

The screencast below shows how to integrate developer tools with the PhpStorm IDE. The configuration steps could be easily adapted to other IDEs for PHP.

结束语Conclusion

Phalcon开发辅助工具为我们提供了一种简易的产生应用代码的方法, 这可以减少开发时间及潜在的错误。

Phalcon Developer Tools provides an easy way to generate code for your application, reducing development time and potential coding errors.