Toro - a php micro-router with great examples
github.com
github.com
RewriteRule ^foo/([0-9]+)/bar /foo/bar.php?x=$1 [L,NC,QSA]
Done.
These things come across as monkey-see-monkey-do. Other frameworks in other languages have routers, therefore they must also make sense in PHP.
Edit: Removed unnecessary snarky comment.
Apps often don't have a physical URL that you could map to in .htaccess like /foo/bar.php. There's only index.php and the appropriate controller/method is executed based on the URL. Of course you could just map .htaccess to something like /index.php?method=foobar instead. But then you're still dealing with a router in your index.php, perhaps a simple switch statement or something. It wouldn't bother me to see this kind of setup but I think having a router is just a bit cleaner.
You can also abstract retrieving values from the URL like /account/1234 (obtaining "1234") so that the router does all the parsing and the controller isn't aware of the URL implementation. Perhaps useful for altering URLs later without affecting the controller code. Keeping things separate also can make unit testing somewhat easier as you can substitute the router with a mock object and get your controller to respond to all the variations of user input.
This is a false dichotomy. A good abstraction makes things simpler.
Tying your app to a particular server is a design choice. Some people like it, some people don't.
But I agree that in some cases the fastest you can do is an htaccess route.
I'm not excited to correct you, because the way it it doesn't quite hold true involves dragons, but I thought I would in case people doubt the power of apache configuration (nginx is much the same way).
It is possible, with included modules like the one below. There is also the ability to use custom modules, which are programmatic in nature and have access to Apache at a low level. Since it wasn't specified whether a shared host is being used, I think it's within the scope of the discussion.
There are a few things I would have to add before I would use it however, and I hope they make it in some day:
1) You should add a whitelist for the HTTP request method names, i.e only allow calls to get(), post(), not some_suspicious_call_from_bad_user(). People will often put non HTTP methods in their views (view as in django sense) and you don't want them to be able to be called by a malicious user.
2) You should make it easy for the developer to configure their own xhr headers.
3) Nitpick: You should probably use 405 (Method Not Allowed) instead of 404 for when a method handler isn't found.
Maybe I'll use it in this project, in which case you may have a few pull requests headed your way!
location {
root /usr/share/nginx/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$request_uri;
}url.rewrite-once = ( "^/(.*)$" => "index.php/$1" )
This is probably what I would use for PHP, but obviously other languages have better solutions like ruby/sinatra, python/flask etc.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-dRewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php
I mean, what could possibly go wrong with untestable code and unfiltered input...?
The code is actually pretty tight and is optimized for empowering a developer rather than inflating the bumpers of your bowling lane.
Unfiltered input? You realise that this is a router, right? The only 'input' is the URL path.
I really wish you could be more specific about these 'worst practices' that make this 91-line library an 'unmaintainable mess and security hole'.
And how is this untestable, exactly? It's really easy to fake http requests in php-cli from a unit test, for instance. And is there any other way you'd want to test a router than by faking php requests? I somewhat wonder whether the fact that this happens to be PHP biased your judgment.
It's a clean separation of concerns - the router is the only thing that knows about the URL implementation. The rest of the code relies on the router to do that. That's a good practice to me.
What would you would consider "best practice" in place of a router?
I confess that I stopped digging further after I saw files being included in methods, absolutely one of the biggest sins in PHP:
https://github.com/anandkunal/ToroPHP/blob/master/examples/b...
https://github.com/anandkunal/ToroPHP/blob/master/examples/b...
I'm fine with the idea of a router simply being a router; I really take exception to yet more PHP examples illustrating bad coding habits being on the web though.
include("views/articles.php");
to
return view ('articles');
would be better...
The actual library code is just the 91 line file in the root directory. I don't understand why people are being so negative about 91 lines of code. It's very simple and most of it is quite well written.
How is this project 'misguided', just from face value?
Right - and examples are meant to illustrate how to best use a library.
How you write your handlers and display your views is completely up to you. Why should a simple router dictate how you write the rest of your code?
I agree that it could negatively influence code that more novice programmers might write with it. They should replace the code inside the page handlers with a comment indicating that the user should render output there.
There's always a benefit to not copying and pasting code, which is what writing an include in each of your handlers boils down to.
It boils down to sloppy thinking; your objects should be well defined enough that you're not having to include extra pieces of code in the middle of execution.
You could argue code reuse - but why isn't that code either part of the object in the first place, or an object in its own right?
If you know some examples of elegant code written with includes scattered throughout it, conditionally or otherwise, I'd love to see it.
I will make exceptions for some parts of page rendering; it can sometimes be a good idea to put page elements into their own files and pull together when necessary. But for including code? No. Just no.
Edit: I took exception in this case, as it's non-obvious what happens once the view file has been included, not to mention the lack of a base example handler class implies and encourages people to write code like this.