Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this how to use an MVC pattern in PHP OO

I am trying to build a nice PHP framework for personal use. I realize there are many existing but this is a great learning experience that covers a vast majority of different challenges and really teaches me a lot, as well as having a finished product when i'm done that I can hopefully use to develop other projects from and since I am building it, there should be no learning curve on how to use it.

Some of the basic goals,
- Use PHP Object Oriented instead of procedural.
- Use an MVC or something similar to learn more about this style.
- Be lightweight and fast/good performance

Here is my planned site structure, excluding some other folders for javascript, images, css, some helper functions/files, etc.

///////////// Site structure /////////////

site.com/
        /index.php
site.com/library/
                /Config.class.php
                /Photos.class.php
                /Mail.class.php
                /Filter.class.php
                /QRcodes.class.php
                /Router.class.php
                /Database.class.php
                /Templates.class.php
                /etc, etc,etc......
site.com/modules/
                /account/
                        /model
                        /views
                        /controllers
                /users/
                        /model
                        /views
                        /controllers
                /messages/
                        /model
                        /views
                        /controllers
                /API/
                        /model
                        /views
                        /controllers
                /forums/
                        /model
                        /views
                        /controllers
                /blogs/
                        /model
                        /views
                        /controllers
               /etc, etc, etc, etc.............
                        /model
                        /views
                        /controllers

I have decided to route all Request through a single point of entry, index.php
I will build a Router class/object that will match the URI against a map of possible destinations using regular expressions. Here is a snippet of what I have for now for this part...

<?php
//get url from URL
$uri = isset($_GET['uri']) ? $_GET['uri'] : null;

$uri_route_map = array( 
    //users/account like http://mysite.com/users/324 (any digit)
    'users/friends/page-(?<page_number>\d+)' => 'modules/users/friends/page-$1',
    'users/friends/edit/page-(?<page_number>\d+)' => 'modules/users/friends/edit/page-$1',
    'users/friends/edit' => 'modules/users/friends/edit',
    'users/friends/' => 'modules/users/friends/',
    'users/online' => 'modules/users/online/' ,
    'users/online/page-(?<page_number>\d+)' => 'modules/users/online/page-$1',
    'users/create' => 'modules/users/create',
    'users/settings' => 'modules/users/settings',
    'users/logout(?<page_number>\d+)' => 'modules/users/logout',
    'users/login' => 'modules/users/login',
    'users/home' => 'modules/users/home',

    //forums
    'forums/' => 'modules/forums/index',
    'forums/viewthread/(?<id_number>\d+)' => 'modules/forums/viewthread/$1',
    'forums/viewforum/(?<id_number>\d+)' => 'modules/forums/viewforum/$1',
    'forums/viewthread/(?<id_number>\d+)/page-(?<page_number>\d+)' => 'modules/forums/viewthread/$1/page-$2',
    'forums/viewforum/(?<id_number>\d+)/page-(?<page_number>\d+)' => 'modules/forums/viewforum/$1/page-$2',

    // TESTING new method to define class and page better!
    'users/home' => array('PAGE CLASS NAME', 'ACTION NAME')

    //blog routes coming soon
    //mail message routes coming soon
    //various other routes coming soon
);

//////////////////////////////////
class Router
{
    public function __construct()
    {
    }

    public function get_route($uri, array $uri_routes)
    {
        foreach ($uri_routes as $rUri => $rRoute) {
            if (preg_match("#^{$rUri}$#Ui", $uri, $uri_digits)) {
                //if page number and ID number in uri then set it locally
                $page_number = (isset($uri_digits['page_number']) ? $uri_digits['page_number'] : null);
                $id_number = (isset($uri_digits['id_number']) ? $uri_digits['id_number'] : null);
                echo '<hr> $page_number = ' . $page_number . '<BR><hr> $id_number = ' . $id_number;
                $uri = preg_replace("#^{$rUri}$#Ui", $rRoute, $uri);
                echo '<BR><BR>Match found: ' . $uri_routes . '<BR><BR>';
                break;
            }
        }
        $uri = explode('/', $uri);
    }
}

$uri = new Router();
$uri = $uri->get_routes($_GET['uri'], $uri_route_map);

?>



PLEASE NOTE

THE CODE ABOVE IS ALL TEST CODE AND WILL BE CHANGED, IT IS JUST THE CONCEPT

So as you can see I am planning to have index.php get the URI, check it against valid paths, if one is found, it will include or build a header section, then will build the content section, then finally the footer section of the page.

If you were to access for example... www.test.com/blogs/userid-32423/page-23

The the page would...

  • build header()
  • create object blogs... $blogs = new Blogs;
  • call $blogs->viewbyID($userID,$paging); //$userID would be 32423 and $paging would be 23 from the URI
  • build footer section

Now based on my folder structure. I believe that the blogs class file in our above example would be considered the CONTROLLER. If I am correct so far, then this blogs class which is calling blogs->viewbyID(ID,PAGE) the viewbyID method would set up some code, query the database and set up some variables for the page and then it could include a blogs template file. This blogs template file could be considered the VIEWS.

Now I might have this whole concept wrong, and that is why I posted so much code and text to try and explain my outlook on it, please give me thoughts, suggestions, tell me where im completely wrong, and where I might be on the right track, I will greatly appreciate any constructive critism or thoughts. If I am right in my above usage of the View, Controller portion of the MVC pattern, then what part of my code would be considered the Modal? This is somewhat confusing to me for some reason.

Bonus question... What about form post, where should I process these at? In my example I am focusing on the blog module, so lets say POST for adding new blog entry and POST for editing blog entry, where should these be processed (modal, view, controller)?

like image 392
JasonDavis Avatar asked Feb 03 '11 23:02

JasonDavis


People also ask

Can you use PHP in MVC?

Traditional PHP applications that follow application design best practices can be ported to MVC frameworks with minimal modifications.

Why do we use MVC in PHP?

MVC allows you to separate your business logic from your presentation layer. This "Separation of Concerns" allows you to quickly find and edit portions of your code. It also enables easy reuse of your UI components across your system.

Does MVC use OOP?

MVC is a software design pattern built around the interconnection of three main component types: Model, View, and Controller, often with a strong focus on Object-Oriented Programming (OOP) software paradigms. MVC is a framework for building web applications using an MVC Design.

What is the MVC pattern used for?

Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.


1 Answers

The controller's job is to examine the user's input and determine what is being requested. Once that's determined, the model(s) is invoked. The controller then takes the model's payload and gives it to the view.

Basically, the model is a model of the business. Want to add a blog post? Then the Blog model will have a ->add or ->save method (which is called by the controller. The blog controller may also have an add method, but it is not for talking to the database. It's for examining the input and then calling the model to do the actual saving). Model methods don't always interact with a database but they usually do.

As far as add/edit, they almost always share the same view and can share the same controller methods if doable.

Just remember that the controller is the entry point for all your clients. Every URL your application handles should map to a controller method. The controller then tells the model what to do, passing the user input to it.

like image 56
webbiedave Avatar answered Sep 19 '22 05:09

webbiedave