This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
This release removed attaching objects to dispatcher. So you got rid of doing
$dispatcher->setObject('blog', $di->lazyNew('Controller\Blog'));
Instead you can add to route itself as a callable or can pass the fully qualified class name.
$router = $di->get('router');
$router->add('home', '/')
->addValues(array('controller' => function ($response) {
return $response
->getBody()
->write("<p>Home page in html.</p>")
->withHeader('Content-Type', 'text/html');
}
));
$router->add('greet', '/greet')
->addValues(array(
'controller' => 'Controller\Greet',
'action' => 'hello'
));
The controller now can return string
, PSR-7 Response
object or even a Response\Payload
.
namespace Controller;
use Psr\Http\Message\ResponseInterface;
use Response\Payload;
class Blog
{
public function hello()
{
$available = array(
'text/html' => '.html',
'application/json' => '.json',
);
// $data, $view, $layout, $available
return new Payload(array('name' => 'Hari KT'), 'greet', null, $available);
}
public function returnString()
{
return "Hello World";
}
public function returnResponse(ResponseInterface $response)
{
return $response->withStatus(200)
->withHeader('Content-Type', 'text/html')
->write("returns response");
}
}
From the payload it can detect the requested content type and send the corresponding response. By default it can send html only.
Please see the tests on the usage.