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