Slim Framework 添加全局函数

Slim Framework add global function

我想在我的路由器中提供以下功能。

public function getAll($request, $response, $args, $table, $prefix, $order, $PermRead) {
  // retrieve all records
  // WORKING... Security questions
  // 1. First, check to make sure authenticated (via JSESSION_ID, etc.)
  // 2. Automatically apply site_id to ALL queries
  // 3. Apply sch_id to this query
  // 4. Get permissions
  $status = null;
  $site_id = $sch_id = 1;
  if (!$PermRead) {
    $status = 403; // 403 Forbidden
  } else {
    $sql =
      "SELECT * from " . $table .
      " WHERE " . $prefix . "_site_id = " . $site_id .
        " AND " . $prefix . "_sch_id = " . $sch_id .
        " AND " . $prefix . "_deleted_timestamp IS NULL " .
        " ORDER BY " . $order;
    $rows = $this->dbw->run($sql);
  }
  if (!$status) {
    $status = 200; // 200 OK
  }
  return $response->withStatus($status)->withJson($rows);
}

但是,我收到以下错误:Fatal error: Using $this when not in object context in C:\Wamp\www\ravine\server\src\routes.php on line 26

我应该如何使这个函数可用,以便我可以在我的路线中调用它,如下所示:

// retrieve all classroom records
$app->get('/classrooms', function ($request, $response, $args) {
  $PermRead = true; // check permissions
  return getAll($request, $response, $args, "classroom", "room", "room_name", $PermRead);
});

我建议使用应用程序 containers 来简化您的应用程序结构。 Slim 3 的设计旨在与应用程序容器配合使用。

将容器传递给您的 class 方法 - 然后您将通过(共享)容器获得请求和响应对象,因为 Slim 自动将这些(请求和响应)分配给容器对象。

您甚至可以 add/assign 将您的数据库连接(以及您想让其他 classes 可用的任何其他内容)连接到容器,然后您只需将同一个容器传递给所有函数需要数据库功能。

想法是您可以编写可以在其他项目中重复使用的 classes,即使您下次决定使用不同于 Slim 的东西。只要框架使用应用程序容器,您就可以重新使用您的 classes.

例如:在你身上index.php

$container = $app->getContainer(); 
$container['db'] = $myDbConnection;

$container['request']$container['response'] 由框架自动分配。

例如MyClass.php

use Interop\Container\ContainerInterface;

class MyClass {

    public function getAll(ContainerInterface $container) {
        // ...
        $myDb = $container['db'];
        // ... do DB stuff
        $response = $container['response'];
        return $response->withStatus($status)->withJson($rows);
    }

}

$this 在您的函数中不可用,最简单的方法是将其添加为参数。

类似于:

public function getAll($request, $response, $args, $table, $prefix, $order, $PermRead, $app) {
    [..]
    $app->dbw->...;

然后在参数$this中调用

return getAll($request, $response, $args, "classroom", "room", "room_name", $PermRead, $this);

实施 Werner 使用应用程序容器的建议:

我在 /lib/common 中创建了一个名为 Common 的 class。php:

<?php

namespace lib;
use Interop\Container\ContainerInterface;

class Common {
  protected $ci;
  private $site_id = 1;

  //Constructor
  public function __construct(ContainerInterface $ci) {
    $this->ci = $ci;
  }

  public function getAll($table, $prefix, $order, $PermRead) {
    // retrieve all records
    // WORKING... Security questions
    // 1. First, check to make sure authenticated (via JSESSION_ID, etc.)
    // 2. Automatically apply site_id to ALL queries
    // 3. Apply sch_id to this query
    // 4. Get permissions
    $status = null;
    $site_id = $sch_id = 1;
    if (!$PermRead) {
      $status = 403; // 403 Forbidden
    } else {
      $sql =
        "SELECT * from " . $table .
        " WHERE " . $prefix . "_site_id = " . $site_id .
          " ORDER BY " . $order;
      $rows = $this->ci->dbh->run($sql);
      $this->ci->response->withJson($rows);
    }
    if (!$status) {
      $status = 200; // 200 OK
    }
    return $this->ci->response->withStatus($status);
  }
}

然后,我将 class 添加到 /src/dependencies。php

<?php
require __DIR__ . '/../lib/common.php';

$container = $app->getContainer();

// common router functions
$container['common'] = function ($c) {
  $common = new lib\Common($c);
  return $common;
};

现在,在我的个人路由器文件中,我可以在 /routers/classroom.router.php:[=13 中调用通用函数=]

// retrieve all classroom records
$app->get('/classrooms', function ($request, $response, $args) {
  $PermRead = true; // check permissions
  return $this->common->getAll("classroom", "room", "room_name", $PermRead);
});

容器携带$request、$response和$args(以及其他函数)。