MVC

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shirkavand
Forum Newbie
Posts: 6
Joined: Sun Sep 28, 2008 11:24 pm

MVC

Post by shirkavand »

Hi,

I am trying to use an Model View Controller Patter in my design, so i want my .php files be stored in one folder called "controls", my .tpl be stored in a folder called "views" and my .php classes that manage the access layer be stored in a folder called "models". So my question are:

1- Do i need to write a "dispatcher" for manage incoming requests? If yes where can i found an example of this in PHP?
2- I am using Smarty so, i can store my tpl's in a sepated folder, but can i do the same with my other .php files(controllers and models)

I am a little bit confused about this issue, i will apreciate any help/idea.

Regards,

Shirkavand
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: MVC

Post by alex.barylski »

Do i need to write a "dispatcher" for manage incoming requests? If yes where can i found an example of this in PHP
Not nessecarily. Some might argue that Apache in this regard, is the dispatcher/router. Your URI about_us.php is consumed by Apache which invokes the appropriate script.

If you wanted to route all requests through index.php, then I suppose you need a dispatcher and possibly a router.
I am using Smarty so, i can store my tpl's in a sepated folder, but can i do the same with my other .php files(controllers and models)
Ummmm sure...why not? Just resolve the paths' properly. This really depends on your interpretation/implementation of MVC me thinks.
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: MVC

Post by The_Anomaly »

MVC is a very common topic in programming. I'd highly recommend you do some serious googling around, as it's covered fully in many different places.

You might (and some will disagree with me on this) want to check out a Framework, like Zend Framework. Maybe not even use it, but the tutorials will introduce you to the implementation of MVC. I didn't really understand it at first, but as I kept learning, it all seemed to click. Go through the ZF wiki, make the quickstart site, and you'll probably really gain some serious understanding.
shirkavand
Forum Newbie
Posts: 6
Joined: Sun Sep 28, 2008 11:24 pm

Re: MVC

Post by shirkavand »

Hi,

Thank you very much for your help. I was googling about MVC in PHP5, i was expecting to find a good example about this pattern were, i found this two examples of how implement it:

http://www.phpit.net/article/simple-mvc-php5/
http://www.onlamp.com/pub/a/php/2005/09 ... intro.html

But i do not know if they are really a good implementation of the MVC, mainly because i do not see that they use a full OO aproach in the first one, and i see a veru complex coding in the second one. Any way, can you take a look and tell me which of them are the best MVC OO implementation, maybe give me a site/book/tutorial recommendation if necessary/possible

Have a nice day,

Cheers
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: MVC

Post by josh »

I'd recommend learning to use a framework before writing one
http://www.framework.zend.com
shirkavand
Forum Newbie
Posts: 6
Joined: Sun Sep 28, 2008 11:24 pm

Re: MVC

Post by shirkavand »

Well the problem of learning to use a framework is that i have to learn how to do every single thing in the way that the framework works/does, that mean a lot of learning time, instead i prefer to create a good MVC architecture from scratch once, and after that use it for all the projects, and its benefits is that i can work using "standard" programming not "zend", "cakephp" etc way.

Beside that i want to understant the MVC internals and how to implement them in PHP5.

Regards
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: MVC

Post by The_Anomaly »

At least the Zend Framework isn't like that. For example, you CAN use Zend_Form to do all your form/vaildation work, or you could just write the classes yourself in this, "Standard PHP" you speak of. The only thing that Zend provides you, and enforces to some extent, is MVC. So, by using Zend, you could still use your own classes and such, but you'd at the same time get used to common patterns of MVC, like Front Controller, or Template View--that Zend has built in.

Once you learn how to use them, then you can get into implementing them yourself. However, I'd recommend you first read some books on the topic, or at least spend a good amount of time googling around. I started with the Zend Framework right off, and without at least some base of MVC knowlege, I could barely understand anything. i.e. That the use of an .htaccess file was to route all requests through the Front Controller, or what that even was, etc.

If you insist on not using a framework, I suggest you pick up some books on the topic. Specifically Patterns of Enterpirse Application Architecture, and PHP 5 Objects, Patterns, and Practice. The former is not only for PHP, but for OOP in general, and the latter really teaches you how to use OOP and patterns in PHP.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: MVC

Post by josh »

I doubt your custom rolled framework would be any more standard then zend or cake,

you can just modify the existing framework to do what you need it to do?

Anyways just saying cuz you asked for a good MVC architecture and zend provides a really simple to use and powerful implementation, complete with 2-step layouts, named layout "partials", etc... Stuff that you seem to be thinking could be implemented overnight.

Most frameworks don't force you to code in a special way at all, in fact if you look at some of the opensource ones you'll see they all have a lot in common, almost a "standard".. if you didn't at least download them and try them out, you'd be contradicting yourself by creating something nonstandard... you wouldn't wanna contradict yourself would you?
( seeing as you don't understand MVC fully yet, if you don't know what a framework is you're statistically highly likely to fail )

All MVC does is says that different types of logic should go in different types of classes, it doesn't force you to write your model code a "cake" or a "zend" way, you still write the same code if you want, heck you could even write procedural code inside a MVC framework while you were still learning it.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: MVC

Post by panic! »

I'd say learn a few frameworks THEN build your own.

I've used codeigniter, zend and cake and learned from all three great frameworks and from here I can cherry-pick aspects I like and I don't like from each.

The reason I decided to make my own is because I wanted something more lightweight, Mine is about 2000 lines of code in all, purely implements an MVC pattern with an activerecord class and 2 DB adapters for MySQL and SQLite. Nothing more, nothing less.
Post Reply