1

我试图让我的 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>
4

2 回答 2

1

看来 Joomla 不喜欢布局名称中的数字...我将 test2.php 更改为 testtwo.php 并且效果很好。

于 2013-09-07T04:17:50.637 回答
1

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

于 2013-09-09T11:40:07.897 回答