php - Building a URL rules map for MVC Framework -
i have built own php mvc framework after following tutorials online. have working using entry script in .htaccess
follows:
rewriteengine on rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.*)$ index.php?rt=$1 [l,qsa]
i have router class translates url , divides in action / controller segments:
$route = (empty($_get['rt'])) ? '' : $_get['rt']; if (empty($route)) { $route = 'index'; } else { /*** parts of route ***/ $parts = explode('/', $route); $this->controller = $parts[0]; if(isset( $parts[1])) { $this->action = $parts[1]; } }
what want take 1 step further , define url rewriting rules work in addition automatic routing. want able this:
$rules = array( 'directory/listing/<id>' => 'listing/index', 'directory/<category>/<location>' => 'directory/view', );
in above, array key entered url - parts in angle brackets dynamic variables (like variables). array value request needs routed (controller/action). above 2 rules have following 2 actions:
public function actionindex($id) { } public function actionview($category, $location) { }
so url needs checked against array keys first, if matches 1 of keys needs use array value controller/action pair.
anybody have ideas how go this?
i'm not sure of exact code need implement in environment, can mention how magento's url rewrites function. urls structured <controller>/<action>/<arguments>
. take 1 step further , define key/value pairs url parameters ../id/20/meta/25
turns array so:
array( 'id' => 20, 'meta' => 25 )
you download source , check out. may head in right direction:
Comments
Post a Comment