donglou1866 2011-11-14 17:31
浏览 46
已采纳

Zend如何使用mod_rewrite解析前端控制器中的URL?

I can't figure out how the Zend Framework controls routes. I am actually trying to create my own controller/route class, wrote the same code as Zend reccomends for .htaccess to rewrite to an index.php located in the root of the website:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

The line I don't understand is the last one. It doesn't have capturing groups and $ variables. In index.php I can't catch anything in $_GET, which is empty. The only way it would work would be:

RewriteRule ^(.*)$ index.php?uri=$1 [NC,L]

and then parse $_GET['uri'], which for me it would be in the form "controller/action/param1/param1value".

But it would be nice to get around this and not use the 'uri' variable. In a Zend project, this just works. Can someone explain me how Zend parses the URL with this kind of rewrite ?

  • 写回答

1条回答 默认 最新

  • dongzaobei0942 2011-11-14 17:43
    关注

    The last rule forwards all requests where the file doesn't actually exist (see previous rule) to index.php which loads the Zend Framework and handles the request. If you trace through what Zend does with it, you'll find eventually that you get to Controller/Request/Http.php through the Front Controller which uses several of the $_SERVER variables to determine what the request was for.

    I would recommend writing your own index.php and inspecting the elements in $_SERVER and reviewing the code in Controller/Request/Http.php.

        print_r($_SERVER);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算