PHP code example of rodrifarias / slim-route-attributes
1. Go to this page and download the library: Download rodrifarias/slim-route-attributes 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/ */
rodrifarias / slim-route-attributes example snippets
odrifarias\SlimRouteAttributes\App\AppSlimFactory;
use Rodrifarias\SlimRouteAttributes\Route\Scan\ScanRoutes;
$pathDirControllers = __DIR__ . '/your-dir';
$app = AppSlimFactory::create();
$app->registerRoutes($pathDirControllers, new ScanRoutes());
$app->run();
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Delete;
use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Get;
use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Patch;
use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Post;
use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Put;
use Rodrifarias\SlimRouteAttributes\Attributes\Middleware;
use Rodrifarias\SlimRouteAttributes\Attributes\PublicAccess;
use Rodrifarias\SlimRouteAttributes\Attributes\Route;
use Rodrifarias\SlimRouteAttributes\Tests\Unit\Route\Middleware\MiddlewareAfter;
use Rodrifarias\SlimRouteAttributes\Tests\Unit\Route\Middleware\MiddlewareBefore;
#[Route('/')]
class HomeController
{
#[Get, PublicAccess(true)]
public function showAll(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
$response->getBody()->write('Home');
return $response->withHeader('Content-type', 'application/json');
}
#[Get('optional[/{id:[0-9]+}]')]
public function optional(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$id = $args['id'] ?? '';
$response->getBody()->write('Optional' . $id);
return $response->withHeader('Content-type', 'application/json');
}
#[Get('{id:\d+}'), PublicAccess(true)]
public function show(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$response->getBody()->write('Hello ' . $args['id']);
$response->withHeader('Content-type', 'application/json');
return $response->withStatus(200);
}
#[Post, Middleware([MiddlewareAfter::class, new MiddlewareBefore()])]
public function create(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $response->withHeader('Content-type', 'application/json');
}
#[Put('{id:\d+}')]
public function update(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$response->getBody()->write('Updated ' . $args['id']);
return $response->withHeader('Content-type', 'application/json');
}
#[Delete('{id:\d+}')]
public function delete(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$response->withHeader('Content-type', 'application/json');
return $response->withStatus(204);
}
#[Patch('{id:\d+}')]
public function updatePatch(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$response->getBody()->write('Updated Patch ' . $args['id']);
return $response->withHeader('Content-type', 'application/json');
}
}
#[Route('/home')]
class HomeController
{
#[Get, PublicAccess(true), Middleware([MiddlewareAfter::class])]
public function showAll(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
$response->getBody()->write('Home');
return $response->withHeader('Content-type', 'application/json');
}
}
#[Route('/home')]
class HomeController
{
#[Map('/map', ['POST', 'GET'])]
public function mapPostGet(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
$response->getBody()->write('Map With Post and Get');
return $response->withHeader('Content-type', 'application/json');
}
}
use Rodrifarias\SlimRouteAttributes\Route\Scan\ScanRoutes;
your-dir');
// you can use getRoutes with cache
$routes = $scan->getRoutes(__DIR__ . '/your-dir', true);
foreach ($routes as $route) {
echo $route . PHP_EOL . PHP_EOL;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.