doubo1883 2016-08-29 17:36
浏览 127

如何在slim framework 3(mvc模式)中构建路由控制器

I am working on a Slim Framework based API. Following the mvc pattern, I want my routes to be controller driven with logger and renderer injected to every controller.

As a start point I checked out a number of example mvc slim skeletons, and decided to base my structor on one particular tutorial and sample project ( http://jgrundner.com/slim-oo-004-controller-classes/ )

In this setup, injection is done by adding the router controllers to the app container like this:

$container = $app->getContainer();

$container['\App\Controllers\DefaultController'] = function($c){
    return new \App\Controllers\DefaultController(
        $c->get('logger'),
        $c->get('renderer')
    );
};

This then allow for a nice clean route and controller:

route e.g.:

$app->get('/[{name}]', '\App\Controllers\DefaultController:index');

controller e.g.:

namespace App\Controllers;

use Psr\Log\LoggerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

    class DefaultController{
        private $logger;
        private $renderer;

        public function __construct(LoggerInterface $logger, $renderer){
            $this->logger   = $logger;
            $this->renderer = $renderer;
        }

        public function index(RequestInterface $request, ResponseInterface $response, $args){
            // Log message
            $this->logger->info("Slim-Skeleton '/' route");

            // Render index view
            return $this->renderer->render($response, 'index.phtml', $args);
        }

        public function throwException(RequestInterface $request, ResponseInterface $response, array $args){
            $this->logger->info("Slim-Skeleton '/throw' route");
            throw new \Exception('testing errors 1.2.3..');
        }
    }

Extending the default controller keeps the controllers neat, but requires adding every new controller object to the app container first, which seems inefficient and messy when you have a lot of classes.

Is there a better way?

  • 写回答

1条回答 默认 最新

  • dpde7365 2016-08-31 02:42
    关注

    You can make basecontroller that contain the acontainer.

    <?php
    
    namespace App\Controller;
    
    use Slim\Container;
    
    class Controller
    {
        var $container;
    
        public function __construct(Container $container)
        {
            $this->container = $container;
        }
    
        public function __get($var)
        {
            return $this->container->{$var};
        }
    }
    

    and the container:

    <?php
    
    $container['App\Controller\Controller'] = function ($c) {
        return new App\Controller\Controller($c);
    };
    

    And the controller

    <?php
    
    namespace App\Controllers;
    
    use Psr\Http\Message\RequestInterface;
    use Psr\Http\Message\ResponseInterface;
    use App\Controller\Controller;
    
    class DefaultController extends Controller{
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。