dpvv37755 2016-04-10 17:01
浏览 93
已采纳

PHPStorm和Slim 3.X DIC

I recently moved from Slim 2.X to Slim 3.X and I've found a problem that may be a little stupid but annoys me in some ways.
The newly Slim 3.X, as a difference between the older 2.X version, it implements a new container system using Dependency Injection Containers (DIC) build on Pimple.

As I've been reading, I find this a very great enhancement because it lets you manage your PHP app in a lot of different ways.
When I started playing around with the new things it has, I found something confusing that maybe is something I'm missing.

I use PHPStorm; one of the things that I like about this IDE is its code-completion and the facility for writing code and understanding classes (I'm actually a student, so this helps me a lot).
When I write a Slim route, if I want to access, for example, to my view object stored inside the container, I have to refer to it with the $this->view variable. The thing is that, normally, PHPStorm lists me the methods and properties inside an object when I mention it, but that didn't happen with the $this object.

I suppose that, inside a route, Slim stores all route functionalities and all the container objects into the $this assigner.

$container = $app->getContainer();
$container['view'] = new \Slim\Views\PhpRenderer('protected/views/');

$app->get('/products', function(Request $request, Response $response) {
    $response = $this->view->render($response, 'products.php');
    return $response;
})->setName('products');

When I access my /products route, it successfully renders my products template and shows what it's expected to show, so no problems with that.
The matter with it is that I want PHPStorm to know that $this variable inside a route it stores all the containers that are set previously before the route is called.

I thought about /* @var */ and /* @global */ or something like this but after searching for a while and trying different things, I haven't found anything that could work.

In conclusion, what I'm trying to say is if it's possible that PHPStorm could list me the properties and methods of the container objects like this: PHPStorm auto-completion feature for objects

but with the $this object inside a route: Missing auto-completion feature in this object

Thanks!

  • 写回答

1条回答 默认 最新

  • dongwei1263 2016-04-10 18:33
    关注

    The easiest way to do this is to have separate Action classes rather than use closures. This also has the benefit of being easier to test.

    Firstly create your action, inject its dependencies into its constructor and write an `__invoke`` method that will be called by Slim:

    class ProductsListAction {
        protected $view;
    
        public function __construct(\Slim\Views\PhpRenderer $view) {
            $this->view = $view;
        }
    
        public function __invoke($request, $response, $args) {
            $response = $this->view->render($response, 'products.php');
            return $response;
        }
    }
    

    For this to work, you now need a DIC factory for it:

    $container['ProductsListAction'] = function ($c) {
        return new ProductsListAction($c['view']);
    };
    

    You can now register your new action as a route callable:

    $app->get('/products', 'ProductListAction');
    

    Now, PhpStorm will correctly autocomplete within your ProductsListAction class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab