PHP Classes

mod_rewrite as alt to front end controller

Recommend this page to a friend!

      PHP Classes blog  >  4 Reasons Why All PHP...  >  All threads  >  mod_rewrite as alt to front end...  >  (Un) Subscribe thread alerts  
Subject:mod_rewrite as alt to front end...
Summary:example?
Messages:6
Author:Steve Wasiura
Date:2014-02-05 13:20:20
Update:2014-02-11 08:02:36
 

  1. mod_rewrite as alt to front end...   Reply   Report abuse  
Picture of Steve Wasiura Steve Wasiura - 2014-02-05 21:35:46
can you post an example of how use would use a mod_rewrite rule to map a url to a class method? and how you would pass variables in the url?

ie /customer/abc/edit map to Customers->Edit

where abc is the key of the data record you are editing?

i would be interested to see how easy / difficult this is, and to compare performance vs a php framework.

and how would you autoload that class, and still have access to other classes in the framework? ie you can't just point a url to a php file, without that php file needing to include other php files to interact with other methods of the framework, etc.

  2. Re: mod_rewrite as alt to front end...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-02-05 22:40:26 - In reply to message 1 from Steve Wasiura
It seems the explanation was not clear.

Once the Web server matches the request URL to a certain script, the PHP code does not have to redo the matching of URLs to controllers.

That said, once the Web server runs a script for a specific URL pattern, your script just needs to extract the parameters with a single regular expression, not tens of regular expressions for all the controllers you have in your site.

  3. Re: mod_rewrite as alt to front end...   Reply   Report abuse  
Picture of Steve Wasiura Steve Wasiura - 2014-02-06 18:28:02 - In reply to message 2 from Manuel Lemos
Hmmm, which framework(s) run regular expressions against all routes to get parameters, instead of just the matched route? I would imagine they are obsolete by now.

Also, consider the most expensive cycle is a developer's time. Computers are relatively cheap vs humans, therefore if the framework makes it easier for the dev to get things built faster, that is an acceptable tradeoff to me. The ability to have the framework handle routing vs using mod_rewrite and some convoluted includes seems like more trouble than it's worth.

The mod_rewrite seems like a straw man argument. If you would use that to match a route and call a function in a unique php script, that script would need to include base scripts that provide basic functions of the framework. I just don't see the advantage.

  4. Re: mod_rewrite as alt to front end...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-02-06 21:57:00 - In reply to message 3 from Steve Wasiura
I think the problem may still not be clear for you. The matter is that Web servers like Apache already match request URLs to PHP scripts even without needing mod_rewrite.

If you match all request URLs to a given PHP script and then do the routing again in the front controller, you are redoing the work that the Web server already done.

The framework with front controller does not save any development work. The time you take to configure the Web server is the actually less than configuring your framework front controller routes. There is no need for mod_rewrite. It seems you have probably not tried to be able to compare.

  5. Re: mod_rewrite as alt to front end...   Reply   Report abuse  
Picture of Steve Wasiura Steve Wasiura - 2014-02-10 18:34:55 - In reply to message 4 from Manuel Lemos
I don't want to beat a dead horse, but perhaps I'm not understanding what you mean.


> Web servers like Apache already match request URLs to PHP scripts even without needing mod_rewrite
How do you mean? Can you give examples?

a physical file ie mysite.com/customer.php?id=abc&op=edit (direct access to php file)

a virtual url mysite.com/customer/abc/edit (how does apache do this without mod_rewrite?)

in the framework I use, this is handled by index.php which has an include which looks up a match in a routing table, and finds the class / method to call. The time spent in that operation is micro-seconds.

But there are so many php frameworks out there, I do not have enough time to use and compare them all.

are you suggesting that you would need to create unique php scripts for each url you intend to have, to get around the concept of a front-end controller / router?

  6. Re: mod_rewrite as alt to front end...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-02-11 08:02:36 - In reply to message 5 from Steve Wasiura
It seems that this is a little known secret but any Apache can run any file as PHP. It does not have to have the .php extension. You just need to use the directive to any file you want to be run as PHP.

SetHandler application/x-httpd-php

So in your example URL mysite.com/customer/abc/edit , customer is your PHP script that runs the controller for whatever customer actions are supposed to do. Apache does not consider any parameters in front of customer, as long as the next character, if present, is / .