Phalcon 开发工具(Phalcon Developer Tools)

Phalcon提供的这个开发工具主要是用来辅助开发,比方生成一些程序的基本框架。生成控制器模型等。

使用这个工具我们仅仅须要一个简单的命令就可以生成应用的基本框架。

很重要: 要使用这个工具我们必需要安装Phalcon 0.5版本号以上的扩展才行。

这里我们推荐使用PHP5.3.6或更高版本号的PHP. 假设你喜欢使用web版而非console版本号的程序,那么在这里 blog post 能够看到很多其它的内容。

下载(Download)

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

获取可用的命令(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.

结束语(Conclusion)

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


注:以上因为国内的网络问题 可能有部分信息无法正常显示

posted @ 2017-05-20 20:01  cxchanpin  阅读(3683)  评论(0编辑  收藏  举报