mamuz/phalcon-application

Phalcon Application 提供简单且可定制的应用程序引导

v2.4.0 2018-10-27 20:36 UTC

README

Author Build Status Latest Stable Version Total Downloads License

Phalcon Application 基于 Phalcon3 框架构建,提供简单且可定制的应用程序引导。

要求

  • 需要 Phalcon3,请按照以下链接中的安装步骤进行安装:https://github.com/phalcon/cphalcon

安装

使用以下命令安装最新版本

$ composer require mamuz/phalcon-application

特性

  • 使用 Composer 自动加载
  • 简单的 MVC 配置
  • 服务注册
  • 支持 XHR 的视图渲染器

用法

引导不带视图支持的应用程序,例如 REST 应用程序

$config = [
    'dispatcher' => [
        // define class namespace identifier of your controllers
        'controllerDefaultNamespace' => 'Rest\Controller',
    ],
    'routes' => [
        // see Router::add in https://docs.phalconphp.com/en/latest/reference/routing.html
        'default' => [
            'pattern'     => '/:controller/:action',
            'paths'       => ['controller' => 1, 'action' => 2], // Optional
            'httpMethods' => ['GET'], // Optional
            'position'    => 1, // Optional
        ],
    ],
    // register custom service factories implementing the InjectableInterface
    // see: https://github.com/mamuz/phalcon-application/blob/master/src/Application/Service/InjectableInterface.php
    // Key is the name to refer to Phalcon's DI, value is the FQCN of the service factory
    'services' => [
        'user'   => 'User\Service\Factory',
        'logger' => 'Logger\Service\Factory',
    ],
];

// make everything relative to the application root
chdir(dirname(__DIR__));

// Composer Autoloader (see: https://getcomposer.org.cn/doc/01-basic-usage.md#autoloading)
include './vendor/autoload.php';

// bootstrap and run your application
Phapp\Application\Bootstrap::init($config)->runApplicationOn($_SERVER);

有关更多详细信息,请参阅基于以下功能测试的示例项目:示例项目

引导带视图支持的应用程序(通常用于响应渲染的 HTML)

有关在 Phalcon 中使用视图的详细信息,请参阅https://docs.phalconphp.com/en/latest/reference/views.html

Phalcon 的视图引擎支持三步视图模板模式。这意味着您可以有一个主布局(外部框架),它包含基于控制器的布局(框架),而框架又包含基于操作的布局(内部框架)。

例如:

<outerframe>
    I am the main layout.
    <frame>
        I am the controller based layout.
        <innerframe>
            I am the action based layout.
        </innerframe>
    </frame>
</outerframe>

因此,每个控制器操作都可以有自己的模板进行渲染。

例如,您有一个具有两个操作的控制器:

  • User::loginAction
  • User::logoutAction

这将导致位于以下位置的两种视图模板:

  • {viewbasepath}\user\login.phtml
  • {viewbasepath}\user\logout.phtml

关于三步视图模板模式,您可以将这些模板放在以下位置:

  • {viewbasepath}\index.phtml(外部框架必须命名为 index,并且需要放置在根级别)
  • {viewbasepath}\layouts\user.phtml(框架必须与控制器同名,并且需要放置在 layouts 文件夹中)

对于 AJAX 请求(XHR),禁用了外部框架和框架的渲染,这意味着仅渲染内部框架。

$config = [
    'dispatcher' => [
        // define class namespace identifier of your controllers
        'controllerDefaultNamespace' => 'Mvc\Controller',
    ],
    'routes' => [
        // see Router::add in https://docs.phalconphp.com/en/latest/reference/routing.html
        'default' => [
            'pattern'     => '/:controller/:action',
            'paths'       => ['controller' => 1, 'action' => 2], // Optional
            'httpMethods' => ['GET'], // Optional
            'position'    => 1, // Optional
        ],
    ],
    // register custom service factories implementing the InjectableInterface
    // see: https://github.com/mamuz/phalcon-application/blob/master/src/Application/Service/InjectableInterface.php
    // Key is the name to refer to Phalcon's DI, value is the FQCN of the service factory
    'services' => [
        'user'   => 'User\Service\Factory',
        'logger' => 'Logger\Service\Factory',
    ],
    // declare the basepath for the view templates, which enables Phalcon's view engine
    'view' => [
        'templatePath' => './view',
    ],
];

// make everything relative to the application root
chdir(dirname(__DIR__));

// Composer Autoloader (see: https://getcomposer.org.cn/doc/01-basic-usage.md#autoloading)
include './vendor/autoload.php';

// bootstrap and run your application
Phapp\Application\Bootstrap::init($config)->runApplicationOn($_SERVER);

有关更多详细信息,请参阅基于以下功能测试的示例项目:示例项目

作为命令行工具引导应用程序

有关创建任务的详细信息,请参阅https://docs.phalconphp.com/en/latest/reference/cli.html#tasks

$config = [
    'dispatcher' => [
        // define class namespace identifier of your tasks
        'taskDefaultNamespace' => 'Command\Task',
    ],
    // register custom service factories implementing the InjectableInterface
    // see: https://github.com/mamuz/phalcon-application/blob/master/src/Application/Service/InjectableInterface.php
    // Key is the name to refer to Phalcon's DI, value is the FQCN of the related service factory
    'services' => [
        'user'   => 'User\Service\Factory',
        'logger' => 'Logger\Service\Factory',
    ],
];

// make everything relative to the application root
chdir(dirname(__DIR__));

// Composer Autoloader (see: https://getcomposer.org.cn/doc/01-basic-usage.md#autoloading)
include './vendor/autoload.php';

// bootstrap and run your application
Phapp\Application\Bootstrap::init($config)->runApplicationOn($_SERVER);

运行带有参数的命令

假设应用程序在 index.php 内引导

$ php index.php mailing send reminder

这将调用 mailing 任务的 send 操作,并使用 reminder 作为参数进行调用。

运行带有参数和选项的命令

假设应用程序在 index.php 内引导

$ php index.php mailing send reminder --sender=foo@bar.com --bcc=baz@bar.com

这将调用 mailing 任务的 send 操作,并使用 reminder 作为参数进行调用。在 send 操作中,选项通过 $this->dispatcher->getOptions() 识别。

更多详细信息,请查看基于以下示例项目 https://github.com/mamuz/phalcon-application/blob/master/tests/functional/CommandLineCest.php 的功能测试。

应用骨架

为了有一个良好的起点,您应该检查基于本项目构建的 骨架