Need a light understanding MVC pattern - php

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.

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..

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.

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.

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?

Related

Create a block in Codeigniter that would generate same output from controller

I have a news blocks. These news are got from the database. What I need is to write $this->load->view('news'); on any of my pages and for the news to display. This implies that 'news' is a view and has display logic for the news AND has logic that calls the controller which calls the model to get the news (this part is what bothers me). I don't want to pass news to the view and inside the view pass it to the 'news' view.
Because news are going to be on roughly 70% of pages, I would like for this system to work (I want to use other views in such fashion). Is this possible?
You can check the Codeigniter HMVC extension:
Modular Extensions - HMVC
This is most useful when you need to load a view and its data within a view. Think about adding a shopping cart to a page. The shopping cart needs its own controller which may call a model to get cart data. Then the controller needs to load the data into a view. So instead of the main controller handling the page and the shopping cart, the shopping cart MVC can be loaded directly in the page. The main controller doesn’t need to know about it, and is totally isolated from it. You can work the same way with your news block.
I think it's a little bit tricky to try to explain everything on a simple SO answer and it has good documentation you can check out there.

yii widget render partial view of another controller

I need to create universal dialog widget for different modules in my yii-powered project, with form which will be auto serialized and passed to controller action for add/update operations.
Is it a good practice if I call render partial method in overloaded run() or init() method of my widget and pass him custom view path with html form (this form will be shown in modal dialog window after dialog-btn click action accordingly)?
My widget path:
/protected/components/widgets/DialogFormWidget.php
My forms path:
/protected/modules/module/controller/views/forms/submittable_html_form.php
go with
$this->renderPartial('application.modules.controllers.views.forms.submittable_html_form');
UPDATE :
in above code, $this refers to the controller, if you want to use controllers renderPartial from inside a widget, you have to access it like $this->controller->renderPartial (
Even you can use path to your view file to access it from other controller ($this->renderPartial('application.views.controller.view', []) , or path to any place where your view live on system), this is not good solution. Better solution is to create Yii widget so u can easy call it from all places because its controller independent. You will have also lot of benefits since with Yii widgets u have option to send some widget configuration parameters that can be used to customize widget and even load different views/style based on that parameters. Also you will have good place for custom functions that can help to add some business logic to your view and similar.
You can find some start point for this here (http://www.yiiframework.com/doc/guide/1.1/en/basics.view)

Dynamic Views in ZF2

I am working on my SaaS app which is going to require a Custom ViewModel to be built to populate widgets (left,center,right,footer, etc)
I started off using a BaseController
`class BaseController extends AbstractActionController {`
But since I am going to try to use modules like ZfcUser etc I was wondering can I create a module that builds the ViewModel and if so where/how should I hook into the event to not break forwards/redirects?
My thought is build the larger view model without interfering with normal controller behaviors. The controllers would just pass back their normal array() for the viewmodel and it would be placed it
`<?php echo $this->content; ?>`
Ok clarification on a website you generally have Header, Footer, Content and say Left Nav. So the footer has 3 columns Login Box, Lastest News, and twitter feed, the left nav has Navigation and some ad's
All this really doesnt pertain to the Controller and Action being called so I am trying to find the best place to fetch the ViewModel and how then build and populate with all these external entities (login template,twitter, Nivo Slider) and replace.
Then when the controller returns its array() have it replace the $this->content in the layout
Putting all this in each Action would be huge and would break outside module use.
I suggest to using zf2plugin, but it works with zend forward helper and maybe you don't want this.But there is a closure result mode that you can generate some dynamic content ..
Try it, hope be useful.
you maybe want to create viewhelpers located in modules like a TwitterFeed-Module. the view helper would access a service from the same module.
The TwitterFeed Service would simply request the feed from twitter (and ideally cache it for an hour or so). the viewhelper would just render the data as needed or use a viewscript for rendering or return the raw data to be processed in the layout itsself.

zend framework - creating controller for admin/action1/action2

In zend framework, how do i create an action for this type of url:
example.com/admin/create/category
which would show a page for creating a new category
or
example.com/admin/edit/category/id
which would show a page to edit a category
here, admin would be the controller, create and edit would be the action but what about the last parameter 'category'? should i check for 'category' argument inside the controller actions or is there another way ?
thanks
Having this kind of issue, I suggest using zend route. Here's the link
http://framework.zend.com/manual/en/zend.controller.router.html
You can create multiple routes for each action if needed.
I think that the good way is to check for 'category' argument inside the controller actions. Based on its value you do what you want.
Assuming you have lots of different "things" you need to administer then i woudl suggest not using ana single admin controller but rather a Category controller. Then just secure the admin actions. Alternatively you sould have 2 controllers a Category controller and an AdminCategory controller...But either way you should have multiple controllers for the admin module....
Also keep in mind you can set up routes pretty much however you like... not every segment in the url needs to map to a parameter...

Good logic of View helper for CodeIgniter

Could someone tell what is good logic for creating custom helper for Views?
What I would like to accomplish is...
load header
load main navigation (static for all pages)
load some widgets etc. (static for all pages)
load content/main pages (dynamic)
and in the end, load footer of course
Could someone point me to the solution?
Thanks a lot, in advance!
This question gives some explanation to the method dqhendricks describes. It includes code examples showing how to use $this->load->view() to return the view to a variable instead of outputting it to the browser, how to include sub-views, and using a specific view as your main template. It's the best way I've found of handling view logic. Getting the hang of CodeIgniter - Templating / loading views
Some people will create a master template, then pass the sub content's view name in the data object. The master template will call the header view, footer view, etc, and use the data array to dynamically display the correct sub content view.
For a Template library that is more actively developed than Collin's (linked by predrag.music) try out mine:
https://github.com/philsturgeon/codeigniter-template
His is good but this supports modules, themes, mobile version variations of themes and plenty more.
you could use inheritance chaining (with a custom controller class or with an included extra class that extends controller that you extend from your controllers instead of the controller class) to make a common controller hub for all pages that has a method for the top stuff, and a method for the bottom stuff.
If you need something for the reference this one is good:
http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html

Categories