2015-12-24 69 views
1

我已經安裝了Joomla v3.4.7來測試和準備我的項目。我根據官方教程[https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Using_the_database][1] 一步一步地創建了一個組件'HelloWorld',我成功地顯示了數據列表,然後編輯頁面添加或編輯現有的數據,從管理員的一部分,就像如何將Joomla管理員組件複製爲一個站點?

localhost/joomla-test/administrator/index.php?option=com_helloworld 

完成這些之後,我只需在/Administrator/components/com_helloworld文件複製到/components/com_helloworld並覆蓋以前的文件,並訪問站點組件:

localhost/joomla-test/index.php?option=com_helloworld 

我沒有工作!我用螢火蟲調試,我得到了一個

NetworkError: 500 Internal Server Error - http://localhost/joomla-test/index.php?option=com_helloworld

錯誤....發生了什麼事?

我的代碼:

網站/ helloworld.php:

<?php 
// import joomla controller library 
jimport('joomla.application.component.controller'); 

// Get an instance of the controller prefixed by HelloWorld 
$controller = JControllerLegacy::getInstance('HelloWorld'); 

// Perform the Request task 
$controller->execute(JFactory::getApplication()->input->getCmd('task')); 

// Redirect if set by the controller 
$controller->redirect(); 

網站/ Controller.php這樣

<?php 
// No direct access to this file 
defined('_JEXEC') or die; 

// import Joomla controller library 
jimport('joomla.application.component.controller'); 

/** 
* General Controller of HelloWorld component 
*/ 
class HelloWorldController extends JControllerLegacy 
{ 
    /** 
    * display task 
    * 
    * @return void 
    */ 
    protected $default_view = 'helloworlds'; 

    public function display($cachable = false) 
    {    
     parent::display($cachable); 
     echo "controller"; 
     return $this; 
    } 
} 

網站/視圖/ helloworlds/view.html.php:

<?php 
    // No direct access to this file 
    defined('_JEXEC') or die; 

    // import Joomla view library 
    jimport('joomla.application.component.view'); 

    /** 
    * HelloWorlds View 
    */ 
    class HelloWorldViewHelloWorlds extends JViewLegacy 
    { 
    /** 
    * HelloWorlds view display method 
    * @return void 
    */ 
    function display($tpl = null) 
    { 
     // Get data from the model 
     $items = $this->get('Items'); 
     $pagination = $this->get('Pagination'); 

     // Check for errors. 
     if (count($errors = $this->get('Errors'))) 
     { 
      JError::raiseError(500, implode('<br />', $errors)); 
      return false; 
     } 
     // Assign data to the view 
     $this->items = $items; 
     $this->pagination = $pagination; 

     // Set the toolbar 
     $this->addToolBar(); 

     // Display the template 
     parent::display($tpl); 
    } 

    /** 
    * Setting the toolbar 
    */ 
    protected function addToolBar() 
    { 
      JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS')); 
     JToolBarHelper::deleteList('', 'helloworlds.delete'); 
     JToolBarHelper::editList('helloworld.edit'); 
     JToolBarHelper::addNew('helloworld.add'); 
    } 
} 

請大家幫忙,謝謝大家。

回答

0

它不以這種方式工作(通過只複製文件夾)。您將不得不通過打包安裝該組件,然後將其安裝在服務器上。您將需要在服務器上安裝壓縮組件(具有XML清單文件)。

請嘗試以下操作:從Joomla下載基本的HelloWorld組件,然後將其安裝到您的網站上,然後使用localhost中的文件覆蓋它。

0

網站和管理員有輕微的差異;最相關的是與模板相關的,因爲在Admin中你可以指望一個標準的佈局;這就是爲什麼在管理員view.html中設置工具欄和側邊菜單;在前端,您可以使用配置創建指向視圖的菜單。

最好的辦法是爲控制器和視圖創建新的文件,然後您可以創建從管理員模塊繼承的模型,這是避免代碼重複的最佳選擇,它仍然會爲您提供最大的自定義靈活性觀點。

0

工具欄無法在前端工作。這很奇怪,是的,但是如果你看它在管理員文件夾中是一個單獨的東西。它實際上檢查你是否也在管理員。我曾經做過一個補丁來取消檢查,但事實證明,它已經打破了大量的解決此問題的組件。
其次,有很多調用依賴於相對定位的東西,或者甚至可能明確要求管理員訪問。 第三,確實有些東西稍有不同,因爲在後端基本上不會呈現普通視圖,只有列表視圖和編輯視圖。

如果您想在前端執行管理功能,最好的一般方法是查看com_config,Com_templates和com_modules是如何執行的。