SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    What is good entire site structure?

    Hi everyone,

    I've done a few searches on the web for "good PHP coding structure", and the like, and I often get to things which are really just style guides for PHP, for example how many spaces to put in front of a bracket, when to use double quotes, etc.

    That's not what I'm looking for. I am looking for information about what is good entire site structure.

    For instance, I am redesigning my already database-driven, PHP site and I want to do it in such a way as it is modular, easily-updated, and it is easy to make code changes.

    Now, you may be thinking 'a template engine'. Well, that doesn't teach somebody to code better. [in fact, I read Vincent's rant the other day and pretty much agreed with it - PHP is an effective template engine already]

    How do you guys go about planning a site, and then implementing it. How do you divide things into functions, classes, methods, etc so it is easy to modify the behaviour of your site in the future?

    How do you separate the template from the code, such that it's easy to modify the appearance/language of the site by editing one file?

    At the moment, I'm calling all files related to appearance .tpl.php, and all files related to code .inc.php. I am setting variables using $template['variable'] in order to pass these to the 'templates'. Is that a good idea, or would classes be better than arrays?

    Any good resources on entire site structure and design? Anyone got any tips?

    Perhaps I'm looking in the wrong place. What should I be looking for?
    Last edited by mmj; Oct 2, 2002 at 22:30.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmj I'm in a similar situation like yours.

    What in my mind is working are the concepts expressed here:

    http://www.martinfowler.com/isa/

    in particular I'm enjoying the front controller:

    http://www.martinfowler.com/isa/frontController.html

    I'm going to use apache mod_rewrite to rewrite the url in the form of /article/888, etc
    All these urls are redirect to a main page called main.php
    Example:
    /article/888 -->> main.php?command=article&id=888

    The main page could like something like:
    PHP Code:
    <?php
    // HttpRequest is class that interfaces http variables
    $controller =& new FrontController();
    $controller->doGet( new HttpRequest );
    ?>
    the aim of the front controller is to handle the http request and then launch the command based on parameter "command".
    I will have many file containing classes as
    class.front_controller.php
    class.article_command.php
    class.article.php
    class.article_finder.php

    For the template issue I was thinking to have many template files:
    template.main.php
    This file will contain the main common html structure of a page

    template.article.php
    This file will contain the html structure for showing an article

    template.main.php
    could look like:
    PHP Code:
    <html>
    <head><title><?= $Helper->getPageTitle() ?></title></head>
    <body>
    //header
    <?php include_once 'template'.$Helper->getTemplateName().'.php'?>
    //footer
    </body>
    </html>
    PHP Code:
    template.article.php
    //html stuff
    <?= $Helper->getArticleTitle(); ?>
    //etc
    <?= $Helper->getArticleContent(); ?>
    As you can see there is an Helper object that is passed to the page.
    There is a helper base class which contain some common stuffs and there is an helper class for article,review,etc that is derived from the base one.

    The article stored in the database will read doing something like:

    $article = ArticleFinder::find( $lang, $id );
    $article->getTitle(), etc

    The template.main.php is launched from the ArticleCommand class.
    The ArticleCommand class is launched from the front controller based on the parameter "command".
    This class will create a specific helper object to be passed to the template.main.php file.

    This is a brief and confused explanation of my thoughts by the reading of the link I provided.
    These are only ideas on the paper and are still in progress or in regress.

    I was thought it's interesting to show them here.



    pippo


    p.s.
    Pardon for the confusing of the post,
    I'm trying to correct some missing tags and grammar errors....
    Last edited by pippo; Oct 3, 2002 at 00:39.
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the post pippo!

    I will look into that link you posted too.

    At the moment, I'm working with something like this:

    index.php - an example of one of the pages on the site.
    Code:
    <?php 
    
    include('resources/config.inc.php');
    
    $template['pagetype'] = 'index';
    
    $row = getarticle();
    
    $template['articletitle'] = $row['title'];
    $template['articlename'] = $row['name']; 
    $template['articlebody'] = $row['body'];
    $template['articleid'] = $row['id'];
    $template['articlecopyright'] = $row['copyright'];
    
    $template['titlecrumbs'] = "";
    $template['panelimage'] = "$imagefolder/cat_index.gif";
    $template['panelimagealt'] = "Main index";
    $template['panelcaption'] = strtoupper(gmdate("F d, Y"));
    
    $template['middlead'] = middlead();
    $template['topad'] = topad();
    
    $template['subnavigationitems'] = categories() . featuredarticles(8) . bestarticles(8);
    $template['relatedpanelitems'] = signupbox() . insertspecials() . buttonad('index') . insertlinks();
    
    $template['contentarea'] = runtemplate("contentarea-index.tpl.php");
    
    echo runtemplate("main.tpl.php");
    
    ?>
    the runtemplate() runs the template, but instead of echoing it to the screen, it returns the output as a string. That means I can have nested templates in any way I want, and they are all to be called by my code, rather than the templates.

    For example, I have set the template variable contentarea to use the output from another template "contentarea-index.tpl.php". I have included templates in other places. For example, the signupbox() function runs a template called "signupbox.tpl.php". It is defined elsewhere!

    I can modify the runtemplate() function at a later date to completely change the type of templates used, without touching my code! At the moment, the templates are in PHP format and they have access to the $template array.

    Sample template - this is just a simple example, not an actual template.
    Code:
    <div class="something">
    
     <h3><?php echo $template['something.heading']?></h3>
    
     <?php echo $template['something.body']?>
    
    </div>
    I can easily add various different templates later as needed, for example, if I wanted a WAP interface.

    My question is: do you like this? Do you approve of it?

    One of my concerns is that $template is an array, it doesn't have a hierarchical structure like XML, or in your case, classes that are inherited from classes above it.

    I could do something like $template['main']['signupbox']['heading'] though. Arrays within arrays.
    Last edited by mmj; Oct 3, 2002 at 01:32.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  4. #4
    ********* Celica Lover Coomer's Avatar
    Join Date
    Apr 2002
    Location
    Not worth the drive
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I simply use one file named index.php, and this file contains the basic code used for the site and the site's design. I simply call pages like this:

    http://www.6gc.net/index.php/action/how-to_articles
    http://www.6gc.net/index.php/action/...rofile/itu/141

    Here's the code that I use to retrieve the variables from the url:

    PHP Code:
    <?php
    $action 
    $_GET['action'];
    if(!
    $action){
        
    $action $_POST['action'];
        if(!
    $action){
            
    $action "main";
        }
    }
    $url_array explode("/"$_SERVER['PATH_INFO']);
    $x count($url_array) - 1;
    while(
    $x 0){
        if(
    $x%== 0){
            
    $prev_value $url_array[$x];
        } else {
            ${
    $url_array[$x]} = $prev_value;
        }
        
    $x $x 1;
    }
    if(!isset(
    $action) or $action == "") {
        
    $action "main";
    }
    ?>
    I simply include the correct file from an includes directory on the server, based on the $action variable. Hope this is kind of what you want.
    + Celica =
    6G Celicas :: My '94-99 Toyota Celica resource

  5. #5
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi mmj,
    thank you for sharing the way you did it.
    My basic idea, that's what I get reading that link, was to have a template file because I liked to desing the page with some WYGIWYS ( sic, for eventual errors ).
    My aim was to try to reduce, or better to separate, the presentation layer ( I think It's called so ).
    The template page has an object, in this case an helper.
    I see that you use an array, which differ from the object I used on the "form only".
    The concept, array or class, is to have an helper that will give us the info we need to display.
    So I think that you did it fine.
    I think that the book I mentioned call this as "TEMPLATE VIEW".
    http://www.martinfowler.com/isa/serverPage.html

    The problem could arise when I have to put some conditional logic inside the code.
    For example when I have to print a link in bold if it's the current page and underlined if it's not.
    I can't define with php some customs tags, such as jsp.
    By the way I do not wanted to put some html layout code on the helper.
    For example I could have:
    PHP Code:
    class Helper
    {
       function 
    getHeaderLink$name )
       {
           
    // if current page
              
    return '<b>etcetc'
           
    // else
              
    return '<a href=etc etc';
       }

    But doing that I'm embedding the html code into the helper.

    With jsp it could be possible to define some custom tags,
    with php I have to put something like:

    PHP Code:
    <?php
    if ($Helper->isCurrentPage'article' ) )
    {
    ?>
    <b>article</b>
    <?php}
    else
    {
    ?>
    <a href=....>article</a>
    <?php}
    ?>
    or on similar fashions.

    My web site is build entirely from me,
    so I have not the interface problem with the designer.

    One thing that I like from your code is this:
    PHP Code:
    $template['subnavigationitems'] = categories() . featuredarticles(8) . bestarticles(8);
    $template['relatedpanelitems'] = signupbox() . insertspecials() . buttonad('index') . insertlinks(); 
    Doing that you are build some dynamic composition of informations.

    One thing that I didn't like with my previous web site was to have html inside functions.
    The functions were stored in different files.
    Using the template view it seemed to me that my life could be less complicated.

    This is the first time I'm trying to build a web site that way.
    In my life I did only one web site with some "canonicals methods" such as a header() and footer() function to print the header and the footer and some pages to display articles, reviews, etc.
    I had only page to display the article, the informations was read from the database.

    I was trying to implement some ideas that I get from that very nice upcoming book.
    I will buy a copy even if the book is almost totally available online, just a little tribute to the author and a nice bed read.

    The author was speaking about another method which is called "TRANSFORM VIEW", but that was too complicate for my actual skills.
    http://www.martinfowler.com/isa/transformView.html
    That kind of view uses xml/xslt.
    The interesting thing that attracted me was that using that kind of view ( transform view ) you will be able to write the "domain logic" in php/java/etc without touching the "presentation layer".
    But I'm too illiterate about that and I too much things to study.

    One thing that I like from the front controller is the that it receives as parameter an object called request.
    My aim creating the class HttpRequest was to have what in Java is HttpServletRequest.
    So from my application code I will not access something like
    $_SERVER[ 'REQUEST_URI' ];
    but something like
    request->requestUri();
    with the latest php revolutions about the super globals name changes....it's not bad
    And as Vincent said globals are evils, so maybe, I say maybe, it's better to have a reference to an object.

    As I said I'm in a developing phase and I'm trying to implements the ideas given to me from the link I posted.
    Eh, eh I'm not saying that's the way to do things,
    but I'm trying to "refactoring" ( sic, bad term use ) my application.

    If I can make an humble obversation to your implementation, I've not the experience to make obversations as I said it's humbled , is that I do not like to have properties inside an array, because "it's something undocumentable".
    I hope you understand what I mean.

    Another humble suggestions is to not use something like:

    $row = getarticle();
    $template['articletitle'] = $row['title'];

    but instead
    $article = getArticle( $id );
    and then
    $template['articletitle'] = $article->getTitle();

    so my suggestion is to use a class to encapsulate the article data.
    I would prefer to use classes and methods instead of array and properties, but that's me .

    Sorry,
    I have spoken too much, mostly wrong , but what you started is a real interesting thread for me and other experiences such as the Coomer one are very interestings.



    pippo
    Last edited by pippo; Oct 3, 2002 at 23:10.
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  6. #6
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestions. The getarticle() is one of the functions I use for accessing the database. It basically just returns the result of a mysql_fetch_array() as an associative array.

    However, I see what you're saying about arrays. I suppose that the $template array itself could be transformed into an object.

    But then it'd be harder for me to add new template vars.

    Anyway, my jokes site is being completely redesigned page by page. Sofar I've done the index page, individual joke pages and the search page.

    It uses the basic structure I put earlier. Functions for accessing the database are in functions.php, functions for generating the left panel and the right panel items are in their own files, etc.

    It is easy for me to add new versions of the same thing now. For instance, the categories.php and search.php execute almost entirely the same code, through using the same functions. They use the same templates, yet if I wanted to edit one it isn't too hard.

    If I want to add wap support or support for some type of web service I'd just create another template and another file for calling the page, and I'd be able to re-use most of the code! I couldn't do that with the old design.

    Anyway, your suggestion is well-received and as I am a beginner, I appreciate all suggestions and look forward to hearing more.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff


Bookmarks

Posting Permissions

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