Is there any standard library to do Rails style URL mapping in PHP? I am not using any framework, all the code is hand-writt开发者_运维问答en. Basically, I am looking for a library that does this
example.com/user/1/active
this should map to a user, with id = 1 and status = 2 (those being the parameters). I should be able to define the map.
There are roughly ten thousand ways to do this in PHP.
I've recently become a fan of klein.php, a lightweight bit of router code with some handy convenience methods. It's not a framework, and doesn't get in the way of you using one if you wanted to.
It's basically little more than "here's a URL pattern, and here's the function to run when the pattern matches."
Frameworks are really built to handle that automatically, but short of using a framework, you would be best off writing your own .htaccess rules (if you are using linux or os x), or try checking out how say, CakePHP handles url rewriting and base off of that. Example:
http://example.com/name/corey
RewriteRule ^(.+)/(.+)$ /$1.php?name=$2 [NC,L]
That would rewrite the above url to /name.php?name=corey
PHP's purpose is not to handle differently formatted URLs. There should be some custom application logic taking care of this.
You've mentioned that you are not using any framework at this moment, so I would like to propose you to include Silex, it's a micro framework based on the components of Symfony 2.
Here's the 'Hello World' example:
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
You've mentioned that you are currently using PHP 5.2. Silex uses namespaces, which are available from PHP 5.3 and so on, so you will have to upgrade your PHP to take this approach.
Go with Symfony framework. http://symfony.com/blog/new-in-symfony-1-2-toward-a-restful-architecture-part-1
Look at this response: https://stackoverflow.com/questions/238125/best-framework-for-php-and-creation-of-restful-based-web-services
精彩评论