Here are some sample methods from my Controller
class. Now when the user clicks the New button $task=add is sent to the Controller and the add() method is called. As you can see it does not really do anything, it just creates a url and forwards it off to the correct view. Is this the correct way of doing things in the MVC pattern?
/**
* New button was pressed
*/
function add() {
$link = JRoute::_('index.php?option=com_myapp&c=apps&view=editapp开发者_运维知识库&cid[]=', false);
$this->setRedirect($link);
}
/**
* Edit button was pressed - just use the first selection for editing
*/
function edit() {
$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
$id = $cid[0];
$link = JRoute::_("index.php?option=com_myapp&c=apps&view=editapp&cid[]=$id", false);
$this->setRedirect($link);
}
I don't believe this is the correct way. I would suggest looking at some of the core Joomla! code to see how it's done. A great, easy example that I always look at is Weblinks. Take a look at what they do in the edit function of their controller:
.../components/com_weblinks/controllers/weblink.php
function edit()
{
$user = & JFactory::getUser();
// Make sure you are logged in
if ($user->get('aid', 0) < 1) {
JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
return;
}
JRequest::setVar('view', 'weblink');
JRequest::setVar('layout', 'form');
$model =& $this->getModel('weblink');
$model->checkout();
parent::display();
}
They set the view and layout variables and then call parent::display to let Joomla! go out and display that view/layout.
精彩评论