Hi All,

To learn how to use MVC in PHP I followed the tip from many (unfortunaley mostly not very good written) tutorials: make a MVC framework yourself.

I've been trying alot the past day's, grabbed some code from there and there and now I basically got the following structure:

-application
   -controllers
      defaultController.php
      indexController.php
   -models
      mysql.php
      registry.php
      router.php
      template.php
      user.php
   -views
      header.php
      footer.php
      index.php
-css
   css files
-js
   js files
-img
   some img files
config.php (mysql conn)
index.php
init.php
phpinfo.php (for phpinfo())
.htaccess

This structure is a combination of the structures of 2 tutorials and my own idea's.

Now I have several questions which I hope you can answer 🙂:

  1. Where is the actual logic code placed, in the model or in the controller? I read alot of contradictory answers in various tutorials.
    I'll give an example:
    I got a login form on my index. When people submit the form it leads them to the url: www.mysite.com/login
    When they go to that url the router loads the indexController.php

But what now? Should indexController call up user.php(the user object) and tell user.php to log the user in and after getting the succes message from user.php it redirects the user to the homepage for people that are logged in.

OR

indexController logs the user in himself using his own method and once the user is succesfully logged in indexController tells user.php that their now is a 'user' and lets user.php make an object from that user.

Or are both these options wrong?

  1. When I submit for example a login form. Should I always add another var in the url(before submit: mysite.com/shop/, after submit: mysite.com/shop/submit)
    so that my router detects 'Hey they submitted a form'. Or is their a way to let the controller itself detect that the form is submitted by POST(so no need for extra get variable)?

Thanks in advance!

Yours Faithfully

    Now the topic is posted I noticed all my spaces in the dir structure dissappeared. The dirs controllers, models and views reside in the application directory. Files with a dash are the dirs.

    P.S. Is there any way I can edit the post itself? Couldn't find the button 😛

      1. Both: the logic which controls the application flow goes in the controller, the logic which models the "business" functionality goes in the models. If something seems to you to fall in the middle, put it wherever you want and nobody will care. 😉 Personally, the technique I use with the CodeIgniter framework has the login/access-control logic in a controller, which in turn uses the user model to access user data.

      2. There is not necessarily a "correct" answer here. A framework such as CodeIgniter uses the first element in that portion of the URL to specify the controller to be used, and if no subsequent elements are present then it calls the "index" method of that controller. If a second URL element is present, then it instead calls that method in the controller instead of the "index" method. Any subsequent elements are supplied as arguments to the specified method. So your example of "mysite.com/shop/submit" in CodeIgniter would include and instantiate the Shop controller and then call its submit() method. This is a useful approach and avoids putting the same logic in each controller to look for $_GET values or whatever to determine what to do.

        Model
        View
        Controller = MVC

        Even if we do not think of it this way, good programmers
        always use something similar to MVC structure.

        Because what a PHP script for the web needs to have is:
        1. a script, a page.php (control)
        2. some data input for variables (model)
        3. some display, output variables, result (view)

        Say a website:

        /includes
        ..... db_functions.php (model)
        /templates
        ..... index.tpl /(view)
        index.php

        Model handles datastructure
        View handles output & layout
        Controller puts it all together using Models and Views as a toolbox

        MVC Framework is really not too much different from good 'normal' structural web scripting.
        MVC just takes one step further in to make it have structure.
        🙂

          NogDog;10943372 wrote:

          1. Both: the logic which controls the application flow goes in the controller, the logic which models the "business" functionality goes in the models. If something seems to you to fall in the middle, put it wherever you want and nobody will care. 😉 Personally, the technique I use with the CodeIgniter framework has the login/access-control logic in a controller, which in turn uses the user model to access user data.

          1. There is not necessarily a "correct" answer here. A framework such as CodeIgniter uses the first element in that portion of the URL to specify the controller to be used, and if no subsequent elements are present then it calls the "index" method of that controller. If a second URL element is present, then it instead calls that method in the controller instead of the "index" method. Any subsequent elements are supplied as arguments to the specified method. So your example of "mysite.com/shop/submit" in CodeIgniter would include and instantiate the Shop controller and then call its submit() method. This is a useful approach and avoids putting the same logic in each controller to look for $_GET values or whatever to determine what to do.

          Thanks, that clarifies alot. Thank god it isn't all as strict as most tutorials tell. I'm momenterally using some parts of CodeIgniter as a example for my own structure.
          At the end I will have my own framework but I suppose that in the end, it's better to use frameworks like CodeIgniter and Zend because they already got all the functionality you can probably wish?

          halojoy;10943374 wrote:

          Model
          View
          Controller = MVC

          Even if we do not think of it this way, good programmers
          always use something similar to MVC structure.

          Because what a PHP script for the web needs to have is:
          1. a script, a page.php (control)
          2. some data input for variables (model)
          3. some display, output variables, result (view)

          Say a website:
          Model handles datastructure
          View handles output & layout
          Controller puts it all together using Models and Views as a toolbox

          MVC Framework is really not too much different from good 'normal' structural web scripting.
          MVC just takes one step further in to make it have structure.
          🙂

          Your right but in my opinion, the bigger projects become, the less structurized it becomes and that's why I, in the end, wanna make use of the big MVC frameworks like Zend and CodeIgniter.

          Thanks for the clarification guys! 🙂

            Exangelus;10943396 wrote:

            Thanks, that clarifies alot. Thank god it isn't all as strict as most tutorials tell. I'm momenterally using some parts of CodeIgniter as a example for my own structure.
            At the end I will have my own framework but I suppose that in the end, it's better to use frameworks like CodeIgniter and Zend because they already got all the functionality you can probably wish?
            ...

            Building your own will end up teaching you a lot, and it allows you to get it just the way you want. For me, however, neither of those has outweighed for me the time and effort that would entail when, so far, I've been able to use the CI framework as is to do what I need. 🙂

              Ok thanks 🙂 For now I'll keep playing with my own until I can use it without thinking and when I run into new problems I'll post them here.

                Write a Reply...