Joomla源代碼解析(十九) JController

同樣 JController 是MVC中重要的起點,正式這個類決定的動作的下一步流向,我們來看看錶格提交數據的典型的controller的代碼:

 function edit()
 {
  JRequest::setVar( 'view', 'hello' );
  JRequest::setVar( 'layout', 'form'  );
  JRequest::setVar('hidemainmenu', 1);
  parent::display();
 }
 /**
  * save a record (and redirect to main page)
  * @return void
  */
 function save()
 {
  $model = $this->getModel('hello');
  if ($model->store($post)) {
   $msg = JText::_( 'Greeting Saved!' );
  } else {
   $msg = JText::_( 'Error Saving Greeting' );
  }
  // Check the table in so it can be edited.... we are done with it anyway
  $link = 'index.php?option=com_hello';
  $this->setRedirect($link, $msg);
 }
 /**
  * remove record(s)
  * @return void
  */
 function remove()
 {
  $model = $this->getModel('hello');
  if(!$model->delete()) {
   $msg = JText::_( 'Error: One or More Greetings Could not be Deleted' );
  } else {
   $msg = JText::_( 'Greeting(s) Deleted' );
  }
  $this->setRedirect( 'index.php?option=com_hello', $msg );
 }
 /**
  * cancel editing a record
  * @return void
  */
 function cancel()
 {
  $msg = JText::_( 'Operation Cancelled' );
  $this->setRedirect( 'index.php?option=com_hello', $msg );
 }

實際上 controller 跟提交的task參數,調用controller中的不同的函數,當然默認會調用display ,我覺得還需要記住的就是

getModel ,和setRedirect ,其餘函數用到再看就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章