dongzhong7299 2016-10-31 14: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-10-31 21: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部