2) http://sitename.com/controllername/method/paramname - invoke same controller having anohter method with the parameters pass tothe method.
we can use ? for getting parameter values else /(Slash) for the parameter values. i m confused with it , for seo we can use / ,if i use /(slash) how can i identify which is method and which is parameter. if the user call method in controller instead of the default method(index). how could i identify the parameter value.
The routing components of today’s frameworks can get fairly sophisticated, but if we boil it down to its simplest, identifying a route can be as simple as a set of if-statements. Here’s an exmaple.
i need to like /{controllername}/{paramname} or /{controllername}/{paramname}/{paramname} how could i get the parametres, can you explain regualr expression you used here.
if i have the code in root folder the above code works else it take my foldername as controller. how do i identify whether the second param is method or querystringvalue
if i have the code in root folder the above code works else it take my foldername as controller. how do i identify whether the second param is method or querystringvalue
It’s going to be tricky to have both /{controllername}/{paramname} and /{controllername}/{methodname}, because unless you define some kind of uniquely identifying format, you won’t be able to distinguish between the two. Consider, for example, the fictional URL /blog/all. Is “all” a method or a param? There’s really no way to tell the difference, so the kind of URL formats you want may not be possible.
$uri=$_SERVER['REQUEST_URI'];
if('/' == $uri):
echo 'No parameters, just a solitary / forward-slash ' ;
endif;
if(strpos($uri, '?' )):
echo 'YES we have a ?';
endif;
echo '<br /><br /><br />';
echo $uri;
echo $new = str_replace('?', '/index/', $uri);
echo '<br /><br />';
// /*
// to see all $_SERVER parameters
echo '<pre>';
$uri = print_r($_SERVER, true);
echo strlen($uri);
echo '<br />';
echo $uri;
echo '</pre>';
die;
// */
Food for thought:
I am only familiar with two MVC PHP Frameworks and both use Apache .htaccess’s “RewriteEngine On” to route absolutely everything to index.php
The index.php file sets paths to the framework core then activates a route.php file.
The route.php file parses $_SERVER[‘REQUEST_URI’] and calls the relevant controller with the associated parameters.