我正在尝试让我的Joomla 3组件渲染网址中指定的布局,但我无法弄清楚为什么它坚持显示 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>
答案 0 :(得分:1)
似乎Joomla不喜欢布局名称中的数字......我将test2.php更改为testtwo.php并且工作正常。
答案 1 :(得分:1)
在新的mvc中,模型,视图,控制器之类的文件夹名称应该是单一的(不像旧的mvc,它可以用于模型,视图,控制器)。同样,你的类名应该有单一的段。