1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP MVC

Discussion in 'Off Topic' started by ChrisR, Sep 6, 2010.

  1. ChrisR

    ChrisR Active Member

    Hello,

    I know a bit of PHP, Well the thing i can never get my head around is MVC and well i want to learn it but after googling a lot i can not find many sites that tell on how to implement it, looking at other frameworks code i am getting very lost and well i mite go crazy ;) .

    The things that i do not get are listed below.

    1) With the hole controller's and all whats the point of having controllers like this.

    PHP:
    class admin extends controller {
    }

    class 
    manage_user extends admin {

    }
    2) With the index file how do you call to the controller and tell what method to load? after looking at some frameworks there like no GET's and i can not seem to break it down to basic code to find out how it works.

    3) with a framework does the main controller need/have to load all the lib's like database, email etc... or is that for the model only?

    PHP:
    class admin extends controller {

    function 
    index() {

    // send email.
    $this->email->send(//your email);
    }

    }
    or is it better to have that in the model.

    4) last one and thats all the issue i sort of having issue with understanding MVC.
    With controller say you want to submit a form how is that done does it still use POST as you see some people when you post something it goes to a URL like mysite.com/login/dologin how can POST be pasted in that hole dologin mothered in the controller?


    Also if you you any sites or tips that will help me out in learning how to use and work with MVC that would be cool.
     
  2. Brandon_R

    Brandon_R Guest

    Your best bet to learning the MVC structure is to get indepth with the zend framework. Read up on it's workings, try making nonsense websites just to test the features of the zend framework. That way you can learn the MVC structure.
     
  3. Shadab

    Shadab Well-Known Member

    1. Is that code snippet from your own implementation? or from some other existing MVC framework?

    2. The index.php acts as a front controller. It catches all the requests to your web application and with the help of a Router object (usually), forwards the request internally to an appropriate controller/action/module. For example:

    If you have associated the url "/article/" with [class: article, method: index], the front controller will automatically call the "index" method of the "article" object with the help of a router, when you visit: yoursite.com/article/

    Take a look at CodeIgniter's application flowchart to see what I mean:

    [​IMG]

    Or symfony's flowchart:

    [​IMG]

    3. The controller is oblivious to your database. Data persistence is the model's responsibility. How the data is stored, is only known to the model: be it in a database or a text file. The controllers and the views shouldn't assume anything about the data storage. Leave that to the Model.

    4. It would be far easier for you to start playing with an existing MVC framework, instead of implementing your own version. There are several MVC frameworks available in PHP, with CodeIgniter, CakePHP, Symfony, Zend being the most popular. You could start with CodeIgniter since it seems to be the easiest framework with minimal learning curve.
    Start coding in it, and then when you get the hang of how everything is done in it, dive into the framework's source code to see how they have implemented it.

    On a sidenote, I started with CI and then moved on to symfony.
     
    chousho and Brandon_R like this.
  4. ChrisR

    ChrisR Active Member

    Thank you for your reply :)

    For one 1 have seen a number of people do this when using frameworks but never got the point of it.
     
  5. Shadab

    Shadab Well-Known Member

    I can't comment much on it without seeing the actual source code of those classes. But I'll take a guess, still. :p

    In the example below, the Admin class' constructor does some generic and repetitive tasks like logging, checking if the user is an admin, etc. And... Manage_User & Manage_Articles extend the Admin class so they'll contain methods focused on managing user and articles respectively. Leaving the generic tasks to the methods they inherit from Admin.
    PHP:
    class Admin extends Controller
    {
        public function 
    __construct()
        {
            
    // Perform generic administration related tasks
            // like: logging, authentication, authorization, etc.
        
    }
    }

    class 
    Manage_User extends Admin
    {
        
    // User specific actions
    }

    class 
    Manage_Articles extends Admin
    {
        
    // Article specific actions
    }
     
    Not really a great example, but I think it'll help get my point across. :)
     

Share This Page