I could use a REST from ModRewrite
I won't enthuse about the virtues of web-services and especially REST - many people have written great articles and presentations before me :-)
One problem irked me: delivering different content on the same URL using a framework.
I'm building a system which stores data about a company.
You visit company/20 and you get a page with data about company 20.
You POST to company/20 and the system saves the new data.
This can make your controller a little fugly:
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $this->_saveCompany(); } else { $this->_viewCompany(); }
Man, that code is ugly. And your controller class balloons because it's handling updates as well as views.
I want to be able to route POSTs to one controller, and views to another. But the framework I'm using doesn't (easily) allow that.
Instead, I'm using mod-rewrite to re-route POSTs to /company/20 to the URL /POST/company/20, and running a different controller on that URL.
Here's the code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#This is the additional code to redirect POSTs to a custom URL
RewriteCond %{REQUEST_METHOD} POST
RewriteCond $1 !^POST.*$
RewriteCond $1 !^index.php?.*$
RewriteRule ^(.*)$ POST/$1 [N,DPI]
#This is the regular framework code, to route requests via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Add new comment