Skip to main content

Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #195488
    bearhead
    Participant

    Hi, I am in the process of updating a website with nearly 400 separate page. I have read that using your index.php file as a single point of entry for the site is a good way to vastly improve maintainability on large sites.

    However I’m not very experienced with php, and I’m not quite sure what that means or how to implement it. Does the index.php contain a script that loads in content, based on what url the user is viewing? If so what would such a script look like? I have searched a bit, but I can’t really find any explanations that I could understand.

    Thanks for the clarification.

    #195495
    Anonymous
    Inactive

    I’m not quite sure what that means or how to implement it. Does the index.php contain a script that loads in content, based on what url the user is viewing? If so what would such a script look like?

    Before coding anything, think through what those 400 pages have in common. The point of a single point of entry is not that it is good in itself, but that you are trying to separate content / data from how it is presented. For example, a blog site might have 400 posts, but each post would be essentially the same. They each have a title, a date, an author, and some content.

    An important first question – have you used databases before and are you currently using one?

    #198592
    Taufik Nurrohman
    Participant

    I was stuck thinking that the links would still be pointing to specific files and was like- how the hell is code on index.php going to matter when the user clicks a link and goes to a different page!?

    Use .htaccess to remove the index.php part in URL. Query string will be used as the fake URL path. When you click on a link, you are not going to any different pages. It’s just the same index.php page with different query strings.

    # Rewrite `http://my_website.com/index.php?foo/bar` to `http://my_website.com/foo/bar'
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L,QSA]
    </IfModule>
    

    Here you can use the query string for loading different template to the index.php using PHP include or require.

    Something like this:

    <?php
    
    $q = str_replace('?', "", $_SERVER['QUERY_STRING']);
    
    if (strpos($q, 'path/to/file-1') === 0) {
        include '../template-1.php';
    } elseif (strpos($q, 'path/to/file-2') === 0) {
        include '../template-2.php';
    } else {
        include '404.php';
    }
    

    This barebones should be able to help you to understand: https://github.com/tovic/cms-starter

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.