josegarcia/router

Simple router for PHP, that minics the behavior of the Express router

2.1.1 2024-05-12 13:17 UTC

This package is auto-updated.

Last update: 2024-05-12 13:18:03 UTC


README

A simple router for PHP, inspired by Express and Laravel.

Description

This is a simple router for PHP, inspired by Express and Laravel. It allows you to define routes and their handlers, and then run the router to match the current request and execute the corresponding handler.

Installation

composer require josegarcia/router

Usage

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Garcia\Router;

// Example of a simple route
Router::addRoute('GET', '/health', fn () => 'Hello, world!');

// Example of a route with a parameter
Router::addRoute('GET', '/health/:id', fn ($params) => "User ID: {$params['id']}");

// Example of a route that returns a JSON response
Router::get( '/api/health/:id', fn ($params) => ['id' => $params['id'], 'name' => 'John Doe', 'email' => 'john@example.com']);

// Example of rendering a view
Router::get('/view', fn () => [
    // we need to pass the view name and the data to be rendered as a template
    'view' => 'template',
    // we can also pass the path to the views directory
    'path' => 'views',
    // we can also pass the data to be rendered
    'data' => [
        'name' => 'John Doe'
    ]
]);

Router::post('/health', fn ($params) => "Hello, {$params['name']} {$params['last']}!");

// Example of rendering
Router::get('/redirect', fn () => redirect('http://www.example.com'));

Router::run();

API

the router has the following methods: get, post, put, patch, delete, options, head, any, addRoute, run, redirect, view, json, and render.

get, post, put, patch, delete, options, head, any

These methods are used to define routes. They take two parameters: the route and the handler. The route can contain parameters, which are defined by a colon followed by the parameter name. The handler can be a function or a string. If it is a string, it is assumed to be the name of a function to be called.

addRoute

This method is used to define routes. It takes three parameters: the method, the route, and the handler. The route can contain parameters, which are defined by a colon followed by the parameter name. The handler can be a function or a string. If it is a string, it is assumed to be the name of a function to be called.

run

This method is used to run the router. It takes no parameters.

redirect

This method is used to redirect to another URL. It takes one parameter: the URL to redirect to.

view

This method is used to render a view. It takes one parameter: the name of the view to render.

json

This method is used to return a JSON response. It takes one parameter: the data to be returned as JSON.

render

This method is used to render a view. It takes one parameter: the name of the view to render.

Examples

You can find more examples in the examples directory.

Tests

You can run the tests with the following command:

composer test

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT