LStarky LStarky - 1 month ago 15
PHP Question

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

Answer

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'] as 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);
    }

}