News for hackers. Stuff that matters.
Re:Macaw - A Simple PHP Router by nodesocket on Saturday December 28, 2013 @10:51PM (#6978260)
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");
    });
Re:Macaw - A Simple PHP Router by X4 on Sunday December 29, 2013 @02:59AM (#6978823)
fourofour is not as semantic as notFound, or not_found xD ..

idk. this mixes up routing and api building, right? What people currently use most to build their api (mostly afterwards) is: http://docs.guzzlephp.org/en/latest/

btw. here's a nice and new php framework I've just recently found: http://www.cygniteframework.com/2013/07/quickstart.html

Re:Macaw - A Simple PHP Router by NoahBuscher on Sunday December 29, 2013 @12:33AM (#6978544)
Errors are now functions!
Re:Macaw - A Simple PHP Router by girvo on Saturday December 28, 2013 @10:56PM (#6978278)
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.
Re:Macaw - A Simple PHP Router by userbinator on Saturday December 28, 2013 @11:13PM (#6978338)
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.

Re:Macaw - A Simple PHP Router by X4 on Sunday December 29, 2013 @03:08AM (#6978847)
an even better solution is http://cumulusnetworks.com/ but it's not in php. wouldn't make sense in php anyway.
Re:Macaw - A Simple PHP Router by Pengu on Sunday December 29, 2013 @02:53AM (#6978816)
pfsense is as close as it gets
Re:Macaw - A Simple PHP Router by augustohp on Sunday December 29, 2013 @02:04AM (#6978734)
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.

Re:Macaw - A Simple PHP Router by chime on Saturday December 28, 2013 @11:34PM (#6978382)
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.

Re:Macaw - A Simple PHP Router by nodesocket on Saturday December 28, 2013 @11:50PM (#6978435)
+1 for flight. Simple, yet powerful.
Re:Macaw - A Simple PHP Router by X4 on Sunday December 29, 2013 @03:04AM (#6978837)
I once used http://fatfreeframework.com/home in a mozilla hackathon; was quite easy to get going.
Re:Macaw - A Simple PHP Router by citricsquid on Sunday December 29, 2013 @12:03AM (#6978472)
How does flight compare with klein (https://github.com/chriso/klein.php)?
Re:Macaw - A Simple PHP Router by agumonkey on Sunday December 29, 2013 @12:59AM (#6978601)
klein, bottle, flask .. Am I seeing things ?
Re:Macaw - A Simple PHP Router by X4 on Sunday December 29, 2013 @03:02AM (#6978832)
what do you mean?
Re:Macaw - A Simple PHP Router by agumonkey on Sunday December 29, 2013 @03:14AM (#6978871)
Bottle and Flask are python microframework, I just wonder if Klein's name is a reference to them.
Re:Macaw - A Simple PHP Router by thejosh on Sunday December 29, 2013 @01:25AM (#6978649)
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.

Re:Macaw - A Simple PHP Router by phaed on Saturday December 28, 2013 @11:28PM (#6978368)
http://macaw.co/ Oblivious or on purpose?
Re:Macaw - A Simple PHP Router by brickcap on Sunday December 29, 2013 @12:11AM (#6978500)
The OP has another project called parrot

https://github.com/GetParrot/Parrot

so I think he just likes parrots and the name is coincidence.

Re:Macaw - A Simple PHP Router by NoahBuscher on Sunday December 29, 2013 @12:34AM (#6978547)
Yep! Macaw will be the future router of Parrot. I'm doing a rewrite.
Re:Macaw - A Simple PHP Router by phaed on Sunday December 29, 2013 @12:14AM (#6978507)
Ah, there you go. That is quite a coincidence.
Re:Macaw - A Simple PHP Router by krapp on Saturday December 28, 2013 @11:42PM (#6978405)
two people can't name their thing after the same bird?
Re:Macaw - A Simple PHP Router by phaed on Saturday December 28, 2013 @11:46PM (#6978427)
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.
Re:Macaw - A Simple PHP Router by Implicated on Sunday December 29, 2013 @01:48AM (#6978701)
I've definitely never heard of your project. Not saying this to be mean, just show that not everyone has heard of it.
Re:Macaw - A Simple PHP Router by phaed on Sunday December 29, 2013 @01:30PM (#6980591)
Not my project.
Re:Macaw - A Simple PHP Router by krapp on Saturday December 28, 2013 @11:55PM (#6978446)
Or maybe it's not nearly as big a deal as you think it is and OP just likes parrots.
Re:Macaw - A Simple PHP Router by ars_technician on Sunday December 29, 2013 @02:30AM (#6978778)
I have never heard of macaw.co. I think your idea of developers that care about web design is flawed.
Re:Macaw - A Simple PHP Router by mikeg8 on Saturday December 28, 2013 @11:57PM (#6978450)
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.
Re:Macaw - A Simple PHP Router by vaidik on Sunday December 29, 2013 @04:47AM (#6979035)
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.

Re:Macaw - A Simple PHP Router by martin_ on Sunday December 29, 2013 @05:45AM (#6979160)
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
Re:Macaw - A Simple PHP Router by krapp on Sunday December 29, 2013 @09:35AM (#6979623)
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.

Re:Macaw - A Simple PHP Router by pplante on Saturday December 28, 2013 @10:55PM (#6978272)
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.

Re:Macaw - A Simple PHP Router by girvo on Saturday December 28, 2013 @10:57PM (#6978286)
.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 :)
Re:Macaw - A Simple PHP Router by pplante on Saturday December 28, 2013 @10:59PM (#6978293)
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"
Re:Macaw - A Simple PHP Router by fidz on Sunday December 29, 2013 @12:25AM (#6978531)
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.
Re:Macaw - A Simple PHP Router by jmathai on Sunday December 29, 2013 @03:59AM (#6978939)
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...

Re:Macaw - A Simple PHP Router by fidz on Sunday December 29, 2013 @06:38PM (#6982101)
It still use sequential matching[0]. What i meant is something like AST implementation in routing algorithm, which is obviously faster than sequential algorithm [1] [2]

[0]: https://github.com/jmathai/epiphany/blob/master/src/EpiRoute...

[1]: http://en.wikipedia.org/wiki/Shunting-yard_algorithm

[2]: http://blog.bigbinary.com/2013/01/29/journey-into-rails-rout...

Re:Macaw - A Simple PHP Router by y1426i on Friday January 03, 2014 @11:48AM (#7006392)
Magic methods are expensive in PHP. You might as well define those explicitly.
Re:Macaw - A Simple PHP Router by 10098 on Sunday December 29, 2013 @12:44AM (#6978569)
It's... 87 lines of PHP?
Re:Macaw - A Simple PHP Router by Phil_Latio on Sunday December 29, 2013 @01:04AM (#6978613)
Don't worry, I don't get it either...
Re:Macaw - A Simple PHP Router by tmzt on Sunday December 29, 2013 @03:30AM (#6978899)
I don't see a composer.json in the root of the Github. Is that planned?
Re:Macaw - A Simple PHP Router by timmow on Sunday December 29, 2013 @06:40AM (#6979281)
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
Re:Macaw - A Simple PHP Router by NoahBuscher on Sunday December 29, 2013 @08:10AM (#6979439)
I'll add one after I fix some things up.
Re:Macaw - A Simple PHP Router by whichdan on Saturday December 28, 2013 @10:53PM (#6978267)
Looks like a nice alternative to using Silex if you just need routing.
Re:Macaw - A Simple PHP Router by krapp on Saturday December 28, 2013 @11:22PM (#6978356)
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.

Re:Macaw - A Simple PHP Router by NoahBuscher on Sunday December 29, 2013 @12:34AM (#6978548)
Great! I'll get to work on these. :D
Re:Macaw - A Simple PHP Router by X4 on Sunday December 29, 2013 @03:24AM (#6978884)
how about caching as a separate composer requirement, or do you think caching should be in a higher level in the stack?
Re:Macaw - A Simple PHP Router by krapp on Sunday December 29, 2013 @09:34AM (#6979616)
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.

Re:Macaw - A Simple PHP Router by pauletienney on Sunday December 29, 2013 @08:24AM (#6979463)
Double name is double. cf : http://macaw.co/
Re:Macaw - A Simple PHP Router by vayarajesh on Sunday December 29, 2013 @04:07AM (#6978950)
Handy. Nice