0

You have the URL: http:///www.site.com/controller/method/values/1/2/3

Do I have to always have the controller being called or can I have the view being called and instantiate the controller inside the view or in a bootstrap file referring to this view?

What I don't get it is if I need more than 1 controller on the view, how to archive that?

For example: On my index page I want run a simple CMS, where the admin can change the text blocks and images of the site. That would be on the content management controller. On my index page I also got the latest added products vitrine, what would be controlled by the controller products. If I define www.site.com/contentmanagement or www.site.com to run the contentmanagement controller, how the product controller would be called?

Also, another example. On my menu I got a link to a page called aboutus, that would be a simple page and the only feature needed would be the content management controller to manage the texts blocks.

If I follow the pattern Im reading all over the place I will end with a link like: http://www.site.com/contentmanagement/method/aboutus ?

Kinda lost here cause surely this URL will look weird. Would be much easier to have the URL calling the view http://www.site.com/aboutus and a boot file where I can tell the controller that should be loaded when the surfer is there ...

bootstrap would look like:

switch($view) case: index controller load contentmanagement controller load product case: aboutus controller load contentmanagement

I appreciate any help or a light here, thanks.

by the way, Im coding in PHP.

Henrique
  • 21
  • 3

4 Answers4

1

Hm, if you want to have text blocks and images of the site (one controller), and products vitrine (second controller), then call the methods you need in one controller..

I would do it this way: when you request index page with all the elements you mentioned above, actually one controller is called, and it decides which view to show.. so in that controller, you call the all methods that you need, get the data, and pass it to the view.. if the methods are called from other controllers, just call that methods and get the data.. i often make some static methods in controllers, which I can call anywhere, so I don't have to instantiate whole object..

E.g. you call www.site.com/contentmanagement, controller is called, which will display index view, and in that controller, you call all methods you need, prepare the data, and pass that data to the final view which will be displayed..

Adrian
  • 1,499
  • 3
  • 19
  • 26
  • Im trying to come with a pattern that works for my business, so I can code the sites of my clients fast without much rewriting when the needs are the same. So, would be doable having a contentmanagement controller, product controller, and others like login controller, categories controller on my controllers library and for each project I code a specific controller for it where would load all other controllers when a page is called? So, in my index I would code an index.controller and call the others inside the index.controller methods? Now, what would be the best solution ... continue ... – Henrique Sep 03 '11 at 22:23
  • for the second part of my question? I want to have a clean URL like site.com/about_us and not something like site.com/main_controller/aboutus ... Any suggestion? Would be a way to go if I have the first param always being the view/mathod and by default I load a single controller called index.controller and for each view I have a method where would load others controllers needed for that view and then render the view? – Henrique Sep 03 '11 at 22:25
  • Hm, you dont' have to do site.com/main_controller/aboutus that's for sure.. every site has two sides, frontend and backend, and I got my frontend done so it always calls the 'content' module by default, and displays the content.. if I have e.g. site.com/about-us, module 'content' is loaded (the public side), which checks the slug 'about-us' and loads that content.. in case the db doesn't have about-us slug, it redirects to 404 page, simple :) This is simplified process.. – Adrian Sep 04 '11 at 07:06
1

Do I have to always have the controller being called or can I have (..blah blah..)?

It kinda depends on what you understand by "call".

Controller needs an ability to change state of both View and Model(s). And each View need ability to request data (in Model2 MVC) from Model(s).

Thought Controller should not cause the rendering of View (which is common in all the RoR sycophants) much like View has not business in executing actions on the Controller.

What I don't get it is if I need more than 1 controller on the view, how to archive that?

Views and Controllers should have 1:1 relationship. If your view need more then one controller, you might want to look into HMVC architecture.

For example: On my index page I want run a simple CMS, (.. more blah .. ) how the product controller would be called?

CMS should be a separate application ( or at least module ) with multiple controllers.

If I follow the pattern Im reading all over the place I will end with a link like: http://www.site.com/contentmanagement/method/aboutus ?

Why not just http://site.com/cms/content/2/edit (where 2 is the ID for "about us" page). There is no law which states that URL structure for site administration must mirror the publicly available page .. hell .. it can actually cause increased vulnerability.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Thanks, still confused but will search more of this topic. But the URL Im talking about is the public view. CMS would request the data blocks from the CMS database table and the product would request the data from the products table. – Henrique Sep 03 '11 at 22:19
  • 1
    @Henrique Data gathering is something done by DAOs , which are contained in the Models. And usually Models have no direct relationship with URL. That actually is the whole point of having Models in first place. – tereško Sep 03 '11 at 22:25
  • I will have a database model where will be called from the CMS controller to handle the content management but will also be called from the product controller to handle the product listing. – Henrique Sep 03 '11 at 22:31
  • @Henrique You should additionally learn more about "Domain Business Model", "Domain Object" and "Datamapper pattern" ( do not mistake with misnamed ORMs ). It seems that your understanding of models is a bit off. – tereško Sep 03 '11 at 22:35
  • That might sound stupid, but, what I called controller on my example above, cms, products would be the models? index.controller would send the data to the cms model, that would do all business logic and return to the index.controller the results. Same for the products, and after that the index.controller would return the view? – Henrique Sep 03 '11 at 23:00
  • For managing product i would use something like *Repository* as primary model, with *Product* and *ProductCollection* as domain objects. And i would bind the *Repository* to the view. – tereško Sep 03 '11 at 23:31
0

I am going to try to walk

What I don't get it is if I need more than 1 controller on the view, how to archive that?

For example: On my index page I want run a simple CMS, where the admin can change the text blocks and images of the site. That would be on the content management controller. On my index page I also got the latest added products vitrine, what would be controlled by the controller products. If I define www.site.com/contentmanagement or www.site.com to run the contentmanagement controller, how the product controller would be called?

Having the URL to contains the controller's name is definitely nice, but it is not the requirement for MVC. So you don't have to necessary map your controller to the URL itself. A classic MVC does not tie itself to a particular naming convention, so you could call your product controller via different URL which then process the product and show the view for the product.The important for MVC is to have one controller that deals with sets of model and resulting in one view as the presentation layer. In your particular example, your entry point seems to be a single controller that interacts with both ContentManagement and Product Class/Module and the resulting interaction produce a single view.

bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement

Thus your original interaction above is not completely off, except that you are not really calling two controllers, but rather doing the following upon hitting index:

  • Load a controller, let's call this one IndexController
  • Load ContentManagement module to get the related content, you might want to put the info as part of your Model for the Index page
  • Load Product module to get related products, again put this into your Model
  • Pass the Model containing the info for rendering the page into the View
  • In your View, you render the Model that contains both Content from ContentManagement module and Product list from the Product module thereby producing a single view

If I follow the pattern Im reading all over the place I will end with a link like: http://www.site.com/contentmanagement/method/aboutus ?

This is again not a requirement, you should follow what makes sense and easier for the users. Also, if you are required to follow the conventions, you could always make your URL pretty by using URL mapping.

momo
  • 21,233
  • 8
  • 39
  • 38
  • Thanks, makes sense like the other comment I just added, will probably have 1 controller as main, always loaded, and it will decide what controllers to load based on the URL request and based on how I code it to interact with the URL request. Been thinking on get the first parameter of the URL as a method, which got the same name of a view and call all other controllers inside this method. But then, I would end with 1 method per page of my site and that seems kinda procedural no? Like stepping back to the procedural programming. – Henrique Sep 03 '11 at 22:30
  • You don't have to build the method switch. You could still map one URL with one controller/object and use the last path as the name of your controller. So in your example the simple http://www.site.com/aboutus can be mapped to load AboutUsController in your code. What I am saying about IndexController is that your www.site.com is automatically mapped to IndexController that calls both ContentManagement and Product module. I didn't mean that as a single point of entry that branch based on method. Also which PHP Framework you are using currently? Might help to clarify if we understand that. – momo Sep 03 '11 at 22:38
  • Actually Im coding my own framework that suit my needs. All my projects are based in the same admin panel, so its kinda a php+javascript+html framekwork, a collection of sets where I can easily build the sites of my clients. A simple copy and past of the hole directory and few adjustments I can get the same system working on a different client and code their special requests. What you assumed is correct, index would be mapped to the indexController and than, inside it I can call the content management and product module. As for the about us, it could call the aboutus controller and ... – Henrique Sep 03 '11 at 23:16
  • Inside it have the rules to render the contentmanagement or any other module needed ... Just dont wanted to write a controller per pagename but its probably the way to go? right? – Henrique Sep 03 '11 at 23:18
  • Your question has been discussed for a long time. There are two classical MVC Pattern for handling the Controller. The method switch that you mentioned is known as Front Controller pattern while Controller per page is known as Page Controller pattern. Some framework promotes one pattern over each other (for example ASP.NET promote Page Controller pattern more so that Front Controller). Eventually, I feel each has appropriate usage and as a developer you will eventually have to decide which one is best fit to your situation. – momo Sep 03 '11 at 23:37
-1

What you are looking for is called HMVC and there are a couple of frameworks for that out there, like Kohana.

Also see this question:

What is the HMVC pattern?

Community
  • 1
  • 1
thwd
  • 23,956
  • 8
  • 74
  • 108