PHP code example of solventt / slim-route-strategy
1. Go to this page and download the library: Download solventt/slim-route-strategy library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
solventt / slim-route-strategy example snippets
$app->get('/profile/{id:\d+}', [ProfileController::class, 'show']);
...
public function show(int $id): Response
{
// incoming $id has integer type instead of default's string
}
public function show($request, $response, $id)
{
echo $request; // 'value_1'
echo $response; // 'value_2'
echo $id; // '1' - string, because the IdIntegerTypeRule is off
}
public function show(Twig $twig, self $surrentClass)
{
// The Twig and declaring class instances will be automatically resolved
}
public function show(?string $name, ?int $count = 5)
{
var_dump($name); // null
echo $count; // 5
}
public function update(Dto $dto, int $id)
{
// do something with $dto
}
return [
'dtoFactories' => [
// key - is a parameter name of a controller method
// value - a corresponding DTO factory class
'dto' => UserUpdateDtoFactory::class
]
];
class UserUpdateDtoFactory
{
public function __invoke(array $requestData): UserUpdateDto
{
$dto = new UserUpdateDto();
foreach ($requestData as $field => $value) {
$value = match ($field) {
'phoneType' => (int) $value,
'date' => new \DateTime($value),
'isActive' => (bool) $value,
default => $value
};
$dto->$field = $value;
}
return $dto;
}
}
public function update(UserUpdateDto $dto)
{
// do something with $dto
}
use DI\Container;
use Slim\Factory\AppFactory;
use SlimRouteStrategy\CustomRulesAggregator;
w CustomRulesAggregator($container);
$app->getRouteCollector()->setDefaultInvocationStrategy($strategy);
$app->get('/hello/{name}', function ($response, $name) {
$response->getBody()->write($name);
return $response;
});
$app->run();
use Slim\App;
use Slim\Container;
use SlimRouteStrategy\CustomRulesAggregator;
ggregator($container);
$app = new App($container);
$app->get('/hello/{name}', function ($response, $name) {
$response->getBody()->write($name);
return $response;
});
$app->run();