Jump to content

Small framework for an 'MVC' + registry pattern


drcphd

Recommended Posts

(I hope I've not posted this in the wrong forum: I'm actually looking for input on my approach, not the coding directly).

 

I want to write my own CMS (with basic features I always need) for my future projects.

 

I've tried two times before but had to stop due to lack of planning.

 

Lately I've been reading about MVC (Model-View-Controller) designs and registry patterns.

I've come up with a solution that I think might work.

 

I have a registry class to store all objects and a module handler which fetches output from the different modules (like a router).

But in this example you can load several modules simultaneously. That means you can assign module-output anywhere on the page (as main content, block content, etc).

The 'main module' (requested by user, think $_GET['mod']) will always be responsible for the main content (the body of the page). But every other module can also be loaded and create output for other parts of the page (like a poll in a block).

 

My approach is best explained by looking at a simplified framework I wrote.

 

http://pastebin.org/2655 - View code

 

Comments? Is this a stupid idea?

Also, if anyone has something to say about the object oriented programming in this example, that would be great. I'm still learning, and it would be nice to know if I've grasped the basics, e.g. how the registry object should be treated.

 

Thanks in advance.

Link to comment
Share on other sites

There aren't really stupid ideas when you're learning something new... Unless what you're doing is counter-productive (which kind of negates what I just said). I you feel you have a sturdy structure in mind, or that you could develop one over time, just go for it. It's a learning process.

If it fails, move on to something else. If you aren't sure of or don't believe in a project you start, don't plan on killing yourself over it. Be flexible.

[insert more motivational clichés here]

 

Your implementation of a registry, I'm not entirely sure of... It looks a lot like a singleton interface, which is probably what you're looking for. That, or just use globals / statics (ie. Database::$current/$instance/$default/etc) for your classes.

It looks like you have the basic grasp of OOP, and all I can say at this point is browse around the PHP documentation for language/syntax info along with docs on libraries. In addition to that, find yourself a well-written or popular (one doesn't always imply the other :) ) library, a CMS, framework, some tutorials, what not, and browse around the source some, see what you find.

Link to comment
Share on other sites

The 'main module' (requested by user, think $_GET['mod']) will always be responsible for the main content (the body of the page). But every other module can also be loaded and create output for other parts of the page (like a poll in a block).

 

Yes, it's one - probably good - approach. Or you could have relationship between URL and modules:

 

site.com/articles (module articles goes into body)

site.com/news (module news goes into body)

Link to comment
Share on other sites

deadimp: thanks for the motivating words :)

 

448191: I thought I was missing the 'view' (for now). Wouldn't the Modhandler and modules be the controller?

 

Mastodont: Yeah, I'm gonna do that: site.com/module/action/parameters

So it's the OOP and whole idea I'm worried about. I don't want it to get dirty. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.