开发者

How to implement the "match" path concept from Rails into PHP?

In particula开发者_如何学JAVAr I have the following in the app's config/routes.rb file:

match "/:id" => "UrlDB#userreq"

And in the UrlDBController is:

def userreq
    @request = UrlDB.find_by_url( (params[:id] )
end

I would like to be able to have the same userreq method respond to all incoming requests regards of what the :id value is - I don't know how to implement this in PHP/Apache.

-daniel


First, Rails is a Ruby framework, and PHP is a language. To get the same functionality in PHP you have to use a framework, as you do with Ruby.

Or you can do it by hands:

The old way of doing this without using any framework, is by using Apache rewrite rules:

RewriteEngine On
RewriteRule /(\d+) userreq.php?id=$1

An other way is to do this manually in PHP, still with some help from Apache.

In Apache config / .htaccess:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]

And in index.php:

$path = $_SERVER['REQUEST_URI'];

if (preg_match('#/(\d+)#', $path, $match)) {
    // execute userreq controller
} else if (preg_match(...)) {
...


PHP gives you freedom about it. You are not forced to use "some concept of routing" (like in Rails), you can use "any concept of routing". You are not even forced to use "controller" nor the MVC model.

Simply get incoming parameters (using $_REQUEST), and decide "routing" by your own.

Or - use any existing framework - most of them define some "routing" template/engine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜