SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 86

Thread: MVC in PHP

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MVC in PHP

    Hiya,

    What other methods are there to design/organise an application, other than MVC?
    Can anybody recommend any books on how to code an MVC framework?

    How are the following forums coded, do any of them use the MVC method as a framework?

    • phpBB
    • myBB
    • SMF
    • vBulletin


    Any help greatly received!

  2. #2
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I know, none of those forum software packages explicitly use an MVC pattern.

    There are many, many ways to design/organize an application, but MVC is nice because it seems to be particularly well-suited to solving the "web problem", because the back-end and front-end are in completely different languages (PHP vs HTML/CSS).

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Am I right in saying "MVC is a framework"?
    Just curious to know, but are there any other methods in use?...or is MVC the only one, or the best?
    How would the forums be coded then?

  4. #4
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i wouldn't recommend looking at phpBB unless you want to learn all the wrong things

  5. #5
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really? It's Bad? How come? - Badly Coded?
    What about myBB?
    Last edited by F.Danials; Nov 11, 2008 at 11:31.

  6. #6
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lussumo Vanilla is about the only PHP forum software I've run into that has actually taken any advantage of design patterns.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    MVC is a Design Pattern not a Framework.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Web implementation of MVC is very loose because the controller acts on the view. However, in a true implementation the view is separate from the controller and updates on its own. In most web applications the view changes are dependent on the controller acting upon it. In a true implementation the view would act upon itself based on information is receives directly from the model not indirectly.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In a true implementation the view would act upon itself based on information is receives directly from the model not indirectly.
    Good evening oddz, that's very very interesting. Do you any examples (preferably in PHP) of such an implementation?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Well this is impossible to achieve with php because the server process ends. However, if you were using flash you could achieve a true MVC online. The thing to remember is that once the html is outputted to the browser everything ends. Therefore, it is impossible for the view to communicate directly with the controller because the script has ended. The process starts over as a cycle, in contrast to a true MVC where the end is only defined by when end user closes the application. In a true MVC page navigation occurs on behalf of the model updating and the view listening for the update event and updating itself. The controller rarely has any part in this transaction between the view and model resulting in displaying the data.

  11. #11
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    Well this is impossible to achieve with php because the server process ends. However, if you were using flash you could achieve a true MVC online. The thing to remember is that once the html is outputted to the browser everything ends. Therefore, it is impossible for the view to communicate directly with the controller because the script has ended. The process starts over as a cycle, in contrast to a true MVC where the end is only defined by when end user closes the application. In a true MVC page navigation occurs on behalf of the model updating and the view listening for the update event and updating itself. The controller rarely has any part in this transaction between the view and model resulting in displaying the data.
    you dont need to use flash, javasctipt and ajax comes in handy here

    you can shift alot of your view processing to the user's browsers as well cutting your server fees

  12. #12
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So if phpBB does use the MVC design pattern, then what does it use?

    Does any of the other forums: myBB SMF vBulletin use the MVC design pattern?
    Also, does Joomla and WordPress?

  13. #13
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    phpBB uses the spaghetti code pattern

    download and see for yourself


    edit: heres the index.php in all its beauty

    PHP Code:
    <?php
    /**
    *
    * @package phpBB3
    * @version $Id: index.php 8638 2008-06-09 17:11:26Z acydburn $
    * @copyright (c) 2005 phpBB Group
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    *
    */

    /**
    */

    /**
    * @ignore
    */
    define('IN_PHPBB'true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH './';
    $phpEx substr(strrchr(__FILE__'.'), 1);
    include(
    $phpbb_root_path 'common.' $phpEx);
    include(
    $phpbb_root_path 'includes/functions_display.' $phpEx);

    // Start session management
    $user->session_begin();
    $auth->acl($user->data);
    $user->setup('viewforum');

    display_forums(''$config['load_moderators']);

    // Set some stats, get posts count from forums data if we... hum... retrieve all forums data
    $total_posts    $config['num_posts'];
    $total_topics    $config['num_topics'];
    $total_users    $config['num_users'];

    $l_total_user_s = ($total_users == 0) ? 'TOTAL_USERS_ZERO' 'TOTAL_USERS_OTHER';
    $l_total_post_s = ($total_posts == 0) ? 'TOTAL_POSTS_ZERO' 'TOTAL_POSTS_OTHER';
    $l_total_topic_s = ($total_topics == 0) ? 'TOTAL_TOPICS_ZERO' 'TOTAL_TOPICS_OTHER';

    // Grab group details for legend display
    if ($auth->acl_gets('a_group''a_groupadd''a_groupdel'))
    {
        
    $sql 'SELECT group_id, group_name, group_colour, group_type
            FROM ' 
    GROUPS_TABLE '
            WHERE group_legend = 1
            ORDER BY group_name ASC'
    ;
    }
    else
    {
        
    $sql 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type
            FROM ' 
    GROUPS_TABLE ' g
            LEFT JOIN ' 
    USER_GROUP_TABLE ' ug
                ON (
                    g.group_id = ug.group_id
                    AND ug.user_id = ' 
    $user->data['user_id'] . '
                    AND ug.user_pending = 0
                )
            WHERE g.group_legend = 1
                AND (g.group_type <> ' 
    GROUP_HIDDEN ' OR ug.user_id = ' $user->data['user_id'] . ')
            ORDER BY g.group_name ASC'
    ;
    }
    $result $db->sql_query($sql);

    $legend '';
    while (
    $row $db->sql_fetchrow($result))
    {
        
    $colour_text = ($row['group_colour']) ? ' style="color:#' $row['group_colour'] . '"' '';

        if (
    $row['group_name'] == 'BOTS')
        {
            
    $legend .= (($legend != '') ? ', ' '') . '<span' $colour_text '>' $user->lang['G_BOTS'] . '</span>';
        }
        else
        {
            
    $legend .= (($legend != '') ? ', ' '') . '<a' $colour_text ' href="' append_sid("{$phpbb_root_path}memberlist.$phpEx"'mode=group&amp;g=' $row['group_id']) . '">' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' $row['group_name']] : $row['group_name']) . '</a>';
        }
    }
    $db->sql_freeresult($result);

    // Generate birthday list if required ...
    $birthday_list '';
    if (
    $config['load_birthdays'] && $config['allow_birthdays'])
    {
        
    $now getdate(time() + $user->timezone $user->dst date('Z'));
        
    $sql 'SELECT user_id, username, user_colour, user_birthday
            FROM ' 
    USERS_TABLE "
            WHERE user_birthday LIKE '" 
    $db->sql_escape(sprintf('%2d-%2d-'$now['mday'], $now['mon'])) . "%'
                AND user_type IN (" 
    USER_NORMAL ', ' USER_FOUNDER ')';
        
    $result $db->sql_query($sql);

        while (
    $row $db->sql_fetchrow($result))
        {
            
    $birthday_list .= (($birthday_list != '') ? ', ' '') . get_username_string('full'$row['user_id'], $row['username'], $row['user_colour']);

            if (
    $age = (int) substr($row['user_birthday'], -4))
            {
                
    $birthday_list .= ' (' . ($now['year'] - $age) . ')';
            }
        }
        
    $db->sql_freeresult($result);
    }

    // Assign index specific vars
    $template->assign_vars(array(
        
    'TOTAL_POSTS'    => sprintf($user->lang[$l_total_post_s], $total_posts),
        
    'TOTAL_TOPICS'    => sprintf($user->lang[$l_total_topic_s], $total_topics),
        
    'TOTAL_USERS'    => sprintf($user->lang[$l_total_user_s], $total_users),
        
    'NEWEST_USER'    => sprintf($user->lang['NEWEST_USER'], get_username_string('full'$config['newest_user_id'], $config['newest_username'], $config['newest_user_colour'])),

        
    'LEGEND'        => $legend,
        
    'BIRTHDAY_LIST'    => $birthday_list,

        
    'FORUM_IMG'                => $user->img('forum_read''NO_NEW_POSTS'),
        
    'FORUM_NEW_IMG'            => $user->img('forum_unread''NEW_POSTS'),
        
    'FORUM_LOCKED_IMG'        => $user->img('forum_read_locked''NO_NEW_POSTS_LOCKED'),
        
    'FORUM_NEW_LOCKED_IMG'    => $user->img('forum_unread_locked''NO_NEW_POSTS_LOCKED'),

        
    'S_LOGIN_ACTION'            => append_sid("{$phpbb_root_path}ucp.$phpEx"'mode=login'),
        
    'S_DISPLAY_BIRTHDAY_LIST'    => ($config['load_birthdays']) ? true false,

        
    'U_MARK_FORUMS'        => ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx"'mark=forums') : '',
        
    'U_MCP'                => ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx"'i=main&amp;mode=front'true$user->session_id) : '')
    );

    // Output page
    page_header($user->lang['INDEX']);

    $template->set_filenames(array(
        
    'body' => 'index_body.html')
    );

    page_footer();

    ?>

  14. #14
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have it installed, along with myBB, SMF, Joomla, & WordPress, hence why I'm asking

    Why spaghetti? - What are the advantages of spaghetti code, over MVC?
    What are the advantages of using MVC?

  15. #15
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  16. #16
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh that sort of spaghetti code, similar to binary or machine code?

    ou say phpBB's code teaches you the wrong code, but they also phpBB also contains templates for the UI.
    Does the the forum need be written using classes, in order to become MVC?

    *I'm aware of what MVC is, but am struggling to see the difference between this and Spaghetti code. Looking at phpBB's code, they look very similar. e.g Template files etc.

  17. #17
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesnt use any patterns, its just all BLOBED in together

    its ugly and it smells and hell they even managed to squeeze in some SQL in there

    theres a reasons phpBB is one of the most hacked php softwares, its rubbish thru and thru written by script kiddies

    its code like this that gives php a bad name


    to answer your question they dont use any patterns, they just write these things and then find themselves with software that has security holes, bugs, inneficient, tightly coupled, hard to understand and extend, hell by the looks of it I wouldn't even know where to start unit testing this crapload, even basic refactoring would help there, sorry but i have no respect for anyone who writes code like this, none


    and before you say it yes I do speak from experience, I have custom written discussion software thats used in larger sites than the biggest phpBB forum you can find



    if you want to learn interesting stuff that will make your brain click (not scream in pain like phpBB code), look at the stuff made by some of the members here such as Konstruct, WACT and lately the Retro Framework
    these members have put in a lot of taught and expertise in and it shows

  18. #18
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent. Cheers
    I'm shocked, phpBB looks good on the outside (interface), but I'll take on board what you said (not on the inside)

    As the saying goes:

    "Never Judge a Book by It's Cover!"
    It does use templates though, so that is a good start isn't it? - The PHP and HTML etc isn't stuffed into one file.

    Can you recommend any books?

  19. #19
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would you rate the myBB and SMF forums as, on a scale of 1-10?
    Last edited by F.Danials; Nov 12, 2008 at 06:01.

  20. #20
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    +1 Konstrukt is impressive stuff.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  21. #21
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    In the end, patterns are merely tools. If you don't understand how to use them then don't. The world will not fall or crumple.

  22. #22
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    In the end, patterns are merely tools. If you don't understand how to use them then don't. The world will not fall or crumple.
    Finally some reason in all that patterns bigotry...

  23. #23
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    In the end, patterns are merely tools. If you don't understand how to use them then don't. The world will not fall or crumple.
    Yes, but I want to learn, as it enables you to organise your coding, and at a later date helps maintain, edit, or delete parts of your website.

    I just though, if any of the forums I have installed on my server used the MVC architecture, then I could use them/those for guidance to create my application.

  24. #24
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does MVC separate PHP code from HTML, or in other words processing from presentation?
    Where can I learn how to code a tailor-made MVC framework?

  25. #25
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Does MVC separate PHP code from HTML, or in other words processing from presentation?
    Where can I learn how to code a tailor-made MVC framework?
    It separates business logic from presentation to a large extent; but since PHP has an embeddable template syntax, it doesn't completely separate PHP from HTML (unless you create a new template language to go with it).

    You can learn by studying MVC frameworks that others have written. Retro is a pretty cool one and simple enough that you should be able to figure it out.

    I'm not sure building a "tailor-made" framework is the right reason to use design patterns. A major goal of patterns is to make code widely reuseable.


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
  •