juliangut/slim-controller 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

juliangut/slim-controller

Composer 安装命令:

composer require juliangut/slim-controller

包简介

Slim Framework controller creator

README 文档

README

PHP version Latest Version License

Build status Style Code Quality Code Coverage Total Downloads

Slim3 controller management

NOTICE: To anyone using this package you should consider moving to juliangut/slim-routing

Class route creation boilerplate, allows you to define your controller classes as services to be pulled out from container easily, extending from a base controller class.

I strongly suggest you don't use this library but for rapid prototyping or certain use cases. If you really want to follow SOLID principles, don't use this package and either directly inject your dependencies in the controller class, use the container to create and inject dependencies or try another container such as PHP-DI with juliangut/slim-php-di

I cannot stress this enough, this library is meant to be used with default Slim container and for certain cases only.

Installation

Best way to install is using Composer:

composer require juliangut/slim-controller

Then require_once the autoload file:

require_once './vendor/autoload.php';

Usage

use \Jgut\Slim\Controller\Resolver;

// Define your controllers
$controllers = [
    'MyController',
];

// Create Slim app and fetch DI Container
$app = new \Slim\App();
$container = $app->getContainer();

// Register Controllers
foreach (Resolver::resolve($controllers) as $controller => $callback) {
    $container[$controller] = $callback;
}

// Define route (\MyController has already been registered)
$app->get('hello/app', '\MyController:dispatch');

// Run app
$app->run();

If your controller implements Jgut\Slim\Controller\Controller it has the DI container automatically injected, you can access it by getContainer method.

If your controller extends Jgut\Slim\Controller\Base you can also directly access container services the same way you do on a Closure route callback. Simply take care to not define class attributes with the same name as services in the container. To do this container is injected in the controller and __get and __isset magic methods are defined to look into the container.

use Jgut\Slim\Controller\Base as BaseController;

class MyController extends BaseController
{
    public function displatch($request, $response, array $args)
    {
        // Pull Twig view service given it was defined
        return $this->view->render($response, 'dispatch.twig');
    }
}

You can use the resolver to define your class routes callback and not implement Controller or extend Base on those classes, in this case your controller won't have access to the container but it will still be a valid callback.

Important notice

As a general rule of thumb directly injecting container is considered a bad practice as you are actually hiding your dependencies, by fetching them from the container, instead of defining them in the class. You'll be using the container as a service locator rather than a true DIC.

Caveat

This controller registration works only for controllers whose constructor doesn't need any parameters. In case you need a controller with paramenters in its __construct() method you can still benefit from \Jgut\Slim\Controller\Controller but you have to register it yourself.

use Jgut\Slim\Controller\Controller;

$container['\MyController'] = function($container) {
    $controller = new \MyController('customParameter');

    // Set container into the controller
    if ($controller instanceof Controller) {
        $controller->setContainer($container);
    }

    return $controller;
}

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before

See file CONTRIBUTING.md

License

Release under BSD-3-Clause License.

See file LICENSE included with the source code for a copy of the license terms

juliangut/slim-controller 适用场景与选型建议

juliangut/slim-controller 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39.5k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2015 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 juliangut/slim-controller 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 39.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 2
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-03-15