2
我的結構在我的組件的根目錄中有component.php。我正在使用http://www.notwebdesign.com/joomla-component-creator/j15/index.php 我覺得需要讓多個控制器有更清晰的代碼,因爲我有大約12個任務要運行。我使用多個模型,需要有多個控制器。在Joomla組件開發中使用多個控制器
任何人都可以指出我正確的方向嗎?示例代碼非常感謝。
我的結構在我的組件的根目錄中有component.php。我正在使用http://www.notwebdesign.com/joomla-component-creator/j15/index.php 我覺得需要讓多個控制器有更清晰的代碼,因爲我有大約12個任務要運行。我使用多個模型,需要有多個控制器。在Joomla組件開發中使用多個控制器
任何人都可以指出我正確的方向嗎?示例代碼非常感謝。
你需要讓你的組件的主目錄下的一個文件夾(如組件/ com_mycom /控制器/ ...)
然後我的組件的主文件(它應該被稱爲「mycom.php」使用例如,從上圖)有代碼看起來像這樣:
// no direct access
defined('_JEXEC') or die('Restricted access');
// load a constants file for use throughout the component
require_once(JPATH_COMPONENT.DS.'lib'.DS.'constants.php');
// fetch the view
$view = JRequest::getVar('view' , 'null');
// use the view to fetch the right controller
require_once(JPATH_COMPONENT.DS.'controllers'.DS.$view.'.php');
// initiate the contoller class and execute the controller
$controllerClass = 'MycomController'.ucfirst($view);
$controller = new $controllerClass;
// call the display function in the controller by default - add a task param to the url to call another function in the controller
$controller->execute(JRequest::getVar('task', 'display'));
$controller->redirect();
然後在你的控制器目錄中的控制器代碼將正常如
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class MycomControllerAdd extends JController
{
function display() {
$viewLayout = JRequest::getVar('tmpl', 'add');
$view = &$this->getView('add', 'html');
$model = &$this->getModel('add');
$view->setModel($model, true);
$view->setLayout($viewLayout);
$view->addStuff();
}
...
的URL會調用這個看起來像這樣:
http://somedomain.com/index.php?option=com_mycom&view=add
但是當你需要調用與組件不同的控制器會發生什麼?是否有默認路由器或者是否需要手動處理這種映射? – emeraldjava