2011-07-07 23 views

回答

4

你需要讓你的組件的主目錄下的一個文件夾(如組件/ 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 
+0

但是當你需要調用與組件不同的控制器會發生什麼?是否有默認路由器或者是否需要手動處理這種映射? – emeraldjava

相關問題