Welcome Guest, Not a member yet? Register   Sign In
Execute controller from view
#1

[eluser]Base Willy[/eluser]
I wonder if it is possible to execute a controller from view?
For example, a have a view of user's page and i want to display his last photos, fresh entries from his blog, last comments etc. by executing corresponding models. How can i do that? Thanks.
#2

[eluser]dedenf[/eluser]
yes it's possible
#3

[eluser]Base Willy[/eluser]
I thought so too, but how? Smile
#4

[eluser]stevepaperjam[/eluser]
...dyou mean through javascript? altering the content of the current page without reloading it?
#5

[eluser]Base Willy[/eluser]
No, i meant through something like:

$this->load->controller('blogs/lastPosts', $params)

in the view file.

But i can't find anything like that


I thought about using ajax to simply load this content dynamically, but it's not always good for me
#6

[eluser]wiredesignz[/eluser]
Do some study on the MVC design pattern before you try that.

"In MVC, The controller is NOT a Mediator between the view and the model. The controller does not sit in between the model and the view. Both the controller and the view have equal opportunity to access the model. The controller does not copy data values from the model to the view, although it may place values in the model and tell the view that the model has changed."

http://www.phpwact.org/pattern/model_view_controller
#7

[eluser]gtech[/eluser]
If you are displaying static HTML you should work out all the data you want display in the controller (by calling multiple models) and then build up a list of variables to parse in the view. you can call other controllers (or the same controller with different params) by providing a link in a href or form for the user to click on.

If you are building dynamic pages that use javascript and need to update/retrieve data without calling a new URL (drag and drop might be an example) then I have used ajax succesfully with the js file being loaded in a view.

home.js
Code:
$.ajax({
    async: false,               // wait for return
    data: '',                   // no data to pass
    dataType: 'json',
    success: home.loadComplete, // function to call on return
    type: 'POST',
    url: site + "/code/ajaxGetPrefs/" // the php controller to call
  });
  ....
  loadComplete: function(json_data)
  {
    ...
#8

[eluser]Pygon[/eluser]
In MVC -- your view should only invoke controller changes through user interaction with the view model. You should consider passing the controller results to the view instead of requesting them from the view.
#9

[eluser]abmcr[/eluser]
A question similar: it is possible to call a function of a controller into a function of another controller? For example


function temp(){
?????? how to
$data=$this->function_of_another_controller($parameters);

Thank You
#10

[eluser]Rick Jolly[/eluser]
[quote author="abmcr" date="1193948361"]A question similar: it is possible to call a function of a controller into a function of another controller? For example


function temp(){
?????? how to
$data=$this->function_of_another_controller($parameters);

Thank You[/quote]
If you have methods that need to be called from more than one controller, abstract them into a library.




Theme © iAndrew 2016 - Forum software by © MyBB