elfcorreia/routing

A routing library

dev-main 2021-02-01 14:21 UTC

This package is auto-updated.

Last update: 2024-04-29 04:28:16 UTC


README

This is a library that introduces a minimal set of functions to work with routing in php applications.

Installation

Install with composer require elfcorreia/routing @dev

Quick start

<?php

use function routing\route;
use function routing\debug;

route('/');
route('/posts');
route('/posts/{name:str}');

debug();

Usage

All the functions from this library are in routing namespace. So in order to use its you must import a function or call it by its qualified name.

Adds a route

Add a route calling the function route with a required path", an optional callback, and and optional name. Example:

route('/');
route('/posts');
route('/posts/{name:str}');
route('/posts/{name:str}/comments');

The function routing/not_implemented_yet_handler its the default callback when none is specified. In order to do that you must pass a callback/callable variable.

// a function name
route('/', 'index_view'); 
// an anonimous function 
route('/posts', function () { /**/ });
// an arrow function
route('/posts/{name:str}', fn ($name) => { /**/ });
// an static class method
route('/posts/{name:str}/comments', 'PostsController::comments');
// an instance method
route('/posts/{name:str}/comments', [new MyClass(), 'comments']);

A path param is specified in a path with '{name:type}', where name must be a valid PHP identifier and type should be a valid path type.