adamlmiller
adamlmiller

Reputation: 532

Slim 3 Controller Issue: Using $this when not in object context

Having an issue using $this->container inside of my controllers.

When I try to access the logger/monolog in the code below, it fails miserably:

$this->container->get('logger')->addInfo('Request: users->get-one');

Here's the code in src/dependancies.php:

<?php

$container = $app->getContainer();

// monolog
$container['logger'] = function ($c) {
    $logs = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($logs['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($logs['path'], $logs['level']));

    return $logger;
};

// database
$container['db'] = function ($c) {
    $database = $c->get('settings')['database'];

    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection([
        'driver' => 'mysql',
        'host' => $database['hostname'],
        'database' => $database['database'],
        'username' => $database['username'],
        'password' => $database['password'],
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ]);
    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};

// register users controller
require __DIR__ . '/../src/controllers/users.php';

$container['UsersController'] = function($c) {
    return new UsersController($c);
};

Below is the code that's in src/controllers/users.php:

<?php

use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;

class UsersController {
    protected $container;

    public function __construct(ContainerInterface $container) {
        $this->container = $container;
    }

    public function get(Request $request, Response $response, $args) {
        $this->container->get('logger')->addInfo('Request: users->get-one');

        /**
         * TODO: Replace $args['id'] with the id from the current token payload
         */
        $data = Users::find($args['id']);

        return $response->withJSON($data)->withStatus(200);
    }

    public function create(Request $request, Response $response, $args) {
        $this->logger->addInfo('Request: users->create');

        $user = $request->getParsedBody();

        $data = Users::create([
            'first_name' => $user['first_name'],
            'last_name' => $user['last_name'],
            'email' => $user['email'],
            'password' => password_hash($user['password'], PASSWORD_BCRYPT),
            'telephone' => $user['telephone'],
            'timezone' => $user['timezone'],
            'verification' => '011010'
        ]);

        return $response->withJSON($data)->withStatus(200);
    }

    public function update(Request $request, Response $response, $args) {
        $this->logger->addInfo('Request: users->update');

        $user = $request->getParsedBody();

        $data = Users::where('id', $args['id'])->update([
            'first_name' => $user['first_name'],
            'last_name' => $user['last_name'],
            'email' => $user['email'],
            'password' => password_hash($user['password'], PASSWORD_BCRYPT),
            'telephone' => $user['telephone'],
            'timezone' => $user['timezone']
        ]);

        return $response->withJSON($data)->withStatus(200);
    }

    public function delete(Request $request, Response $response, $args) {
        $this->logger->addInfo('Request: users->delete');

        $data = Users::destroy($args['id']);

        return $response->withJSON($data)->withStatus(200);
    }

    /*
    * ==============================
    * Manager Functions Routines
    * ==============================
    */
    public function getAll(Request $request, Response $response, $args) {
        $this->logger->addInfo('Request: admin->users->getAll');

        $data = Users::all();

        return $response->withJSON($data)->withStatus(200);
    }
}

I have tried following different tutorials as well as the documentation on the Slim website however, nothing has seemed to fix the issue. I am sure it's something easily fixed that I am just missing.

PHP Slim 3 Framework - Use MonoLog in Custom Class - Using $this when not in object context - The accepted answer here just seems silly to do when I want to be able to access the entire app container and NOT just the logger.

Any help is appreciated.

Thank you!

Upvotes: 1

Views: 715

Answers (1)

adamlmiller
adamlmiller

Reputation: 532

Well, I figured it out. Apparently using two semicolons when invoking the controller routine is not the proper way of doing things. Oops!

/*
 * users::read::one
 * method:get
 */
$app->get('/users', '\UsersController::get');

When the code above is adjust to only use one semicolon, it works:

/*
 * users::read::one
 * method:get
 */
$app->get('/users', '\UsersController:get');

Upvotes: 3

Related Questions