我很難弄清楚JomSocial中的路由是如何工作的。有誰知道如何創建一個新的視圖?如何在Jomsocial中創建新視圖
回答
首先,你創建一個控制器,使視圖的要求:
文件:控制器/ hello.php
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
class CommunityHelloController extends CommunityBaseController
{
function helloWorld() //index.php?option=com_community&view=hello&task=helloWorld
{
$document = JFactory::getDocument();
$viewType = $document->getType();
$view = $this->getView('hello', '' , $viewType);
echo $view->get('helloWorld');
}
function hello() //index.php?option=com_community&view=hello&task=hello
{
$document = JFactory::getDocument();
$viewType = $document->getType();
$view = $this->getView('hello', '' , $viewType);
echo $view->get('helloWorld');
}
}
?>
查看:查看/你好/ view.html.php 在這裏,你將要傳遞到模板文件 例如變量:
<?php
defined('_JEXEC') or die('Restricted access');
jimport ('joomla.application.component.view');
class CommunityViewHello extends CommunityView {
function helloWorld() //This function shows a "Hello World" without an template view
{
echo 'Hello World';
}
function hello()
{
$user = CFactory::getUser($userid);
$tmpl = new CTemplate(); //Call a template file
echo $tmpl->set ('user', $user)
->fetch ('hello'); // Returns the templates/default/hello.php file
}
}
文件模板/默認/ hello.php:
<?php defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $user->name; ?></p>
</div>
就這樣!
我可能已將此作爲對2013年由@ Thavia-Farias給出的答案的評論,但我的聲望還不足以發表評論。
首先,在控制器/作爲提供hello.php @ Thavia-:我的回答的內容將根據使用Jomsocial 4.2.1我的經驗與新的關鍵信息,修正和改進沿重申她的信息Farias有一個錯誤:在function helloWorld()
和function helloWorld()
函數hello()中,最後一行是echo $view->get('helloWorld');
,但function hello()
中的函數應該是echo $view->get('hello');
。既然這樣,既*的index.php?選擇= com_community &視圖=你好&任務= 的HelloWorld和的index.php?選擇= com_community &視圖=你好&任務=你好都將調用HelloWorld的觀點,而不是第二個調用hello視圖,因爲它應該。
而且,根據我的經驗,而不是把模板在路徑/templates/default/hello.php,我把它放在/模板/ customtemplatename/HTML/com_community /佈局如果您使用的是如果您使用默認的jomsocial模板,則可以使用cusomt模板或/components/com_community/templates/jomsocial/layouts/。
創建/components/com_community/controllers/hello.php:
<?php
defined('_JEXEC') or die();
class CommunityHelloController extends CommunityBaseController
{
public function renderView($viewfunc, $var = NULL) {
$my = CFactory::getUser();
$jinput = JFactory::getApplication()->input;
$document = JFactory::getDocument();
$viewType = $document->getType();
$viewName = $jinput->get('view', $this->getName());
$view = $this->getView($viewName, '', $viewType);
echo $view->get($viewfunc, $var);
}
function helloWorld()
{
$this->renderView(__FUNCTION__);
}
function hello()
{
$this->renderView(__FUNCTION__);
}
function display($cacheable = false, $urlparams = false) {
$this->renderView(__FUNCTION__);
}
}
?>
創建/var/www/html/components/com_community/views/hello/view.html.php:
<?php
defined('_JEXEC') or die('Restricted access');
jimport ('joomla.application.component.view');
class CommunityViewHello extends CommunityView {
function helloWorld() //This function shows a "Hello World" without an template view
{
echo 'Hello World';
}
function display() //This function what happens when the hello view is called without a task
{
echo 'welcome to the main landing page for the hello view! There is nothing else shown here besides this message.';
}
function hello()
{
echo $tmpl->fetch('hello');
}
}
正如你所看到的,如果你希望你的觀點有一個默認的視圖,即使沒有任務被調用,類似於與/index.php?option=com_community發生view = groups然後您將想要將任務命名爲控制器中和視圖中的功能顯示。
最後,創建/components/com_community/templates/jomsocial/layouts/hello。PHP:
<?php defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $my->name; ?></p>
</div>
$我的定義早在控制器!當您的視圖和任務分組足夠大時,每個任務都會有不同的文件。任務文件與view.html.php中的提取函數一起使用。
$tmpl = new CTemplate(); //Call a template file
echo $tmpl->set ('vars1', $vars1)
echo $tmpl->set ('vars2', $vars2)
echo $tmpl->set ('vars3', $vars3)
->fetch ('hello');
調用/components/com_community/templates/jomsocial/layouts/hello.php文件。
使用->fetch ('hello.greeting');
調用/components/com_community/templates/jomsocial/layouts/hello.greeting.php。
如果你想創建一個新的目錄,這些再->fetch ('hello/create');
電話 /components/com_community/templates/jomsocial/layouts/hello/create.php
如果你想創建菜單項和別名你需要創建一個新的文件(如果你想爲你的任務傳遞菜單項定義的參數,你需要創建一個新的文件(以及第二個文件,並修改第三個文件):
創建文件:/components/com_community /views/hello/metadata.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<view title="Groups">
<message>
<![CDATA[
Hello view
]]>
</message>
<options var="task">
<default name="Hello" msg="displays landing page" />
<option value="hello" name="- one greeting" msg="Display detail page of one greeting" />
<option value="helloWorld" name="- helloworldview" msg="Display whatever you have in the hello world task" />
</options>
</view>
<state>
<name>Hello Groups Layout</name>
<description>Hello Groups listings</description>
</state>
</metadata>
該文件會將項目添加到管理員菜單面板中菜單的「社區」部分。選項value
s是任務的名稱。沒有value
的使用default
標記的選項將拉起前面描述的display
函數。
如果您需要的參數添加到文件中,那麼你需要做的事情有點複雜:
創建/components/com_community/views/hello/tmpl/default.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="Name" option="View">
<message>
</message>
</layout>
<fields name="params">
<fieldset
name="basic"
label="Selected Group">
<field
name="item_id"
query="SELECT `id`, `name` FROM #__community_groups_category WHERE ORDER BY `id`"
type="sql"
key_field="id"
value_field="name"
label="Associated Group"
require="true"
description="Select the jomsocial group whose hello task this will be associated with">
</field>
</fieldset>
</fields>
</metadata>
這將創建一個選項卡,其中用戶可以從數據庫中的可用組中指定一個組。它會將該組的ID分配給#__menu
數據庫表中params
列JSON對象的參數字段,作爲item_id
鍵的值。爲了呈現頁面時,您的視圖中使用該值,包括下面的代碼在的意見/你好/ view.html.php:
$mainframe = JFactory::getApplication();
$jinput = $mainframe->input;
$document = JFactory::getDocument();
// Get category id from the query string if there are any.
$groupId = $jinput->getInt('group', 0);
// Load the parameters.
$params = $mainframe->getParams();
$params_array = $params->toArray();
if (isset($params_array['item_id']))
{
$groupId = $params_array['item_id'];
}
這樣一來,你的任務可以接收來自必要的細節要麼從組件內部提供URL(option = com_community & view = hello & task = hello & groupid = 5),或者從菜單數據庫中存儲的參數的主菜單或社會工具欄項目圖形調用該菜單項的表格。
您在此處創建的選項和選項卡對於此任務的所有菜單項均可見。如果你想爲不同的菜單選項選擇不同的標籤,你將不得不創建完全不同的視圖。將所有內容都放在一個視圖中可能會導致未使用且可能導致誤導的選項卡,其中的值可由用戶設置,但不會或不應該由用戶指定的實際任務使用。
請原諒我未在集成組件中測試此代碼的每一行。在我看來,我已經完成了所有這些功能,但是已經刪除了我的代碼,這些代碼是根據@ Thavia-Farias的回答初步指導構建的。雖然它比發佈我的擴展代碼更清晰,但它尚未經過功能測試。一定要檢查你的PHP錯誤日誌來調試你的項目。我必須以root身份登錄(sudo su
),並在我的系統上使用nano/var/log/mysqld/error_log進行檢查。祝你好運!
- 1. 如何在jomsocial中創建多個社區?
- 2. 如何在新創建的視圖上觸發視圖旋轉?
- 3. 如何在視圖中創建新項目在ASP.net MVC中創建?
- 4. 在OpenGL中重新創建Android視圖
- 5. 在Eclipse中創建新視圖
- 6. 在視圖中創建新集合 - Backbone.js
- 7. 如何在PHP中創建樹視圖?
- 8. 如何在SmartInspect中創建視圖?
- 9. 如何在android中創建視圖?
- 10. 如何在Android中創建視圖?
- 11. 如何在活動中創建視圖?
- 12. 如何在SKScene中創建子視圖?
- 13. 如何在視圖中創建方法?
- 14. 如何在Android中創建樹視圖?
- 15. 如何在Android中創建視圖?
- 16. 如何在SQL Server中創建視圖?
- 17. 如何在MySQL中創建視圖?
- 18. 如何在Ionic中創建此視圖?
- 19. 如何在Access 2007中創建視圖?
- 20. 如何使用python API在bigquery中創建新視圖?
- 21. 如何動態更新分段(SlidingMenu)在視圖中創建
- 22. 如何在for循環中創建新的android視圖?
- 23. 如何在SQL視圖中創建新列時使用條件
- 24. 如何在TSQL中創建可更新視圖
- 25. 如何在eclipse 4.2中創建一個新視圖?
- 26. 如何在XCode 4.3.2中創建新的表格視圖?
- 27. 如何在XCode 4.3中創建新的視圖控制器
- 28. 如何在獨立視圖中創建新控制檯
- 29. 如何在ClojureScript中創建新圖像
- 30. 如何創建視圖
我一直在尋找這個答案一段時間,所以謝謝。但是,當我嘗試導航到index.php?option = com_community&view = hello&task = hello時,它只是將我重定向回index.php。我錯過了什麼嗎? – FlabbyRabbit
我想我已經想通了......'view.html'需要'view.html.php' – FlabbyRabbit