LStarky
LStarky

Reputation: 2777

Slim Framework add global function

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\www\ravine\server\src\routes.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);
});

Upvotes: 0

Views: 4107

Answers (3)

Werner
Werner

Reputation: 3663

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

}

Upvotes: 1

LStarky
LStarky

Reputation: 2777

Implementation of Werner's suggestion to use the application's container:

I created a class called Common in /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);
  }
}

Then, I added the class to /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;
};

Now, within my individual router files, I'm able to call the common function like this in /routers/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);
});

The container carries $request, $response and $args (and other functions).

Upvotes: 3

jmattheis
jmattheis

Reputation: 11125

$this is not available in your function, the easiest way would be to just add it as an parameter.

Something like:

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

Then call it with $this in the parameter

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

Upvotes: 1

Related Questions