Can't Change layout in Custom Joomla 3 Component

1.6k views Asked by At

I'm trying to get my Joomla 3 component to render the layout specified in the url but I can't figure out why it insists on displaying the test.php layout. All relevant code below and the url I'm using is:

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

Maybe I'm doing this completely wrong but here's my code so far:

Code:

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>
2

There are 2 answers

0
doovers On BEST ANSWER

It appears that Joomla doesn't like numbers in the layout names... I changed test2.php to testtwo.php and it worked fine.

0
Elin On

In the new mvc the folder names like model, view, controller are expected to be singular (unlike old mvc which expece models, views, controllers). Likewise your classnames should have singular segments.