twitterfacebookgoogle plusrss feed

Pages

Tuesday, March 18, 2014

.htaccess for MVC


In MVC routes are normally sent through query string formatted using .htaccess. You are passing a query string to index.php i.e. domain.com/index.php?route=records/dvds/10 transform into domain.com/records/dvds/10. What you have to do is create a new file .htaccess using any supported editor (netbeans will be best, remember notepad of windows do not support a file with no file name) and put following lines into the file, save and place this file in root of the folder where you want to create operational MVC.


RewriteEngine On

RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-l

RewriteRule ^(.*)$ index.php?route=$1


It will successfully tranforme junky query string into nice looking url. The first part of url is considered a controller or class, the second part after front slash "/" is known to be method and the last one is parameters for the method. If "route" in query string is records/dvds/10 (looks like domain.com/records/dvds/10) you have to explode this string on "/" and it will returns an array contains three pockets, 0=>records (controller or class), 1=>dvds (method of records), 2=>10 (parameter of records).

0 comments:

Post a Comment

comment or ask