在Slim Framework 3中访问类中的应用 [英] Access app in class in Slim Framework 3

查看:87
本文介绍了在Slim Framework 3中访问类中的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当路由位于与index.php不同的类中时,我无法理解如何访问Slim实例

Im having trouble understanding how to access the instance of Slim when a route is in a seperate class than index.php

在使用Slim Framework 2时,我总是使用以下内容,但在Slim 3中不起作用:

When using Slim Framework 2 I always used the following, but its not working in Slim 3:

$this->app = \Slim\Slim::getInstance();

我正在尝试访问数据库连接,我已经在容器中进行了设置,但是是从单独的类中进行的.这是我目前在index.php中启动的Slim应用程序:

Im trying to access a database connection I have setup in the container, but from a separate class. This is what I currently got in my index.php to initiate a Slim app:

require_once("rdb/rdb.php");
$conn = r\connect('localhost');
$container = new \Slim\Container;
$container['rdb'] = function ($c){return $conn;}
$app = new \Slim\App($container);

这是我的路线:

$app->get('/test','\mycontroller:test');

这是我在mycontroller.php类中得到的,我的路线指向该类,由于$ this-> app不存在,这显然不起作用:

And this is what I got in my mycontroller.php class which my route points to, which obviously is not working as $this->app doesn't exist:

class mycontroller{
public function test($request,$response){
$this->app->getContainer()->get('rdb');
}

由于getinstance与Slim 2相比不是Slim 3的一部分,因此错误消息如下:

The error message is the following, due to getinstance not being part of Slim 3 compared to Slim 2:

Call to undefined method Slim\App::getInstance() 

感谢您的帮助,

问候 丹

推荐答案

看看 Slim 3 Skeleton 由罗伯·艾伦(Rob Allen)创建.

Have a look at the Slim 3 Skeleton created by Rob Allen.

Slim 3大量使用依赖项注入,因此您可能也想使用它.

Slim 3 heavily uses dependency injection, so you might want to use it too.

在您的dependencies.php中添加以下内容:

In your dependencies.php add something like:

$container = $app->getContainer();

$container['rdb'] = function ($c) {
    return $conn;
};

$container['Your\Custom\Class'] = function ($c) {
    return new \Your\Custom\Class($c['rdb']);
};

在您的Your\Custom\Class.php中:

class Class {
    private $rdb;
    function __construct($rdb) {
        $this->rdb = $rdb;
    }

    public function test($request, $response, $args) {
        $this->rdb->doSomething();
    }
}

如果您还有其他问题,希望对您有所帮助.

I hope this helps, if you have any more questions feel free to ask.

更新:

当您这样定义路线

$app->get('/test', '\mycontroller:test');

Slim在容器中查找\mycontroller:test:

Slim looks up \mycontroller:test in your container:

$container['\mycontroller'] = function($c) {
    return new \mycontroller($c['rdb']);
}

因此,当您在浏览器中打开www.example.com/test时,Slim自动创建\mycontroller的新实例,并使用参数$request$response$args执行方法test. 并且因为您接受数据库连接作为mycontroller类的构造函数的参数,所以也可以在方法中使用它:)

So when you open www.example.com/test in your browser, Slim automatically creates a new instance of \mycontroller and executes the method test with the arguments $request, $response and $args. And because you accept the database connection as an argument for the constructor of your mycontroller class, you can use it in the method as well :)

这篇关于在Slim Framework 3中访问类中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆