51 comments

[ 3.0 ms ] story [ 121 ms ] thread
Probably shouldn't be "hard" coding the scheme (http). Check $_SERVER['https']. You can make sure this fastcgi param exists with a simple directive in nginx (fastcgi_params):

    fastcgi_param  HTTPS  $https if_not_empty;
Also, the 404 route, should be able to be defined as a custom function:

    Macaw::fourofour(function() {
        header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
        header("Status: 404 Not Found");
    });
Looks like a nice alternative to using Silex if you just need routing.
this is very elegant. i haven't used php (beyond wordpress) in years, so i am sure a lot has changed with all the frameworks which cropped up. one of the things i miss about php was the simplicity of dropping a file into a path and making requests directly to it, no extra dependencies required. i know a lot of people prefer prettier urls, so why not use .htaccess file for mod_rewrite rather than using something like macaw?

nice job on creating a really damn simple interface.

.htacess and nginx rewriting breaks down when you need to do more complex stuff, at least if you wasn't to keep things maintainable and clean. Pushing routing like this through to the app side gives a lot more flexibility :)
i can see why having route parsing in the code would be useful. especially when dealing with user created urls such as "/user/pplante/profile"
Nice. Php's community have -- with the advent of sane dependency management through Composer, no more PEAR yay! -- move to this small composable library style. I think it's a great thing, using Macaw (or Slim, in my case) plus your business logic modelled as raw, SOLID, tested classes gives you an amazing API to work with,both on the PHP side, and on the network side. I haven't needed to use Symfony for anything new lately,though if I was building a huge app from scratch with a larger team, Symfony is where it's at.
Oh, that sort of router...

For a second I thought someone had coded an IP router in PHP. Which would be a really interesting thing to see, if not particularly practical.

pfsense is as close as it gets
(comment deleted)
really nice.

if I had any suggestions at all though it would be to have the 404 route actually return the 404 header (or have a way to specify headers with routes), and also have it handle https.

Great! I'll get to work on these. :D
how about caching as a separate composer requirement, or do you think caching should be in a higher level in the stack?
If you can send headers with routes then I suppose you could cache with them as well. Although that could also get a bit tricky if you didn't want the routes to enclose an entire document. The typical use cases i'm thinking of would be a json route and an image route.

Probably, all a bare minimum viable url router should do is display some content based on a url or fail to an error code. I would post my own experiments with building a url router that deals with everything (because I tried it) but the end results were objectively worse than what's already been posted here... i've found that it helps to have the router, and whatever process handles rendering the document as a whole (including headers and caching) be completely separate.

http://macaw.co/ Oblivious or on purpose?
two people can't name their thing after the same bird?
Well when one thing has had a viral kickstarter campaign, been on every Tech blog, and its name known by just about every developer who cares about web design. Then to hear something else named the exact same name around the same time feels kinda weird.
Or maybe it's not nearly as big a deal as you think it is and OP just likes parrots.
I've definitely never heard of your project. Not saying this to be mean, just show that not everyone has heard of it.
Not my project.
I have never heard of macaw.co. I think your idea of developers that care about web design is flawed.
(comment deleted)
I agree with OP. While maybe "oblivious" is a bit condescending, I would be a little peeved if I was a founder of Macaw... the design tool that has been hugely popular here lately.
If you want something with more features but still just as simple, check out: http://flightphp.com/

I'm using it to make a JSON backend API and it works great.

+1 for flight. Simple, yet powerful.
I'm a huge fan of silex, I use it as a JSON backend API that fetches from a SOAP API.

Silex and other microframeworks are great for these.

Is there any pattern matching router in PHP? Just like Journey on Rails. It is faster to use Patrern Matching than sequential brute force route.
Not just a router but Epiphany[1] lets you specify a regex for routes[2]. My favorite part though is the API module which I blogged about a while back[3].

[1] https://github.com/jmathai/epiphany

[2] https://github.com/jmathai/epiphany/blob/master/examples/reg...

[3] https://trovebox.com/blog/building-trovebox-entirely-using-o...

It's... 87 lines of PHP?
Don't worry, I don't get it either...
I like the "slim" approach on LOC, kudos to that. I would point some - IMO - design problems:

# Static attributes and methods. Are they really needed? They are difficult to test and predict, specially the public attributes.

# The "echo"s on routes. Since every route is a method (encapsulated by one), this behavior could be easily avoided (easier to test and control). You are already using callables, you can easily test (and run) matching routes to execute any callable until a stream is given (on Respect/Rest we learned what a powerful feature it is).

# Lack of tests. Although low LOC, there is much happening, specially on the "dispatch" method (it would be awesome to know through tests what the If's/Else's actually do and prevent).

# Why (as mentioned before) the error header do not provide a proper HTTP header!? It would also be nice to provide two different error routes: one for client errors (404: not found, forbidden) and another to server errors (HTTP/1.1 500 Server Error).

A suggestion (we've been talking a lot on Respect/Rest) is to implement the URI Template (RFC 6570) for matching routes and hydration of parameters (something macaw lacks but i like using the PHP way of $_GET, $_POST and filter_input()). With a component dealing nicely with URI Templates many projects would benefit from it.

I don't see a composer.json in the root of the Github. Is that planned?
One problem is the library as it stands doesn't match up to composers namespacing requirements - which some php developers have decided they don't like and therefore are not going to support
I'll add one after I fix some things up.
Great project. I personally prefer Python over PHP and use it a lot for most of the web applications. But when I must use PHP in some cases, I like frameworks like this.

For precisely this reason, I wrote a PHP router like this called Marg - http://github.com/vaidik/marg. Used it for 3 projects and I love the way I can keep things simple and sane. And that's why I appreciate this project as well.

Great to see sane projects like these in the PHP community.

Sane PHP developers typically don't use global to avoid using objects properly! I only mention it because it seems you haven't given PHP a fair chance
to be fair, I've seen that idiom used quite a bit when calling database objects. It's still terrible.

Not as bad as using extract() though. Yes i'm looking at you Wordpress.

Magic methods are expensive in PHP. You might as well define those explicitly.