Search code examples
phpheaderincluderouterclean-urls

Clean-URL and Router: Load new Site -> Use include() or header()?


I have a generell question about how to load a new Site.

For example:

On my website there is a link like this:

<a href="home">Home</a>

"home" is the Parameter which is handled by the router. The router checks in a switch/case-construct this parameter and calling a controller-method. This controller-method calls a model-method which returns maybe little bit to the controller-method. And at the end the controller-method must "open" the home-site (home.php).

But what is the normal and good way to do this? Use include("blabla/home.php"); or use header("blabla/home.php); ?

If you say include() then my question is:

How you fix that problem, that the URL is maybe not up-to-date?
Example: The home-link above is in the profile.php. At the moment you are in the profile.php, you can see in the URL at the end "profile". If you click on that home-link and use include(), then when you see now the home-site, you have still the profile-URL.


Solution

  • Think in terms of HTTP requests and responses. If you request the URL /profile, the response should be a profile page. If you request the URL /home, the response should be some sort of home page.

    header('Location: ...') is an HTTP response mechanism to instruct the client to request a different URL. It goes:

    1. client requests /profile
    2. server responds with header Location: /home
    3. client requests /home
    4. server responds with home page

    include on the other hand is simply a PHP internal mechanism to load the contents of another file. It has nothing to do with HTTP responses as such and is pretty much irrelevant to the discussion.

    You respond with the content that should be visible at a URL. You may redirect clients to a different URL with a Location header. That's all.