Trying to comprehend MVC

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

Post Reply
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Trying to comprehend MVC

Post by thinsoldier »

Trying to comprehend MVC here.

i've got a part of a site that does 3 things

show search form
show search results
show full record details

I imagine the urls like:
site.com/index.php/realty/search/
site.com/index.php/realty/results/4/ (4 = pagination page number)
site.com/index.php/realty/details/127/ (127 = record id)

I get the basic idea of setting up a _controller_ for that.
1 controller,
a model for pulling the data from mysql,
possibly a separate model/business logic if the search logic is complicated
and 3 methods/actions in the controller to show the 3 "pages",
and 3 templates

But what if one of those 3 actions actually does a few more things?

site.com/index.php/realty/details/127/print/ (show a print-friendly template)
site.com/index.php/realty/details/127/share/ (show a form where the visitor can e-mail a link to his friends)
site.com/index.php/realty/details/127/sharesent/ (show a confirmation page indicating that e-mail was sent)

Do I make another controller just for the 'details' part and somehow put that controller inside the first controller which is already inside of a front-controller?
Warning: I have no idea what I'm talking about.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying to comprehend MVC

Post by Christopher »

It is not clear to me which is the controller: realty or details ?

I tend to do the following (implying that realty is the controller and details, print, etc. are methods):

site.com/index.php/realty/details/127/
site.com/index.php/realty/print/127/
site.com/index.php/realty/share/127/
site.com/index.php/realty/sharesent/127/

But details could also be the controller (and realty the module).
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Trying to comprehend MVC

Post by josh »

Actions should perform 1 task, if that action is doing 3 tasks, that action could probably be moved into it's own controller and that method broken up.

Putting the id before the action like /details/127/print is a cool idea, it wouldn't hurt anything and may even make the site easier to crawl in a logical order, or easier for humans to jump from action to action by hand editing the URLs.

I tend to do it the same as Christopher tho, because on the other hand the users may want to edit the ID and remain at the same action, rather then edit the action and remain at the same ID, it would just depend on the user's preference I would say.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Trying to comprehend MVC

Post by thinsoldier »

I think the "module" thing is what's getting me.

So with "modules" then index.php/realty/details/127/
would be FrontController/module/controller/id/
allowing for
index.php/realty/details/view/127/
index.php/realty/details/print/127/
index.php/realty/details/share/127/

index.php/realty/search/simple/
index.php/realty/search/advanced/
index.php/realty/search/results/

index.php/user/manageAccount/viewDetails/
index.php/user/manageAccount/editDetails/
index.php/user/favourites/list/
index.php/user/favourites/add/
index.php/user/favourites/edit/
index.php/user/favourites/delete/
index.php/user/savedsearches/listRSS/
index.php/user/savedsearches/listWeeklyEmail/
index.php/user/newsletter/subscribe/
index.php/user/newsletter/unsubscribe/

index.php/newsletter/..? um... newsletter again? /unsubscribe/
index.php/newsletter/..? um... newsletter again? /subscribe/
index.php/newsletter/..? um... newsletter again? /archives/
index.php/newsletter/..? um... newsletter again? /cron_send_emails/
index.php/newsletter/..? um... newsletter again? /cron_cleanup/

Does it sound like I'm thinking in the right direction?
( :( if only one of these damned frameworks would run on my server. )
Warning: I have no idea what I'm talking about.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying to comprehend MVC

Post by Christopher »

thinsoldier wrote:Does it sound like I'm thinking in the right direction?
Yes.
thinsoldier wrote:( :( if only one of these damned frameworks would run on my server. )
Are you PHP4 ?
(#10850)
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Trying to comprehend MVC

Post by thinsoldier »

no, but really really really close

what about this part: index.php/newsletter/..? um... newsletter again? /unsubscribe/
would I really have to put 'newsletter' in that spot?

I'm guessing real frameworks have a way to indicate some sort of assumed default controller based on the module name in special cases.
Warning: I have no idea what I'm talking about.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying to comprehend MVC

Post by Christopher »

Yes, you can define a default module, controller and method.
(#10850)
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Trying to comprehend MVC

Post by thinsoldier »

Making it a point to try out skeleton before the start of next week and zend framework before the end of the month.

Have anything "beginner's starting point"-like I should read first on skeleton?

For the site I'll be trying to re-create I'm pretty sure for every user accessible *.php file will be a "module" in framework terms... I think... ok, I'm not so sure.
Warning: I have no idea what I'm talking about.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying to comprehend MVC

Post by Christopher »

thinsoldier wrote:Making it a point to try out skeleton before the start of next week and zend framework before the end of the month.

Have anything "beginner's starting point"-like I should read first on skeleton?
There are some docs. And the Blog example is pretty standard.
thinsoldier wrote:For the site I'll be trying to re-create I'm pretty sure for every user accessible *.php file will be a "module" in framework terms... I think... ok, I'm not so sure.
I think you have the wrong idea of 'module'. I think of each module as a mini-application. The Admin area if a website is the most common use fora module, so all of the URLs start with "example.com/admin/". A module give you an separate directory tree that has its own controllers/models/views directories.
(#10850)
Post Reply