Skip to content

Instantly share code, notes, and snippets.

@angelxmoreno
Created April 18, 2017 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelxmoreno/480541a6976d8534fcfa27301c2ed3e9 to your computer and use it in GitHub Desktop.
Save angelxmoreno/480541a6976d8534fcfa27301c2ed3e9 to your computer and use it in GitHub Desktop.
Sample Slim Framework Controller
<?php
namespace App\Controllers;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Container\ContainerInterface as Container;
class HomeController
{
/**
* @var Container
*/
protected $container;
/**
* @var Request
*/
protected $request;
/**
* @var Response
*/
protected $response;
/**
* HomeController constructor
*
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* @param Request $request
* @param Response $response
* @param array $args
*
* @return string
*/
public function __invoke(Request $request, Response $response, array $args)
{
$this->request = $request;
$this->response = $response;
$this->args = $args;
//@todo assert the method exists or meaningful throw exception
$method = $this->args['action'] . 'Action';
if (@$this->args['params']) {
$params = explode('/', $this->args['params']);
return call_user_func_array([$this, $method], $params);
} else {
return $this->{$method}();
}
}
/**
*
*/
public function homeAction()
{
echo 'I am the home action';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment