定制 adelowo/cfar 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

adelowo/cfar

Composer 安装命令:

composer require adelowo/cfar

包简介

Using Controllers with Aura.Router

README 文档

README

Latest Version on Packagist Software License Build Status Scrutinizer Coverage Quality Score SensioLabsInsight Total Downloads

This library was written to enable users of Aura.Router make use of symfony/laravel "type" controllers.

Already using CFAR , Migrate to: 1.2

Installation

You are recommended to make use of the latest available PHP version, but CFAR should run on >=5.5.

CFAR has a dependency, which is Aura.Router. >=1.0 releases require Aura.Router 3.x while <=0.2.1 requires Aura.Router 2.x .

Install CFAR via one of the following methods :

    composer require "adelowo/cfar" : "~1.0"

If you are still using Aura.Router 2.x, please install 0.x

    composer require "adelowo/cfar" : "~0.2"
  • Repo Cloning :
    git clone https://github.com/adelowo/cfar.git

If downloading the library without composer or cloning directly from the repository, you'd have to write an autoloader yourself

Usage

Cfar doesn't require any special config - other than specifying the class that acts as your controller (namespaced) and method to invoke - you'd still write your routes as specified in Aura.Router's (3.x) doc.

Aura.Router 3 is a big improvement to the much loved router with it being broken into many parts such as a Mapper, Matcher, Route (that contains the matched route).

Internally, Cfar uses PHP's Reflection Api.

By default, Cfar would search and invoke a method called indexAction

Below is a little snippet that shows Aura.Router and Cfar fully integrated, an index.php file and controllers for the routes would be written.

<?php

//filename : index.php

use Aura\Router\RouterContainer;

require_once "vendor/autoload.php";

$routeContainer = new RouterContainer();

$routeMapper = $routeContainer->getMap();


$routeMapper->get('blog.read', '/blog/{ide}')
    ->handler('\Http\controller\BlogController@show');

$routeMapper->get(null, "/")
    ->handler('\Http\controller\BlogController');

$routeMapper->get('dev', '/dev');

$routeMapper->get(null,'/error')
    ->handler('\Http\controller\ErrorController'); //`indexAction` would be the invoked method

$routeMatcher = $routeContainer->getMatcher();


$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(
	$_SERVER,
	$_GET,
	$_POST,
	$_COOKIE,
	$_FILES
);

$matched = $routeMatcher->match($request);

if (!$matched) {
	throw new \Aura\Router\Exception("Route does not exists");
}

foreach ($matched->attributes as $key => $val) {
	$request = $request->withAttribute($key, $val);
}


/**
 * This is totally optional. But you could use some "Control Inverting", than have `new` wrap all lines of your code
*`SomeContainer` implement `Interop\Container\ContainerInterface`.;
* A neat way to do this is to extend your choosen container and have the `get` method exposed by the interface retrieve the service from the container.
* @see https://github.com/slimphp/slim/
*/
$container = new SomeContainer(); 

//Add an ORM, Doctrine in this case.
$container['db'] = function ($container) {
    
    $paths = array("/src/Entities");
    
    $isDevMode = false;

    $dbParams = [
        'driver' => 'pdo_mysql',
        'user' => 'root',
        'password' => 'xx-xxx-xx-xx',
        'dbname' => 'foo',
    ];
    
    $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
    
    return \Doctrine\ORM\EntityManager::create($dbParams, $config);    
};

//You def' need a logger
$container['logger'] = function ($container) {

    $logger = new \Monolog\Logger("Your App Name");
    
    $handler = new \Monolog\Handler\SyslogHandler('Owambe');
    $handler->setFormatter(new \Monolog\Formatter\LineFormatter());
    
    $logger->pushHandler($handler);

    return $logger;
};

//register X,Y,Z services 

try {

    $cfar = new \Adelowo\Cfar\Cfar($matched , $container);

    $cfar->dispatch();

} catch (\Adelowo\Cfar\CfarException $exception) {
    echo $exception->getMessage(); 
}

The constructor of the controller would always receive a container. Might be null or a valid one. Your call.

Parameters would be passed to the invoked method in the same order defined in the route.. A method for /users/{id}/{name} should have two parameters, where the first one would be passed the value captured by the router for {id} and vice-versa

<?php

namespace Adelowo\Controller;

class BlogController
{

    protected $container;
    
    public function __construct(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
    public function showUser($id , $param)
    {
        $db = $this->container->get('db');
        
        $data = $db->find("User" , $id);
        
        var_dump($data);
    }

    public function showPdf($name)
    {

            echo $name;
    }

    public function indexAction($id ,$name)
    {
        echo $id. PHP_EOL;
        echo $name;
    }
}

License

MIT

adelowo/cfar 适用场景与选型建议

adelowo/cfar 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 72 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「controller for aura router」 「aura router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 adelowo/cfar 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adelowo/cfar 我们能提供哪些服务?
定制开发 / 二次开发

基于 adelowo/cfar 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 72
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-20