Created
April 18, 2017 01:19
-
-
Save angelxmoreno/480541a6976d8534fcfa27301c2ed3e9 to your computer and use it in GitHub Desktop.
Sample Slim Framework Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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