Injecting Objects
As well as Route Parameters, regular objects from the Container can be injected into a Controller method:
namespace App\Web\Controllers;
use Centum\Interfaces\Http\ResponseInterface;
use Centum\Interfaces\Http\SessionInterface;
use Centum\Interfaces\Router\ControllerInterface;
class UserController implements ControllerInterface
{
public function logout(SessionInterface $session): ResponseInterface
{
$session->clear();
}
}
Thanks to Centum\Container\Resolver\RouterRequestResolver
, you also have direct access to things within the Request:
DataInterface
HeadersInterface
CookiesInterface
FilesInterface
- Individual
CookieInterface
objects - Individual
FileGroupInterface
objects
For example, to access the HTTP Headers from within a Controller:
namespace App\Web\Controllers;
use Centum\Interfaces\Http\HeadersInterface;
use Centum\Interfaces\Http\ResponseInterface;
use Centum\Interfaces\Router\ControllerInterface;
class IndexController implements ControllerInterface
{
public function index(HeadersInterface $headers): ResponseInterface
{
// ...
}
}
In this example, we can access a Cookie with the ID of theme
and a File Group with the ID of files
:
namespace App\Web\Controllers;
use Centum\Interfaces\Http\CookieInterface;
use Centum\Interfaces\Http\FileGroupInterface;
use Centum\Interfaces\Http\ResponseInterface;
use Centum\Interfaces\Router\ControllerInterface;
class UploadController implements ControllerInterface
{
public function submit(CookieInterface $theme, FileGroupInterface $files): ResponseInterface
{
// ...
}
}
If the Cookie or File Group is optional, then you can make the parameter optional:
namespace App\Web\Controllers;
use Centum\Interfaces\Http\CookieInterface;
use Centum\Interfaces\Http\ResponseInterface;
use Centum\Interfaces\Router\ControllerInterface;
class ProfileController implements ControllerInterface
{
public function index(?CookieInterface $theme): ResponseInterface
{
// ...
}
}