views:

70

answers:

2

I have a website that use this style : /index.php?page=45&info=whatever&anotherparam=2

I plan to have pretty url to transform the previous url to : /profile/whatever/2

I know I have to use .htAccess and to redirect everything to index.php. That's fine.

My problem is more in the index.php (Front Controller). How can I build back the $_GET["info"] and $_GET["anotherparam"] to be able to continue to use all the existing code that use $_GET[...] in their page?

Do I have to build back the GET in the header with some code or do I have to get rid of all $_GET[...] on every pages by creating my own array that will parse ever / to and assign something like : $myParam["info"] = "whatever" and than in the page use $myParam[] instead of $_GET[] ?

I would like not to modify all those pages that use $_GET[]

Edit:

My .htAccess look like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ index.php [NC,L]

Everything that doesn't exist go to index.php. Since I already use this structure : /index.php?page=45&info=whatever&anotherparam=2 nothing is broken. But now I will use /profile/whatever/2 and in the switch case I can determine what page to include(..) but the problem is with all GET parameters. How do I build them to have access from all page with $_GET[]?

+2  A: 
$path = ... // wherever you get the path $_SERVER[...], etc.
            // eg: /profile/wathever

$segments = split ($path);

$segments_name = Array ('page', 'info', 'anotherparam');
for($i=0;$i  < count ($segments); $i++) {
  $_GET[$segments_name[$i]] = $segments[$i];
}

with this solution you have to always use the same arguments at the same position

if you don't want that you have two solution: - use path like /page/profile/info/wathever - use router system (for that I recommend you to use a framework instead of doing it all manually)

edit: second solution

$path = ... // wherever you get the path $_SERVER[...], etc.
            // eg: /page/profile/info/wathever
$segments = split ($path);

for($i=0;$i  < count ($segments); $i+=2) {
  $_GET[$segments[$i]] = $segments[$i+1];
}
mathroc
I like the second solution but do not see why I shouldn't do it myself and that I should implement a framework?
Daok
edit it's the router solution that might be complex, give a look at that: http://framework.zend.com/manual/en/zend.controller.router.htmlof course you can do it, it's just more ccomplicated
mathroc
A: 

Use a switch statement instead. Also remember to modify your .htaccess

<?php
switch ($_GET) {
    case "home":
     header('Location: /home/');
        break;
    case "customer":
        header('Location: /customer/');
        break;
    case "profile":
        header('Location: /profile/');
        break;
}
?>
streetparade