• This repository has been archived on 15/Jan/2022
  • Stars
    star
    899
  • Rank 48,781 (Top 1.0 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The simple PHP 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\demo@index');
Macaw::get('page', 'Controllers\demo@page');
Macaw::get('view/(:num)', 'Controllers\demo@view');

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;
}

More Repositories

1

inspire

Collection of frontend dev and web design links ๐Ÿ’ก
1,162
star
2

flatron

Minimalist Sublime Text theme
JavaScript
246
star
3

n1-taiga

A clean, Mailbox-inspired theme for Nylas N1 ๐ŸŒฒ
CSS
204
star
4

shore

Simple JS waveform library
JavaScript
131
star
5

glide

Deprecated
JavaScript
72
star
6

n1-jiffy

N1 plugin to search and add gifs to emails
JavaScript
66
star
7

react-tsdoc

Document Typescript React components with TSDoc and export Storybook-friendly JSON ๐Ÿค–
TypeScript
17
star
8

spigot

Zen RSS reader.
PHP
16
star
9

gumnode

Node client for Gumroad API
JavaScript
9
star
10

resumes

Free, clean, simple, and stylish resume templates ๐Ÿ“ Available in: Figma (.fig)
6
star
11

toy-json-parser

Tiny toy JSON(like) parser, written in Typescript
TypeScript
4
star
12

kempt

Fresh WordPress theme
CSS
4
star
13

skeleton

๐Ÿ’€ Barebones WordPress theme
CSS
3
star
14

react-tsdoc-loader

Loader to use react-tsdoc blobs in Storybook Docs plugin (ArgsTable, Controls)
JavaScript
3
star
15

markdown-portfolio-tutorial

Simple Markdown-powered Next.js portfolio
TypeScript
3
star
16

prettycommon.com

The homestead
JavaScript
1
star
17

chainpoint-cloudflare-app-web

Chainpoint Client Web for Cloudflare โ˜๏ธ
JavaScript
1
star
18

instascrape

TypeScript
1
star
19

noahbuscher

1
star
20

photocreds

List of places I've seen my photos ๐Ÿ“ท
1
star
21

jayforeman.net

Jay's personal site
HTML
1
star
22

portfolio-old

The tiny house of portfolios ๐Ÿ 
HTML
1
star
23

meeet.co

Meeet lander
CSS
1
star
24

engrain-dev-test

Dev test for Engrain
CSS
1
star
25

402.studio

A space for creatives.
HTML
1
star