Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (42 sloc)
1.24 KB
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 Karen\Controller; | |
use \Psr\Http\Message\ServerRequestInterface as Request; | |
use \Psr\Http\Message\ResponseInterface as Response; | |
abstract class MyController | |
{ | |
protected $app; | |
protected $container; | |
protected $request; | |
protected $response; | |
public function __construct($app) | |
{ | |
$this->app = $app; | |
$this->container = $app->getContainer(); | |
$this->setErrorHandler(); | |
} | |
public abstract function action($args); | |
public function __invoke(Request $request, Response $response, $args) | |
{ | |
$this->request = $request; | |
$this->response = $response; | |
return $this->action($args); | |
} | |
public function setErrorHandler() | |
{ | |
$this->container['errorHandler'] = function ($c) { | |
return function ($request, $response, $exception) use ($c) { | |
return $c['response']->withStatus(500) | |
->withHeader('Content-Type', 'text/html') | |
->write('Something went wrong!'); | |
}; | |
}; | |
} | |
public function render($output) | |
{ | |
return $this->response->write($output); | |
} | |
public function render404($output) | |
{ | |
return $this->response->withStatus(404)->write($output); | |
} | |
} |