2016-10-31 9 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を使用してアプリケーション構造を簡略化します。スリム3は、アプリケーション容器でうまく機能するように設計されています。

クラスメソッドにコンテナを渡します。要求と応答オブジェクトをコンテナオブジェクトに自動的に割り当てるため、(共有)コンテナを介して要求オブジェクトと応答オブジェクトを利用できます。

データベース接続(他のクラスで利用可能にしたいもの)をコンテナに追加/割り当てすることもできます。データベースの機能を必要とするすべての関数に同じコンテナを渡すだけで済みます。

次回はSlim以外のものを使用することに決めたとしても、他のプロジェクトで再利用できるクラスを書くことができます。フレームワークがアプリケーションコンテナを使用している限り、おそらくクラスを再利用できます。

例:

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

$container['request']$container['response'] index.phpのあなたには、フレームワークによって自動的に割り当てられます。

例えば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->...; 

は、その後、アプリケーションのコンテナを使用するためにウェルナー提案のパラメータ

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

実装で$thisとそれを呼び出します/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); 
}); 

容器は$要求、$応答と$引数(および他の機能)を運びます。