|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I have been trying to get a little templating thing going and I want everything to pass through a single index.php file which then decides what page is wanted and includes them. The scheme is to go to a url like [http://localhost/~donn/blah/] which serves index.php in that dir. That includes 'home.php' home.php has a link to 'use1.php' The problem I am having is that as I click back and forth between home and use1 the url starts growing... I get things like [http://localhost/~donn/blah/index.php/index.php/use1] and it just keeps getting longer. I include my code below, very much shortened. I am sure I am missing some obvious way to control links in this weird situation where every page is *really* index.php but I still need to link from page to page. [code] [index.php] <?php $expl = explode("/",$_SERVER["REQUEST_URI"]); $resource = $expl[count($expl)-1]; if ($resource=="") $resource="home"; if (file_exists($resource.".php") ) { include $resource.".php"; } else { echo "No such file."; } ?> [home.php] <h1>home</h1> <a href="index.php/use1">link to use1</a> [use1.php] <h1>use1</h1> <a href="index.php/home">Back</a> The overall idea is to have clean urls without a bunch of htaccess stuff (which mystifies me). I want to be able to bookmark [http://localhost/~donn/blah/index.php/somepage] and have stuff like [http://localhost/~donn/blah/index.php/gallery:showpic1] Thanks for reading. \d |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
UsDonn Ingle wrote:
> Hi, > I have been trying to get a little templating thing going and I want > everything to pass through a single index.php file which then decides what > page is wanted and includes them. > > The scheme is to go to a url like [http://localhost/~donn/blah/] which > serves index.php in that dir. That includes 'home.php' > > home.php has a link to 'use1.php' > > The problem I am having is that as I click back and forth between home and > use1 the url starts growing... > > I get things like [http://localhost/~donn/blah/index.php/index.php/use1] and > it just keeps getting longer. > > I include my code below, very much shortened. I am sure I am missing some > obvious way to control links in this weird situation where every page is > *really* index.php but I still need to link from page to page. > > [code] > [index.php] > <?php > $expl = explode("/",$_SERVER["REQUEST_URI"]); > $resource = $expl[count($expl)-1]; > if ($resource=="") $resource="home"; > > if (file_exists($resource.".php") ) { > include $resource.".php"; > } else { > echo "No such file."; > } > ?> > > [home.php] > <h1>home</h1> > <a href="index.php/use1">link to use1</a> > > [use1.php] > <h1>use1</h1> > <a href="index.php/home">Back</a> > > The overall idea is to have clean urls without a bunch of htaccess stuff > (which mystifies me). I want to be able to bookmark > [http://localhost/~donn/blah/index.php/somepage] and have stuff like > [http://localhost/~donn/blah/index.php/gallery:showpic1] > > Thanks for reading. > \d > > > Use /index.php instead of index.php maybe... |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Shawn McKenzie wrote:
> Use /index.php instead of index.php maybe... I assume you meant in the <a> tag. I tried that and the URL (when you mouse-over the link) becomes [http://localhost/index.php] which is not anywhere near where the files live. I must say I am rather confused by this situation, but I can fix it by always using absolute urls - which is a bit of a pain. \d |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Donn Ingle wrote:
> Shawn McKenzie wrote: >> Use /index.php instead of index.php maybe... > I assume you meant in the <a> tag. I tried that and the URL (when you > mouse-over the link) becomes [http://localhost/index.php] which is not > anywhere near where the files live. > > I must say I am rather confused by this situation, but I can fix it by > always using absolute urls - which is a bit of a pain. > > \d > Does seem strange. Try: <head> <base href="http://localhost/~donn/blah/index.php" /> </head> Then just use /home, /use1 etc... Might work. -Shawn |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Shawn McKenzie wrote:
> Does seem strange. ÂTry: > <head> > <base href="http://localhost/~donn/blah/index.php" /> > </head> > Then just use /home, /use1 etc... ÂMight work. Right, I'll give it a go. It's not much different from wiring-in a php var naming the base with {$base}/use1 being the form. The thing that would here is if I could get a reliable way to extract the working 'path' (from an http:// pov) from the $_SERVER array somehow. Each of them gives a result tantalizingly close to the base, but they get it wrong in various ways. Well, thanks for your . \d |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
> The thing that would here is if I could get a reliable way to extract
> the working 'path' (from an http:// pov) from the $_SERVER array somehow. > Each of them gives a result tantalizingly close to the base, but they get > it wrong in various ways. You can use $_SERVER['PHP_SELF'] which will give you the path to the script or $_SERVER['REQUEST_URI'] (you will probably have to remove the query string with this), along with either dirname(). -- Richard Heyes Employ me: http://www.phpguru.org/cv |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Tuesday, 18 March 2008 12:02:14 Richard Heyes wrote:
> You can use $_SERVER['PHP_SELF'] which will give you the path to the > script or $_SERVER['REQUEST_URI'] (you will probably have to remove the > query string with this), along with either dirname(). The problem is that the URL does not stay constant, it keeps growing. For example after clicking between home and use1 a few times I see these values, note how index.php repeats: 'REQUEST_URI':/~donn/template-dev/routertest/index.php/index.php/home dirname('REQUEST_URI'):/~donn/template-dev/routertest/index.php/index.php I suppose some kind of htaccess voodoo could here, but I am in no shape to grok those murky waters. A link <a href="index.php/use1"> causes 'index.php/use1' to be appended to the full url [http://localhost/~donn/template-dev/routertest/] and at first, it's fine but after a few clicks, it keeps appending. Amazingly things still work, but the URL is out of control! All I can think to use to enforce correct link behaviour is to employ absolute urls. Thanks for the feedback. \d |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
> A link <a href="index.php/use1"> causes 'index.php/use1' to be appended to the
> full url [http://localhost/~donn/template-dev/routertest/] and at first, it's > fine but after a few clicks, it keeps appending. Amazingly things still work, > but the URL is out of control! Links that don't start with a / are taken to be relative to the current documents path. You (probably) want this: <a href="/~donn/blah/index.php/use1"> The forward slash at the start causes your browser to ignore whatever your current path is, albeit remain on the current domain. -- Richard Heyes Employ me: http://www.phpguru.org/cv |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Richard Heyes wrote:
> The forward slash at the start causes your browser to ignore whatever > your current path is, albeit remain on the current domain. Okay, I think I am getting confused by this ~donn/ virtual path thing. I will try your suggestion it looks good. Thanks. \d |
|
![]() |
| Outils de la discussion | |
|
|