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 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问