Controllers

Although Slim 3 is not a MVC (model-view-controller) framework, it is very easy to simulate this without insulting the concepts of the makers of Slim 3.

Autoloading controllers

The controllers can be placed in the folder 'Controllers' under the 'app' folder. In this folder you will find a 'Controller.php', which is the class on which all
the other controllers to be made are extended.

<?php
namespace App\Controllers;

use Enovision\Slim\Http\Helper\Postman;
use Tmdb\ApiToken;
use Tmdb\Client;

class Controller
{
    protected $container;
    protected $settings;
    protected $postman;

    public $client;

    public function __construct($container, $request)
    {
        $this->container = $container;
        $this->settings = $this->config->getConfig();
        $this->postman = new Postman($request);

        /* TMDB Client Init */
        $token = new ApiToken( TMDB_API_KEY );
        $this->client = new Client( $token );
    }

    public function __get($property)
    {
        return isset($this->container->{$property}) ? $this->container->{$property} : null;
    }

    /**
     * Handle the response and put it into a standard JSON structure.
     *
     * @param bool $status Pass/fail status of the request
     * @param string $message Message to put in the response [optional]
     * @param array $addl Set of additional information to add to the response [optional]
     */
    protected function jsonResponse($status, $message = null, array $addl = [])
    {
        $output = ['success' => $status];
        if ($message !== null) {
            $output['message'] = $message;
        }
        if (!empty($addl)) {
            $output = array_merge($output, $addl);
        }

        $response = $this->response->withHeader('Content-type', 'application/json');
        $body = $response->getBody();
        $body->write(json_encode($output));

        return $response;
    }

    /**
     * Handle a failure response.
     *
     * @param string $message Message to put in response [optional]
     * @param array $addl Set of additional information to add to the response [optional]
     */
    protected function jsonFail($message = null, array $addl = [])
    {
        return $this->jsonResponse(false, $message, $addl);
    }

    /**
     * Handle a success response.
     *
     * @param string $message Message to put in response [optional]
     * @param array $addl Set of additional information to add to the response [optional]
     */
    protected function jsonSuccess($message = null, array $addl = [])
    {
        return $this->jsonResponse(true, $message, $addl);
    }
}

Sample of a controller

<?php

namespace App\Controllers;

// use App\Models\User;
use \Tmdb\Repository\MovieRepository;
use \Tmdb\Repository\TvRepository;

class BoxOffice extends Controller {

    public function __construct( $container, $request ) {
        parent::__construct( $container, $request );
    }

    public function Popular() {
        $repository = new MovieRepository( $this->client );
        $result = $repository->getPopular( ); // Popular Movies
        return $result;
    }

    public function PopularTV() {
        $repository = new TvRepository( $this->client );
        $result = $repository->getTopRated( ); // Popular Movies
        return $result;
    }

    public function BoxOfficeTV() {
        $result = $this->mod_tmdb->BoxOffice( true, false );
        $result['records'] = $result['records']['boxoffice_tv'];
        $result['total'] = count( $result['records'] );
        $this->feedback( $result );
        // echo json_encode($result);
    }

    public function PersonsPopular() {
        $result = $this->mod_tmdb->PersonsPopular();
        $this->feedback( $result );
        // echo json_encode($result);
    }

    public function MoviesPopular() {
        $result = $this->mod_tmdb->MoviesPopular();
        $this->feedback( $result );
        // echo json_encode($result);
    }
}

Sample of corresponding route

<?php

use Enovision\Slim\Http\Middleware\Input;
use Enovision\Slim\Http\Middleware\Jsonp;
use App\Helpers\Util;

$app->group( '/movies', function () {

    $this->get( '/BoxOffice', function ( $request, $response ) {
        $postman = $request->getAttribute( 'input.Postman' );

        if ( $postman->hasErrors() ) {
            $feedback = $postman->Error();
        } else {
            $feedback = ['success' => true, 'message' => 'Super, everything is fine'];
        }

        return Util::Response($feedback, $response);

    } )->add( new Input( [
        'level' => 'controller',
        'johan' => 'pohan'
    ]) );

} )->add( new Input( [
    'start' => 0,
    'limit' => 50
] ) )->add( new Jsonp( 'cbJSON' ) );
Last update: October 22nd at 8:39am