www.webdeveloper.com
Results 1 to 6 of 6

Thread: Modularizing a PHP website

  1. #1
    Join Date
    Aug 2006
    Posts
    255

    Modularizing a PHP website

    Hi There,

    I am about to start building a new PHP/MySQL application. I always use the traditional MVC architecture, however, I am thinking of making a small change with the way I set my application out.

    Usually, each of my VIEW files independently calls the header HTML and footer HTML files.

    I am thinking what I would like to do is have my entire application run through a single control file (eg, index.php). Each page would then be accessed by passing a GET value in the URL. Eg, instead of accessing certain pages directly, like this:

    www.somesite.com/about_us.php
    www.somesite.com/faq.php
    www.somesite.com/news_and_events.php

    My site would instead load each page through index.php like this:

    www.somesite.com/?p=about_us
    www.somesite.com/?p=faq
    www.somesite.com/?p=news_and_events


    What I am trying to decide is whether or not I should have the header and footer include commands in each individual VIEW file, or should I let the index.php file take care of that, meaning each VIEW file will only contain HTML <body> to </body> text?

    Although having the header and footer include commands contained within the main index.php file seems to be cleaner and more modular, what if I want to include some extra information between the <head></head> tags of only some view files? Eg, an extra CSS link, or a Javascript link, etc?

    I'd love to get some insight in to how other people are structuring their applications in this respect.

    Thank you.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    I normally call them in each view, but that is in part because it seems to me the easiest way to work it with the CodeIgniter framework, which is what I normally use. It also allows me to customize some aspects via optional function parameters (e.g. a page-specific javascript or CSS file, page title, etc.). I'm in no way saying that's the best or only way I'd do it, but it works for me.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    240
    Well, I use modular structure for my pages, and let a module loader custom build the site for each request.

    I have tried to build websites in almost every way in my time, and this is far the best way to do it in my opinion.

    But if you wish to keep it simple, and still want to be able to put static content into the main page, but with the ability to insert extra scripts and style sheets in the head section, just create some variables.

    $content
    $head_content
    etc.

    and then echo it out where you want it.
    Then include your pages in the top of the index page. Now you can just put what ever you want into your variables.

  4. #4
    Join Date
    Jan 2010
    Posts
    84
    I've done it various ways, but what I find usually works best for me is to have all the pages called from one central script, as you said.
    My approach tends to be that the string representing what the client sees "acquires" difference bits depending on various conditions. So, I might do something like this:
    PHP Code:
    <?php
    require($_POST['page'] . '.php');
    require(
    'global_headers.php');
    require(
    'navigation.php');
    ?>
    <!DOCTYPE...
    <html>
    <head>
    <title><?php echo $title?></title>
    <?php echo $extraheaders?>
    </head>
    <body><div id="wrapper">
    <?php
    echo $navigation;
    echo 
    $content;
    If(
    $etc) echo $etc;
    while(
    blahblahblah>0)...
    echo 
    $footer;
    ?>
    </div>
    </body>
    </html>
    Last edited by Jerail; 01-25-2010 at 02:51 AM.

  5. #5
    Join Date
    Aug 2006
    Posts
    255
    Thank you for all the responses. Not quite sure yet which way I'll go yet, but this will help with my thinking.

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    Personally I favour the MVC approach. There is nothing to stop you using this and just nesting your views. Typically I have 3 layers to my views, a top layer which contains the main site layout, head section, footer etc, then main views which get plugged into one of the top layer views, then an elements layer which is partial blocks of content which can be injected into other views. This gives you enormous flexibility because you can cache views at any of these 3 levels - ie you can fully cache the whole thing for a static page, or plug in cached static content into higher level views etc.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles