how to get stuff from controller to view...

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
kabo
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Feb 19, 2008 5:01 pm

how to get stuff from controller to view...

Post by kabo » Wed Feb 20, 2008 7:25 am

Hi!

I've made a component and in the controller done:

Code: Select all

$model =& $this->getModel();
$sector = "User-input-here";
$model->setSector($sector);
But when I do this in my view:

Code: Select all

$model =& $this->getModel();
echo $model->getSector();
I get nothing! Am I missing something here?
Is this the correct way to handle userinput?
Thanks for any help!

kabo
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Feb 19, 2008 5:01 pm

Re: how to get stuff from controller to view...

Post by kabo » Wed Feb 20, 2008 9:02 am

OK, I've solved it by doing it this way instead.
In the controller:

Code: Select all

$sector = "User-input-here";
$document =& JFactory::getDocument();
$viewType = $document->getType();
$view =& $this->getView(null, $viewType);
$view->setSector($sector);
And then in the view:

Code: Select all

$model =& $this->getModel();
$model->setSector($this->sector);
$model->doStuffThatRequiresASector()
It works but seems like weird way of doing it... It would be better if I could just manipulate the model directly from my controller. :/

kabo
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Feb 19, 2008 5:01 pm

Re: how to get stuff from controller to view...

Post by kabo » Wed Feb 20, 2008 2:52 pm

AHA!
Figured out a better way to do it :)

Controller:

Code: Select all

function display() {
$model =& $this->getModel();
$document =& JFactory::getDocument();
$viewType = $document->getType();
$view =& $this->getView(null, $viewType);
$model->setSector("User-input");
$view->setModel($model);
$view->display();
}
View:

Code: Select all

function display($tpl = null) {
$foo = $this->model->DoStuffThatRequiresASector();
$this->assignRef('foo', $foo );
parent::display($tpl);
}
function setModel(&$model) {
$this->model =& $model;
}
Hope this post can help somebody else and help them figure it out faster than I did...

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: how to get stuff from controller to view...

Post by Rogue4ngel » Thu Feb 21, 2008 12:09 am

Thanks for posting your findings! It may come in handy for someone else.
If you're not a part of the solution, you're a part of the problem.


Post Reply