php - URL Routing using RewriteRule -
i trying create own php mvc framework learning purpose. have following directory structure:
localhost/mvc:
.htaccess index.php application controller model view config/ routes.php error/ error.php inside application/config/routes.php have following code:
$route['default_controller'] = "mycontroller"; now trying achieve when user visits root directory using browser want value of $route['default_controller'] route.php file , load php class inside folder controller matches value .
and if user tries visit application using url this: localhost/mvc/cars, want search class name cars inside controller folder , load it. in case there no class called cars want take user error/error.php
i guess achieve above targets have work .htaccess file in root directory. please tell me code there? if there other way achieve please suggest me.
i have tried use .htaccess codes here, not working me
it sounds , buzzword standpoint, me little confusing because see php's model mvc model already. it's providing api program , deliver content web server apache , database (something mysql). translates code(model) html(view) ... provided that's intend, , you're supplying code user input (control). getting wrapped in terminologies gets little distracting , can lead chaos when bring in collaborate isn't familiar conventions. (this should never used in production environment paying gig.)
i can tell on page referenced guy's .htaccess file needs little work. [l] flag tells mod_rewrite last command process when rule returns true. either need this:
<ifmodule mod_rewrite.c> rewriteengine on rewriterule ^(.*)$ public/$1 [l] </ifmodule> or following... using passthru flag means implying there other things processed prior last rule (eg. might rewrite_base or alias), that's not case .htaccess file since it's little bare. code work similar code above not same. can't used though, , there no need to:
<ifmodule mod_rewrite.c> rewriteengine on rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.*) index.php?url=$1 </ifmodule> the difference in way it's processed. on first .htaccess example you're passing file index.php regardless of whether exists or not. can [accidentally] rewrite path has real file real file never accessed using method. example might have file called site.css can't accessed because it's being redirected index.php.
on second ruleset he's @ least checking see if server doesn't have file or directory name being requested, they're forwarding index.php $_get variable (which seems little pointless).
the way typically write these (since know mod_rewrite loaded in config) to this:
rewriteengine on rewritecond %{http_host} ^mydomain.com rewriterule (.*) http://www.mydomain.com/$1 [r=301,l] rewritecond %{script_filename} !-f rewritecond %{script_filename} !-d rewriterule .* index.php in php code pull $_server['request_uri'] , match against list of uris database. if there's match know it's real page (or @ least record existed @ point in time). if there's not match, explode request_uri , force through database using fulltext search see potentially might match on site.
note: if blindly trust request_uri , query database directly without cleaning run risk of sql injection. not want pwnd.
<?php $intended_path = $_server['request_uri']; if(in_array($intended_path,$uris_from_database)){ //show page. } else { $search_phrase = preg_replace('!/!',' ',$intended_path); $search_phrase = mysqli_real_escape_string($search_phrase); $sql = "select * pages match (title,content) against ('$search_phrase');" } sorry if sounds bit pedantic, i've had experience managing couple of million dollar (scratch) website builds have had hurdles people not sticking standard convention (or @ least agreed upon team consensus).
Comments
Post a Comment