dongzhong7299 2016-10-31 22:57
浏览 22
已采纳

Slim Framework添加全局功能

I would like to make the following function available throughout my routers.

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);
}

However, I get the following error: Fatal error: Using $this when not in object context in C:\Wamp\wwwavine\server\srcoutes.php on line 26

How should I make this function available so that I can call it inside my route, like this:

// 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);
});
  • 写回答

3条回答 默认 最新

  • douyinliu8813 2016-11-01 05:43
    关注

    I would suggest making use of application containers to simplify your application structure. Slim 3 has been designed to work well with application containers.

    Pass the container to your class method - you will then have the request and response objects available via the (shared) container, since Slim assigns those (request and response) to the container object automatically.

    You can even add/assign your database connection (and whatever else you want to make available to other classes) to the container, then you only need to pass the same container to all functions that require database functionality.

    The idea is that you can write classes that can be re-used in other projects, even if you decide to use something different than Slim next time. As long as the framework uses application containers, you can probably re-use your classes.

    Eg: In you index.php

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

    $container['request'] and $container['response'] are assigned automatically by the framework.

    E.g 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);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题