3

Greetings all!

Looking for some help with MVC in a PHP context. Currently I am building a small, lightweight MVC framework to help expedite application development at work. It's a long hard separation eliminating inline code - at least with numerous projects looming overhead and the temptation to utilize it ever-present.

I understand most of the basic requirements of MVC, and I've already begun porting some of my existing classes that are in Singleton pattern over as utilities in my new framework (these are mostly basic 'handlers' to perform site services - a class for file uploads, authorization, wrapped PDO database queries, error printing etc.)

What I can't seem to grasp moving forward after reading much documentation is the best approach to instantiating views. In my old, inefficient design I would switch off a $_GET variable to switch ouput from within the home view. Just going off intuition, this seems like an extremely bad way of getting the job done.

I've looking into CodeIgniter, and it would seem that there are predefined functions for loading views within that framework. What is the best approach to such an application design? Would it be a class based 'link factory' that utilizes the same variables to fetch content, select the proper view file, and place it in the page flow? Also, how could the new view be included between the header and footer includes in the root index without using switches? This is the only thing really confusing me - I really hope I have worded myself clearly enough.

Thanks all as ever!

6
  • 4
    Why are you building and MVC framework yourself if you'll be using it not for a hobby project but at work? There are several great PHP MVC frameworks out there that are well thought out, quite mature and well documented. Why re-invent the wheel from scratch? Commented Dec 10, 2009 at 15:10
  • 3
    A great point. Mainly I am interested in doing so to get a better understanding of the approach itself and expand my own versatility in writing code. I'm also interested in extending the program myself for whatever functions it may need, in order to keep it as lightweight as possible. But mainly my interest is academic in nature. I have one very large project that for the first time in awhile I can focus singly upon, and I'd like to build it from the ground up as MVC as possible. Commented Dec 10, 2009 at 15:13
  • 1
    If you have little experience, don't start experimenting while doing "large work projects". Use existing, mature solutions. As mentioned, Codeigniter is a great choice. Commented Dec 10, 2009 at 15:49
  • Codeigniter is a really good choice. Commented Dec 10, 2009 at 15:53
  • I've been attracted to CodeIgniter as it seems like the most transparent framework aimed at developers who are comfortable with php's core functions. Commented Dec 10, 2009 at 16:12

2 Answers 2

2

I highly recommend "PHP Objects, Patterns, and Practice" by Matt Zandstra. A good bit of the book deals with creating MVC frameworks and would be very, very helpful to you.

It covers these patterns (which you can also research elsewhere):

  • Front Controller
  • Application Controller
  • Page Controller
  • Template View
  • View Helper
Sign up to request clarification or add additional context in comments.

Comments

2

While I'd suggest going with an established, flexible framework (like Zend), to answer your question, here are the steps involved as I see them (understand I stopped trying to write this kind of stuff a while ago, this is based on my understanding of the existing frameworks I've used).

  1. Some kind of router parses the request and translates to a controller object with an action (or takes the default) and optional parameters. The router then calls the controller object's function matching the action.
  2. The controller object (usually extended from a generic controller object) process the request and determines what data to pass to the view, as well as what view to use. Most frameworks setup a default view based on the action, but ultimately it's up to the controller to decided what view to use.
  3. The view takes the data and displays it.

That is my very simplified take on the process.

3 Comments

Thanks for the input - I just purchased the book suggested above in PDF - Between that and your explanation I think I am beginning to see what was missing from my understanding.
@TimothyCoetzee I'm not going to disagree with you.
@TimLytle You look well versed in PHP & MVC would you mind having a look at this question stackoverflow.com/questions/43313201/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.