我正在尝试让我的Joomla 3组件呈现url中指定的布局,但我无法弄清楚为什么它坚持显示测试.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>
看来Joomla不喜欢布局名称中的数字...我将 test2.php 更改为 testtwo.php它工作正常。
在新的 mvc 中,模型、视图、控制器等文件夹名称应该是单数的(不像旧的 MVC 那样体验模型、视图、控制器)。同样,您的类名应该有单数段。