All Projects → noahbuscher → Macaw

noahbuscher / Macaw

Licence: mit
🐦 Simple PHP router

Labels

Projects that are alternatives of or similar to Macaw

Dispatch
a tiny library for quick and easy PHP 5.6+ apps
Stars: ✭ 535 (-39.75%)
Mutual labels:  router
Flutter thrio
flutter_thrio makes it easy and fast to add flutter to existing mobile applications, and provide a simple and consistent navigator APIs.
Stars: ✭ 717 (-19.26%)
Mutual labels:  router
Preact Router
🌎 URL router for Preact.
Stars: ✭ 771 (-13.18%)
Mutual labels:  router
Gear
A lightweight, composable and high performance web service framework for Go.
Stars: ✭ 544 (-38.74%)
Mutual labels:  router
Navaid
A navigation aid (aka, router) for the browser in 850 bytes~!
Stars: ✭ 648 (-27.03%)
Mutual labels:  router
Androuter
A android router framework used to map url to activities or actions.
Stars: ✭ 730 (-17.79%)
Mutual labels:  router
React Router Page Transition
Highly customizable page transition component for your React Router
Stars: ✭ 531 (-40.2%)
Mutual labels:  router
One
一个极简高性能php框架,支持[swoole | php-fpm ]环境
Stars: ✭ 789 (-11.15%)
Mutual labels:  router
Rapid.js
An ORM-like Interface and a Router For Your API Requests
Stars: ✭ 700 (-21.17%)
Mutual labels:  router
Path To Regexp
Turn a path string such as `/user/:name` into a regular expression
Stars: ✭ 6,789 (+664.53%)
Mutual labels:  router
React Router Relay
[Deprecated] Relay Classic integration for React Router
Stars: ✭ 561 (-36.82%)
Mutual labels:  router
Micro Router
🚉 A tiny and functional router for Zeit's Micro
Stars: ✭ 621 (-30.07%)
Mutual labels:  router
Router
一款单品、组件化、插件化全支持的Andoid端路由框架。
Stars: ✭ 741 (-16.55%)
Mutual labels:  router
Rill
🗺 Universal router for web applications.
Stars: ✭ 541 (-39.08%)
Mutual labels:  router
React Keeper
A routing library of React.
Stars: ✭ 774 (-12.84%)
Mutual labels:  router
Kakapo.js
🐦 Next generation mocking framework in Javascript
Stars: ✭ 535 (-39.75%)
Mutual labels:  router
Found
Extensible route-based routing for React applications
Stars: ✭ 718 (-19.14%)
Mutual labels:  router
Xunlei Fastdick
迅雷快鸟 Xunlei Network Accelerator For Router
Stars: ✭ 789 (-11.15%)
Mutual labels:  router
Find My Way
A crazy fast HTTP router
Stars: ✭ 776 (-12.61%)
Mutual labels:  router
Lion
Lion is a fast HTTP router for building modern scalable modular REST APIs in Go
Stars: ✭ 750 (-15.54%)
Mutual labels:  router

Macaw

Macaw is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to just throw it into your project and start using it immediately.

Install

If you have Composer, just include Macaw as a project dependency in your composer.json. If you don't just install it by downloading the .ZIP file and extracting it to your project directory.

require: {
    "noahbuscher/macaw": "dev-master"
}

Examples

First, use the Macaw namespace:

use \NoahBuscher\Macaw\Macaw;

Macaw is not an object, so you can just make direct operations to the class. Here's the Hello World:

Macaw::get('/', function() {
  echo 'Hello world!';
});

Macaw::dispatch();

Macaw also supports lambda URIs, such as:

Macaw::get('/(:any)', function($slug) {
  echo 'The slug is: ' . $slug;
});

Macaw::dispatch();

You can also make requests for HTTP methods in Macaw, so you could also do:

Macaw::get('/', function() {
  echo 'I'm a GET request!';
});

Macaw::post('/', function() {
  echo 'I'm a POST request!';
});

Macaw::any('/', function() {
  echo 'I can be both a GET and a POST request!';
});

Macaw::dispatch();

Lastly, if there is no route defined for a certain location, you can make Macaw run a custom callback, like:

Macaw::error(function() {
  echo '404 :: Not Found';
});

If you don't specify an error callback, Macaw will just echo 404.


In order to let the server know the URI does not point to a real file, you may need to use one of the example configuration files.

Example passing to a controller instead of a closure


It's possible to pass the namespace path to a controller instead of the closure:

For this demo lets say I have a folder called controllers with a demo.php

index.php:

require('vendor/autoload.php');

use NoahBuscher\Macaw\Macaw;

Macaw::get('/', 'Controllers\[email protected]');
Macaw::get('page', 'Controllers\[email protected]');
Macaw::get('view/(:num)', 'Controllers\[email protected]');

Macaw::dispatch();

demo.php:

<?php
namespace Controllers;

class Demo {

    public function index()
    {
        echo 'home';
    }

    public function page()
    {
        echo 'page';
    }

    public function view($id)
    {
        echo $id;
    }

}

This is with Macaw installed via composer.

composer.json:

{
   "require": {
        "noahbuscher/macaw": "dev-master"
    },
    "autoload": {
        "psr-4": {
            "" : ""
        }
    }
}

.htaccess(Apache):

RewriteEngine On
RewriteBase /

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?$1 [QSA,L]

.htaccess(Nginx):

rewrite ^/(.*)/$ /$1 redirect;

if (!-e $request_filename){
	rewrite ^(.*)$ /index.php break;
}

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].