aferalabs/phalcon-expressive

此包的最新版本(dev-master)没有可用的许可证信息。

dev-master 2015-12-28 11:09 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:06 UTC


README

Build Status

该项目旨在在 Zend Expressive 和 Phalcon 框架之间搭建一座桥梁。它部分经过测试,但仍可能包含许多错误,因此请不要将其视为生产就绪。

安装

此步骤是可选的,但建议用于启动您的项目。通过运行以下命令使用 Zend Expressive 框架来创建建议的项目结构。

$ composer create-project zendframework/zend-expressive-skeleton <project-path>

当被要求选择路由器时,输入

aferalabs/phalcon-expressive:dev-master

当被要求选择 DI 容器时,输入

aferalabs/phalcon-expressive:dev-master

Phalcon 路由器

创建路由器简化为以下几行

use PhalconExpressive\PhalconRouter;

$router = new PhalconRouter;

PhalconExpressive\PhalconRouter 依赖于 Phalcon\Mvc\RouterPhalcon\Mvc\Url。如果您想提供这些服务的替代实例,可以通过构造函数参数传递。否则,它们将使用默认值创建。

use Phalcon\Mvc;
use PhalconExpressive\PhalconRouter;
use Zend\Expressive\AppFactory;

$url = new Mvc\Url;
$url->setBaseUri('/blog');

$router = new Mvc\Router;
$router->setEventsManager(new Phalcon\Events\Manager);

$router = new PhalconRouter(null, $url);
$app = AppFactory::create(null, $router);

将 Phalcon 路由器集成到最简单的方式是,在路由配置中定义可调用的依赖项

return [
    'dependencies' => [
        'invokables' => [
            Zend\Expressive\Router\RouterInterface::class => PhalconExpressive\PhalconRouter::class,
        ],
    ],
    'routes' => [
        [
            'name' => 'home',
            'path' => '/',
            'middleware' => App\Action\HomePageAction::class,
            'allowed_methods' => ['GET'],
        ],
        [
            'name' => 'api.ping',
            'path' => '/api/ping',
            'middleware' => App\Action\PingAction::class,
            'allowed_methods' => ['GET'],
        ],
    ],
];

Phalcon DI

Phalcon Expressive 包含了一个基于 Phalcon\DI 构建的 Interop\Container\ContainerInterface 实现。为了设置容器,创建一个新的文件 config/container.php 并添加以下内容到其中

<?php

use PhalconExpressive\PhalconDI;

// Load configuration
$config = require 'config.php';

$di = new PhalconDI;
$di->set('config', $config);

// Inject factories
foreach ($config['dependencies']['factories'] as $name => $object) {
    $di->set($name, function() use ($object, $di) {
        return (new $object)->__invoke($di);
    });
}

// Inject invokables
foreach ($config['dependencies']['invokables'] as $name => $object) {
    $di->set($name, $object);
}

return $di;

就是这样,Phalcon\DI 应该已经设置好,并准备好作为 Zend Expressive 的 DI 容器使用。

待办事项

  • 扩展路由器功能
  • 添加 volt 集成