Repeated controller in url

Hi.

I am using the book PHP & MySQL: Novice to Ninja.

I’m having a problem with repeated controllers in the URL. Here’s what’s happening.

I have table in my website called “pages” which contains the text for a number of miscellaneous pages: privacy policy, about us, etc. As these pages are all in the same table, I have created a single action called “display” to which I pass a page_id. I have a controller, “Page” which contains this action.

The links to these pages are in the footer of “layout.html.php”, a master page, so they are on every page. The links are hard-coded like so:

<li><a href="page/display?id=84">About Us</a></li>
<li><a href="page/display?id=134">Find Us</a></li>

When the site is first loaded, if I hover over any of these links, they show:
“www.domain.local/page/display?id=84”, etc.

That’s exactly what I would expect: one controller (“page”). And the url will say:
http://www.domain.local/page/display?id=134” or such.

BUT, if I click on one of the links, the page is displayed properly with a url like above, but then all the other links (when hovering over them) look like this:

“www.domain.local/page/page/display?id=84”

A doubled, controller.

I also have other links around the master page which involve completely different controllers, say “Service” with links like:

“www.domain.local/service/display?id=123”

If I click one of these, just after the site is first loaded, the service page will load properly with a url like so:
http://www.domain.local/service/display?id=33” (one controller)
but then all other links (when hovered) will look like this:

“www.domain.local/service/service/display?id=456”
or
“www.domain.local/service/page/display?id=84”

Basically, if I click a link, the controller that link routes through appends itself to “www.domain.local” so the routes of other links are added AFTER this controller, hence a repeated controller, or two different controllers.

Interestingly, if I click on the link for the Home page, which has a route of an empty string, the other links go back to displaying a single controller when hovered.

I have xDebugged and cannot find where this is happening. xDebug reports the route correctly as just service/display or page/display. The problem seems to be with the url BEFORE that.

Anyone had any experience of this? Any ideas WHERE to look for the problem?

Cheers.

This could be because the links you have are relative to where you are. So if you are at www.domain.local/page/ and hit the link page/display?id=84 it will take you to www.domain.local/page/page/display?id=84. You can avoid that by using absolute paths for the links, which take you back to the root of the application first.

Yes. This is exactly my thought. However, the routing and the site folder structure are completely different. When I am linking to page/display?id=1 for example, the folder “page” doesn’t exist and the page “display” doesn’t exist. It’s all code managed in Controllers which send output to templates. An absolute url in this context?

The site uses a single entry point system. Precisely because the page doesn’t exist, url rewriting sends the request to index.php which contains an entrypoint method which calls some routing which sends the request to the display method of the page controller.

Yes. I don’t know how the framework in this book works, but with CodeIgniter, I have a constant for my base url, and then I use ‘controller/method/id=…’ for the rest of the link. It does not refer to the folder structure at all.

Right, that sounds like what I need. Can you elaborate? WHere in your project is the “constant” defined and how is it used in code (as a variable before the route?)

Again, I don’t know how this framework is structured. In CodeIgniter, it is set as a function in application/config/confi.php with a trailing / and my links are like <?php echo base_url(); ?>controller/method . In the small php framework that I built myself for practice, I have a file app/init.php where I set my BASE_URL constant without a trailing /, and my links are like <?php echo BASE_URL; ?>/controller/method. The base url is just the root of the application.

I have had problems in the past with URLs, paths and login details which are hard-coded for the localhost and different when online.

The solution I adopted was to test the platform and set a Boolean constant:

<?php
declared('LOCALHOST') || declare('LOCALHOST',  'localhost'===$_SERVER['SERVER_NAME'] );


// usage
if(LOCALHOST) : 
  // do this
else : 
 // do that
endif ;

The script is well worth the additional effort because all files work as expected locally and online.

1 Like

Instead of

<li><a href="page/display?id=84">About Us</a></li>
<li><a href="page/display?id=134">Find Us</a></li>

You should use

<li><a href="/page/display?id=84">About Us</a></li>
<li><a href="/page/display?id=134">Find Us</a></li>

Constants for paths are fine, but I find it’s a lot easier to use the same path structure on all hosts instead of changing them per host for no particular reason.

4 Likes

Hey, ScallioXTX.

Yes, I thought of doing that but didn’t as my routes class looks for the controller/action structure WITHOUT the leading forward slash and I guessed if I did that it would mess up the routes completely but, hey, it didn’t mess anything up. It just worked. Should have just tried it. :roll_eyes:

Thank you very much for your response, you’re a star.

Mike

Never use it without the leading slash (unless there is rock solid reason for that), or else it will just concatenate the link to the current browser url - whatever it may be.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.