1. Go to this page and download the library: Download icanboogie/routing 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/ */
icanboogie / routing example snippets
namespace App\Articles;
use ICanBoogie\HTTP\File;
use ICanBoogie\HTTP\Response;
use ICanBoogie\Routing\Attribute\Get;
use ICanBoogie\Routing\Attribute\Part;
use ICanBoogie\Routing\Attribute\Post;
use ICanBoogie\Routing\Attribute\Prefix;
use ICanBoogie\Routing\Attribute\Query;
use ICanBoogie\Routing\Attribute\Upload;
use ICanBoogie\Routing\IntoResponse\Json;
enum Status: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';
}
#[Prefix(pattern: '/articles')]
final readonly class ArticlesController
{
public function __construct(
private ArticleRepository $articles,
) {
}
// GET /articles?page=2
#[Get(id: 'articles')]
public function list(#[Query] int $page = 1): Json
{
return new Json($this->articles->page($page));
}
// GET /articles/42 — "/articles/abc" doesn't match: 404
#[Get('/<id:\d+>', id: 'article')]
public function show(int $id): Json
{
return new Json($this->articles->get($id));
}
// GET /articles/status/draft — "/articles/status/foo" is a 404
#[Get('/status/:status')]
public function by_status(Status $status): Json
{
return new Json($this->articles->with_status($status));
}
// POST /articles, with a form or a JSON body — a missing "title" is a 400
#[Post]
public function create(#[Part] string $title, #[Part] Status $status = Status::DRAFT): Json
{
return new Json($this->articles->create($title, $status), status: 201);
}
// POST /articles/42/cover, as multipart/form-data — a missing or failed upload of "cover" is a 400,
// "thumb" is optional because the parameter is nullable
#[Post('/<id:\d+>/cover')]
public function set_cover(int $id, File $cover, #[Upload('thumb')] ?File $thumbnail): Json
{
return new Json($this->articles->set_cover($id, $cover, $thumbnail));
}
}
use ICanBoogie\HTTP\Request;
use ICanBoogie\HTTP\Responder\DelegateToProvider;
use ICanBoogie\Routing\ActionResponderProvider\FromConfig;
use ICanBoogie\Routing\CacheStrategy;
use ICanBoogie\Routing\ConfigBuilder;
use ICanBoogie\Routing\RequestResponderProvider;
/* @var \Psr\Container\ContainerInterface $container */
/* @var \ICanBoogie\Routing\UrlGenerator $url_generator */
$config = new ConfigBuilder()
->add_controller(App\Articles\ArticlesController::class)
->cache_strategy(CacheStrategy::COMPILE) // compiled routes for production, exportable with var_export()
->build();
$responders = new FromConfig($config, fn(string $class): object => $container->get($class));
$application = new DelegateToProvider(new RequestResponderProvider($config->routes, $responders));
$response = $application->respond(Request::from($_SERVER));
$response();
$url_generator->generate_url('article', [ 'id' => 42 ]); // "/articles/42"
$url_generator->generate_url('articles'); // "/articles"
$url_generator->generate_url('articles:by_status', [ 'status' => 'draft' ]); // "/articles/status/draft"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.