MVC .. again

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

sam4free
Forum Newbie
Posts: 18
Joined: Fri Oct 10, 2008 6:02 pm

MVC .. again

Post by sam4free »

I have been studying MVC and PHP frameworks for a while, but still having one thing not clear.

lets assume we have this address

Code: Select all

http://www.yoursite.com/index.php?modul ... =login&....
In basic .. the controller gets the name of the required module from the request "user", loads it, and executes the event "login", passing the arguments. -it may load the default module in case there was no requested one-

then, it passes the data returned from the module to the view, witch returnes the formated result "for example as HTML"
That was so easy and there is more than one way to implement. but...

in the above example we assumed that the page contained only one dynamic part "that is the part processed by the module"
In real world, pages contains a lot of parts each with its own module. for example the page may contain a "poll" at the sidebar, with a hit counter in the footer.
I read TONs of papers and no one ever got beyond the simple example written above..
:banghead:

My first idea was to store the request variables in a global var and call the main page module witch calls modules to form the parts, each part calls modules for sub-parts and so on "like a tree of calls"
This idea looks good but i think its implementation will be "weak" if you know what i mean.



any ideas??
Thanks
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: MVC .. again

Post by alex.barylski »

Joomla solves this problem nicely -- at the conceptual level -- implementation is a small disaster.

Google component-ized architectures and other CMS as they all address this very design problem differently than the next.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MVC .. again

Post by Eran »

The M in MVC stands for Model not module. Modules have no formal reference in MVC, though often they refer to a group of controllers / views / models.

Repeating display elements such as the poll you mentioned are best encapsulated as different view templates and then composed on demand to create the complete view. A view which composes other views is often called a layout.

Joomla handles this pretty horrifically in my opinion. For good implementation of those patterns I'd advise you to have a look at the major frameworks - such as Zend Framework, CakePHP, Symfony and others.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC .. again

Post by Christopher »

Given your URLs, you are doing the worst thing possible -- combining your Model and Controller. Each View should have one or more Models associated with it. The Controller is just the program flow code. Your Controller can pass the Model(s) to the View or the View can get the Model(s) itself.

Remember that the Controller and View are in the Presentation Layer together. The Model is in the Domain Layer and should generally have no dependencies on any Controllers or Views. Combining Controller and View code can be done if necessary. If you only do one thing when designing your application is should be keeping the Models independent -- that's the most important thing.

Typically in modern controller architectures your "module" would be a class/file and your "event" would be an action/method. Usually "module" is used to describe dividing up an application into multiple directories, each with its own MVC tree, so you might want to clean up your nomenclature to module/controller/action so others will instantly know what you are talking about.
(#10850)
sam4free
Forum Newbie
Posts: 18
Joined: Fri Oct 10, 2008 6:02 pm

Re: MVC .. again

Post by sam4free »

Given your URLs, you are doing the worst thing possible
That's not how my real software works .. i just wanted to simplify the case.
anyway you are right ... there is a lot of things i can do to improve my framework and i'm working on it.
..is should be keeping the Models independent
that's a good point .. thanks


any way i think i'll try to understand the zend framework as i have already read the joomla code and 'i think' joomla is not a good example for solving this kind of problems
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: MVC .. again

Post by alex.barylski »

Joomla handles this pretty horrifically in my opinion. For good implementation of those patterns I'd advise you to have a look at the major frameworks - such as Zend Framework, CakePHP, Symfony and others.
Regardless of implementation details, you can implement components in strict MVC -- so I fail to see how Zend is any better in this regard, or CakePHP for that matter. Zend has a weird view dependency and CakePHP auto-loads models based on convention from what I remember so their all pretty horrific. ;)
any way i think i'll try to understand the zend framework as i have already read the joomla code and 'i think' joomla is not a good example for solving this kind of problems
Both are equally bad IMHO. Zend might have slightly more conrete/defined/clear separations -- but both suffer from over engineering and neither are simple. Don't let anyone tell you Zend is simple. WHat they fail to tell you is that basic MVC examples are always trivial to look at.

Code: Select all

class MyController extends Controller{
  function action()
  {
    $view = new MyView();
    $model = new MyModel();
 
    $results = $model->getList();
    $view->addItems($results);
  }
}
This is not exactly a complete project and thus makes a complete component implemented in Joomla look very complex -- guess what? If you implement a complete system like Joomla and then begin building your own components -- your components (using Zend) will look just as complex. ;)

Cheers,
Alex
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MVC .. again

Post by Eran »

so I fail to see how Zend is any better in this regard, or CakePHP for that matter. Zend has a weird view dependency and CakePHP auto-loads models based on convention from what I remember so their all pretty horrific
How can you even compare Joomla to everything else out there. Joomla is the worst attempt at MVC I've seen to date, obviously by people who have no real understanding of the pattern or are very inexperienced with it.

It's true zend and cakephp have some defaults that are not for everyone (ie viewRenderer etc). Those though can be turned off very easily. On the other hand they have a much better implementation of the MVC pattern. I keep getting the feeling you have very limited expereince with frameworks outside of Joomla from the way you keep mentioning it. How much experience do you have developing with the ZF or cakephp? I really believe that with more experience with it you would understand how much better those are from Joomla.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: MVC .. again

Post by alex.barylski »

How can you even compare Joomla to everything else out there. Joomla is the worst attempt at MVC I've seen to date, obviously by people who have no real understanding of the pattern or are very inexperienced with it.
Quite easy actually. It's MVC in a nut shell -- it's implementation might be iffy as are the dependencies -- but I've seen a lot worse (ie: WordPress).

The framework will invoke your controller -- which is derived from their base controller class. Your controller:action method is invoked by a default router/dispatcher (or custom) and from there everything you effectively echo is injected into the component block of the template.

The component architecture in this regard, is fairly standard. I don't need to fret about anything like setting up basics/boilerplate code -- I just derive a class from JController create a method named whatever and inside that controller I have full control over how or where or when my models and views are used. Joomla has arguably the best component/plugin support when compared to other CMS.

Granted it's not the most elegant solution and there are tons of caveats -- but at the conceptual level it's MVC as I can choose how dependent my views are on my models, etc.

I'm willing to bet Joomla will do model auto-loading (I prefer to instantiate models manually) -- I'm quite certain it does auto-view loading too -- but again I can override that easily and instantiate my own view classes so the separations at this point are as clean and concise or as cluttered and confusing as I want them to be. :P
Those though can be turned off very easily
I don't know about disabling but you can just ignore them in the controller, like I do. I don't call any controller methods and rely on default routing. Joomla is also a more robust framework. Perhaps a little to robust. :P
On the other hand they have a much better implementation of the MVC pattern
Without getting into details -- yes probably. Still to complex and to many dependencies for my tastes so their all puke if you ask me. :P

Anyways, Joomla component architecture is still worth knowing/studying. The fact they separate components from modules and restrict components to a single instance per page is a best practice -- no point in re-inventing the wheel using Zend only to later discover "why" Joomla enforces this.
I keep getting the feeling you have very limited expereince with frameworks outside of Joomla from the way you keep mentioning it. How much experience do you have developing with the ZF or cakephp?
Interesting. Have you searched the history of framework discussions on these boards, or maybe the codeproject? I've been using/studying frameworks for more than 10 years...I think I am probably more familiar with CodeIgnitor/Cake/Zend/Symphony/etc than you think I am.

I mention Joomla only because it's 'component' architecture is done right -- I say this in confidence having implemented and worked with countless CMS over the years -- it's what I specialize in...

I used Zend when it was first released. I played with it over a few months and encountered some issues I didn't like, chewed them over with others on here and ultimately decided to start my own framework. For commercial projects I use Joomla because it has a huge community and clients like to hear that stuff. :P

CakePHP I have only glanced at...enough to know I don't like it with it's management of dependencies/complexities, etc

I think maybe you are midunderstanding what is meant by component architecture (where MVC is somewhat implied) -- at least from the perspective of RAD. A component is implemented using the MVC -- regardless of whether the view is an included template or a class with a distinct API which the controller might instantiate and manipulate. The more concise the interface the happier I am -- but including a template works too and the most important separations are take care of -- explicitly rather than implicitly.

http://book.cakephp.org/view/50/Introduction

Read those first few paragraphs:
In CakePHP, controllers are named after the model they handle, in plural form.
Puke.
Your application's controllers are classes that extend the CakePHP AppController class, which in turn extends a core Controller class.
Deep hierarchy. WHat kind of functionality could possibly be required by every single controller ever invoked -- or is there massive amount of overhead -- I assume more than I care for.

Zend, Cake, Symphony, CodeIgnitor...are not like Joomla at a framework perspective. Joomla throws a lot more at you which is why new developers tend to think Joomla sucks when compared to Zend -- it's more complex.

You lose some control, that is a consequence behind using frameworks.

Cheers,
Alex
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MVC .. again

Post by Eran »

Interesting. Have you searched the history of framework discussions on these boards, or maybe the codeproject? I've been using/studying frameworks for more than 10 years...I think I am probably more familiar with CodeIgnitor/Cake/Zend/Symphony/etc than you think I am.
You didn't answer my question. How much hands on experience do you have with current versions of those frameworks? (not versions from 2 years ago or whatever). Experience is actually developing with those instead of reading and discussing about it.
Joomla has arguably the best component/plugin support when compared to other CMS.
Oranges to apples. I'm talking about frameworks, and Joomla in this respect started out as a CMS and branched out to full framework and it shows. CMS is an application, which can be built using a framework but is unrelated to it.
Joomla is also a more robust framework.
Again, as a framework its definitely not more robust. It does give you ready functionality (A CMS and an administration zone), but this functionality is pretty limiting in my perspective. In this respect wordpress is a clear winner over joomla with their administration which is far more usable and robust - but again I'm talking frameworks not specific applications.
I mention Joomla only because it's 'component' architecture is done right -- I say this in confidence having implemented and worked with countless CMS over the years -- it's what I specialize in...
It seems to me that you are favoring joomla because of your massive experience with it - leading to a high comfort level. You are very used to its flaws and can dance around those. Having worked extensively with several frameworks in the past year, Joomla is the worst by far.
Deep hierarchy. WHat kind of functionality could possibly be required by every single controller ever invoked -- or is there massive amount of overhead -- I assume more than I care for.
Talking about deep hierarchies - Joomla has the most convoluted I've ever seen. Trying to follow their code base is an iffy proposition at best, with globals all over and an inconsistent naming scheme. Zend at least follow the Pear convention for directory structure and file names making it very easy to track down specific files and classes. I now organize all of my project files with the same standards.
You lose some control, that is a consequence behind using frameworks.
I never felt I lost control when using ZF. On the contrary, it just gives me better tools to take control.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: MVC .. again

Post by alex.barylski »

I can still pee further than you, so I don't quite understand your point. :P
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MVC .. again

Post by Eran »

:)
you win!
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: MVC .. again

Post by alex.barylski »

:drunk:

When I'm finished these bad boys I'll be packing 25 PSI :lol:
sam4free
Forum Newbie
Posts: 18
Joined: Fri Oct 10, 2008 6:02 pm

New idea

Post by sam4free »

so .. the guys are friends again .. :wink:

OK .. here is an edited idea from the first idea i have posted above.. :idea:

the framework has a "main controller" which is the entry point to our software
and has a number of controllers each with its own view.
also we have an independent models (thanks arborint)

the tree call idea remains the same
each controller in the tree calls sub-controllers until we reach the leaves
the leave controller calls the model(s) it wants and passes them to the view
then it returns the result to the caller, which calls its view, and so on.

(Please remember here i am trying not to go deep in the details .. thanks)

I think it is good but have not tried it (yet)
give me your opinions ..
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MVC .. again

Post by Eran »

MVC implementations often use a Front Controller (what you refered to as a "main controller") which is a single entry point to the application. Controllers don't map 1-to-1 to views usually, rather they group together a collection of "actions" - which usually map to particular views.
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: MVC .. again

Post by Theory? »

What Zend has you do, which I think is a nice way, is to call separate actions for all the components of your view that require separate business logic. So your nav and any sidebar components each have their own action in the controller, but you can and probably should use helper actions to make sure no one can directly call your "menu" action from a URI because that would just be silly.
Post Reply