Hooking the PHP Runtime
Is there any way in php.info (or other config file) for me to indicate a script to be run every single time PHP is invoked?
I'm interested in mapping HTTP requests for a particular page to a Controller class representing that page, similar to how a lot of MVC framework (like CI) work.
For instance, the user clicks a link that should map to /webroot/some/url/widget.php, but rather the /webroot/app/mvc/controllers/WidgetController.php class is what gets invoked instead.
To do this, I figure MVC framework like CI have figured out a way to "hook" the PHP Runtime by running a script that runs and says "oh, the request is for widget开发者_如何学Python.php, but we want to actually run the WidgetController.php object."
Any ideas/thoughts/suggestions/concerns?
Thanks!
I think you might be the wrong trail here. Zend Framework and other Frameworks I know use mod_rewrite or similar techniques to redirect requests, which are then processed by some kind of Front Controller.
This means that your request to example.org/mypage is being redirected by Apache (not PHP) to myfrontcontroller.php, which then decides how to handle the incoming request.
What you're looking for is the php.ini option auto_prepend_file
.
You need a router, and apache mod_rewrite or equivalent. You set up mod_rewrite to send all requests to a single file, such as /index.php. The router then looks at the request URL and determines which controller file to load and which method to call. Pretty much any MVC framework has this functionality built-in, and they all work pretty much the same.
An index.php
in the webroot should be sufficient, the path will be passed as the server variable PATH_INFO
.
See http://us3.php.net/manual/en/ini.core.php#ini.auto-prepend-file
Edit: My answer is simply meant to be an FYI. As others have mentioned, mod_rewrite is the accepted way to solve the problem.
精彩评论