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 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到