Sections

Phalcon开发人员工具

这些工具是生成骨架代码的有用脚本的集合。 应用程序的核心组件可以通过简单的命令生成, 使您可以轻松地使用 Phalcon开发应用程序。

If you prefer to use the web version instead of the console, this blog post offers more information.

下载

你可以从Github下载或克隆一个跨平台的开发者工具包。

安装

以下是有关如何在不同平台上安装开发人员工具的详细说明:

Linux : MacOS : Windows

可用的命令

通过输入 typing::code: Phalcon commands,您可以在Phalcon工具中获得一组可用的命令

$ 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工具为您的应用程序生成预定义的项目框架。 默认情况下,项目框架生成器将生成Apache使用的mod_rewrite。 在web服务器文档根上键入以下命令:

$ pwd

/Applications/MAMP/htdocs

$ phalcon create-project store

产生以上推荐的项目结构:

您可以添加参数--help来获得关于某个脚本使用的帮助:

$ 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服务器访问项目将显示:

生成控制器

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

$ phalcon create-controller --name test

以下代码由脚本生成:

<?php

use Phalcon\Mvc\Controller;

class TestController extends Controller
{
    public function indexAction()
    {

    }
}

准备数据库设置

使用开发人员工具生成项目时。 A configuration file can be found in app/config/config.php. 要生成模型或脚手架,需要更改连接到数据库的设置。

Change the database section in your config.php file:

<?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"]),
    ]
]);

Generating Models

创建模型有几种方法。 您可以从默认数据库连接或有选择地创建所有模型。 Models can have public attributes for the field representations or setters/getters can be used.

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]

生成模型的最简单方法是:

$ phalcon model products
$ phalcon model --name tablename

所有表字段都声明为公共以便直接访问。

<?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

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;
    }
}

模型生成器的一个很好的特性是,它可以在代码生成之间保持开发人员所做的更改。 这允许添加或删除字段和属性,而不必担心会丢失对模型本身所做的更改。 下面的视频展示了它的工作原理:

CURD 脚手架

脚手架是生成应用程序主要部分的一种快速方法。 如果您希望在单个操作中为新资源创建模型、视图和控制器,那么脚手架就是这项工作的工具。

一旦生成了代码,就必须对其进行定制,以满足您的需求。 许多开发人员完全避免搭建代码,而是选择从头开始编写全部或大部分源代码。 生成的代码可以作为指导,更好地理解框架如何工作或开发原型。 下面的代码显示了基于表products的脚手架:

$ phalcon scaffold --table-name products

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

File 目的
app/controllers/ProductsController.php 产品控制器
app/models/Products.php Products 模型
app/views/layout/products.phtml 产品的控制器布局
app/views/products/new.phtml new操作的视图
app/views/products/edit.phtml edit 操作的视图
app/views/products/search.phtml search 操作的视图

浏览最近生成的控制器时, 您将看到一个搜索窗体和一个用于创建新产品的链接:

“0>create 页 </0 > 允许您在” 产品 “模型上创建应用验证的产品。 如果需要其中任何一个, phalcon 将自动验证非空字段并生成警告。

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.

工具的 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 ide 集成

The screencast below shows how to integrate developer tools with the PhpStorm IDE. 配置步骤可以很容易地适应 php 的其他 ide。

结语

Phalcon开发人员工具提供了一种为应用程序生成代码的简单方法, 从而减少了开发时间和潜在的编码错误。