PHP code example of juliangut / slim-controller

1. Go to this page and download the library: Download juliangut/slim-controller library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

juliangut / slim-controller example snippets




use \Jgut\Slim\Controller\Resolver;

// Define your controllers
$controllers = [
    'MyController',
];

// Create Slim app and fetch DI Container
$app = new \Slim\App();
$container = $app->getContainer();

// Register Controllers
foreach (Resolver::resolve($controllers) as $controller => $callback) {
    $container[$controller] = $callback;
}

// Define route (\MyController has already been registered)
$app->get('hello/app', '\MyController:dispatch');

// Run app
$app->run();

use Jgut\Slim\Controller\Base as BaseController;

class MyController extends BaseController
{
    public function displatch($request, $response, array $args)
    {
        // Pull Twig view service given it was defined
        return $this->view->render($response, 'dispatch.twig');
    }
}

use Jgut\Slim\Controller\Controller;

$container['\MyController'] = function($container) {
    $controller = new \MyController('customParameter');

    // Set container into the controller
    if ($controller instanceof Controller) {
        $controller->setContainer($container);
    }

    return $controller;
}