【问题标题】:Can't Change layout in Custom Joomla 3 Component无法更改自定义 Joomla 3 组件中的布局
【发布时间】:2013-09-11 05:35:26
【问题描述】:

我试图让我的 Joomla 3 组件呈现 url 中指定的布局,但我不明白为什么它坚持显示 test.php 布局。下面的所有相关代码和我使用的网址是:

mysite.com/index.php?option=com_test&controller=test&layout=test2

也许我做错了,但这是我目前的代码:

代码:

joomla/components/com_test/test.php:

<?php defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.session.session' );
JTable::addIncludePath(JPATH_COMPONENT.'/tables');
JLoader::registerPrefix('Test', JPATH_COMPONENT);
TestHelpersAssets::load();
$app = JFactory::getApplication();
$controller = $app->input->get('controller','default');
$classname = 'TestControllers'.ucwords($controller);
$controller = new $classname();
$controller->execute();

joomla/components/com_test/controllers/test.php:

<?php defined( '_JEXEC' ) or die( 'Restricted access' ); 

class TestControllersTest extends JControllerBase
{
  public function execute()
  {

    $app = $this->getApplication();
    $document = JFactory::getDocument();

    $viewName = $app->input->getWord('view', 'test');       
    $viewFormat = $document->getType();     //html, raw etc.
    $layoutName = $app->input->getWord('layout', 'test2');

    $app->input->set('view', $viewName);

    // Register the layout paths for the view
    $paths = new SplPriorityQueue;
    $paths->insert(JPATH_COMPONENT . '/views/' . $viewName . '/tmpl', 'normal');

    $viewClass  = 'TestViews' . ucfirst($viewName) . ucfirst($viewFormat);
    $modelClass = 'TestModels' . ucfirst($viewName);

    $view = new $viewClass(new $modelClass, $paths);

    $view->setLayout($layoutName);

    echo $view->render();

    return true;
  }

}

joomla/components/com_test/models/test.php:

defined( '_JEXEC' ) or die( 'Restricted access' ); 

class TestModelsTest extends JModelBase
{

  function __construct()
  {
    parent::__construct(); 
  }

}

joomla/components/com_test/views/test/html.php:

<?php defined( '_JEXEC' ) or die( 'Restricted access' ); 

class TestViewsTestHtml extends JViewHtml
{
  function render()
  {
    return parent::render();
  }
}

joomla/components/com_test/views/test/tmpl/test.php:

<h1>This is the test layout for the test view</h1>

joomla/components/com_test/views/test/tmpl/test2.php:

<h1>This is the test2 layout for the test view</h1>

【问题讨论】:

    标签: php joomla joomla3.0 joomla-component


    【解决方案1】:

    Joomla 似乎不喜欢布局名称中的数字...我将 test2.php 更改为 testtwo.php,它运行良好。

    【讨论】:

      【解决方案2】:

      在新的 mvc 中,模型、视图、控制器等文件夹名称应该是单数的(与旧的 mvc 不同,它需要模型、视图、控制器)。同样,您的类名应该有单数段。

      【讨论】:

        猜你喜欢
        • 2013-04-08
        • 2013-04-27
        • 1970-01-01
        • 2014-01-11
        • 2014-03-27
        • 2012-04-12
        • 1970-01-01
        • 2016-11-12
        • 1970-01-01
        相关资源
        最近更新 更多