2016-10-31 31 views
0

我想就在我的路由器提供以下功能。修身框架添加全局函數

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

回答

1

我會建議利用應用containers,簡化您的應用程序結構。 Slim 3的設計可以很好地與應用程序容器配合使用。

傳遞所述容器到類方法 - 那麼你將有請求和響應通過(共享的)容器對象可用,因爲超薄自動分配那些(請求和響應)的容器對象。

你甚至可以添加/分配數據庫連接(和其他任何你想提供給其他類)的容器,那麼你只需要在同一個容器傳遞給需要數據庫功能的所有功能。

的想法是,你可以寫在其他項目中,可重複使用的類,即使你決定使用的東西比苗條下一次不同。只要框架使用應用程序容器,就可以重新使用你的類。

例如:在你的index.php

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

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

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

} 
1

$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); 
0

沃納的建議的執行情況使用應用程序的容器:

我創建了一個名爲通用類/lib/common.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); 
    } 
} 

然後,我添加了類/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; 
}; 

現在,我個人的路由器文件中,我能夠這樣調用常用功能/路由器/classroom.router。PHP

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

的容器中攜帶$請求,$響應和的$ args(以及其他功能)。