Creating Joomla component called mvc

Posted: November 9, 2010 in Joomla, MySql, PHP
Tags: , ,
Today I shall create component called mvc. The tutorial is discussed below step by step :
  1. Create folder administrator/components/com_mvc.
  2. Create file administrator/components/com_mvc/admin.mvc.php. This is standard Joomla file that will be launched on access to the component.

    File listing: admin.mvc.php

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once (JPATH_COMPONENT.DS.'controller.php');
if($controller = JRequest::getVar('controller')) {
require_once (JPATH_COMPONENT.DS.'controllers'.
DS.$controller.'.php');
}
$classname = 'MvcController'.
ucfirst($controller);
$controller = new $classname( );
$controller->execute( JRequest::getVar('task'));
$controller->redirect();
?>

Line 2 is to protect document from direct access. line 4 to require default controller. This controller will render pages if controller GET/POST variable is not set. 6-8 to include other controller class if controller GET/POST variable was set. 10-11 create new controller class. 13-14 execute controller or render page according task

This file is quite simple. First it includes controller, then create class, then execute.

3. Create administrator/components/com_mvc/controller.php. This is controller we includes in line 4 of admin.mvc.php. This controller will be launched if there was not any GET/POST controller variable found.

File listing: controller.php

<?php
jimport('joomla.application.component.controller');
class MvcController extends JController{
function display() {
parent::display();
}
}
?>

2d line to include Joomla API controller class for extending new one created in line 4. Function display render default view. More about that later.Basically that is all we need. If instead parent::display(); we put echo “test this”; we will see this on the main page of the component. But let’s create fully structured MVC component. For that we need to create View for this controller.Default view should have the same name as the component.

4.Create folder administrator/components/com_mvc/views.

5.Create folder administrator/components/com_mvc/views/mvc. To create default view we need to name this folder as we named component.

6.Create file administrator/components/com_mvc/view/mvc/view.html.php. This file will have view class. We do not use “mvc” word in the name of this file. Every view class has different folder but the same file name. it is view.html.phpFile listing: views/mvc/view.html.php

<?php
jimport( 'joomla.application.component.view');
class MvcViewMvc extends JView
{
function display($tpl = null)
{
JToolBarHelper::title(JText::_( 'MVC Main' ), 'generic.png');
parent::display($tpl);
}
}
?>

Line 2 we include Joomla API View class to extend new one we create in line 3. 5-10 create function display(). This function is called by default, by MvcController class if not task called.

I used JToolBarHelper::title() to display tile. Here you can also use other JToolBarHelper methods. Also here you can make DB queries and prepare HTML elements to render such as Published Yes/No radio button set, Public/Registered/Special access select list, pageNav class to create navigation and other. Please, note the difference. In this view class we use Models(tables) and SQL queries to get information to create HTML elements to render. But in controller we make SQL queries and use models to control and operate records. Something like delete, publish, save, …

Function display() will be called by default if there is no GET/POST task variable set. parent::display($tpl); will call default template if POST/GET layout variable is not set. So let’s create default template.

7. Create file administrator/components/com_mvc/view/mvc/tmpl/default.php.

File listing: views/mvc/tmpl/default.php

<h1>MVC Main</h1>;
<a href="index.php?option=com_mvc&amp;controller=list">List something</a>;

This is HTML document. This template will display H1 header and link to other controller. This is complete MVC minimum component. We only lack Models.

Please, save all files and try to call your new component. Login to Joomla admin and put to address line /index.php?option=com_mvc. Before you continue please, be sure all code works fine till this point.

Everything you will read below just to explain some more things. How to control different tasks and different Views within one Controller. For that, let’s create another section that will be controlled by Controller list. First link to controller is without task so we will call default View again and create some more links with tasks to call other Views.

8.Lets imagine we clicked on index.php?option=com_mvc&controller=list link. That means we enable following lines in admin.mvc.php.

File listing: views/mvc/tmpl/default.php

if($controller = JRequest::getVar('controller')) {
require_once (JPATH_COMPONENT.DS.'controllers'.
DS.$controller.'.php');
}

That means we need to create file administrator/components/com_mvc/controllers/list.php.

9.Create it.

File listing: controllers/list.php

<?php
jimport('joomla.application.component.controller');
class MvcControllerList extends JController
{
function __construct()>
{
parent::__construct();
}
function display() {
JRequest::setVar('view', 'list');
parent::display();
}
}
?>

JRequest::setVar(‘view’, ‘list‘); tells this controller to look for View in views/list folder. If we skip or delete this line, this controller class will look for View in views/mvc default folder. So if we told controller to look for View in list folder, let’s create it.

10.Create folder administrator/components/com_mvc/view/list

11.Create folder administrator/components/com_mvc/view/list/tmpl

12.Create file administrator/components/com_mvc/view/list/view.html.php

File listing: views/list/view.html.php

<?php
jimport( 'joomla.application.component.view');
class MvcViewList extends JView
{
function display($tpl = null)
{
JToolBarHelper::title(JText::_( 'List' ), 'generic.png' );
parent::display($tpl);
}
}
?>

I won’t explain this file. It is the same as views/mvc/view.html.php, only name of the class is different.

13. Create file administrator/components/com_mvc/view/list/tmpl/default.php

File listing: views/list/tmpl/default.php

<h1>This is Default.php</h1>
<P>This file launch automatically is nothing
passed to JRequest::setVar('layout', 'something');</P>
<a href="index.php?option=com_mvc&amp;controller=list&amp;task=test">
test</a><BR>
<a href="index.php?option=com_mvc&amp;controller=list&amp;task=new_task">
new_task</a><BR>

14.That is it. It should work ok now. I mean you can easily navigate from MVC component homepage to list controller homepage.

But this file will display few links that have different tasks within same Controller. Here is how you can handle them. Start with task=new_task.

15. Create function new_task in File controllers/list.php.

File listing after adding function: controllers/list.php

<?php
jimport('joomla.application.component.controller');
class MvcControllerList extends JController
{
function __construct()
{
parent::__construct();
}
function display() {
JRequest::setVar('view', 'list');
parent::display();
}
function new_task()
{
JRequest::setVar('view', 'list');
JRequest::setVar('layout','newtask');
parent::display();
}
}
?>

We created function new_task() in line 13-18, that will be called if GET/POST task variable equal to new_task. JRequest::setVar(‘view’, ‘list’); tells controller to look for View in views/list/ folder. JRequest::setVar(‘layout’,’newtask’); tells controller that View should display newtask template or really newtask.php. Let’s create it.

16. Create file administrator/components/com_mvc/view/list/tmpl/newtask.php

File listing: views/list/tmpl/newtask.php

<h1>This is new task</h1>
<P>This task1, task2, new_task triger the same function </P>
<a href="index.php?option=com_mvc&amp;controller=list">Go back</a>

This file displays some text and link back to list controller homepage. Save all files and try it. Try to navigate to list controller homepage and click on new_task link. If you see text of above listing you are done. Congratulation. You have just created new task.

17.One more thins to know. It is possible to call same method with different tasks. For that add $this->registerTask( ‘test’ , ‘new_task’ ); to construct method of controllers/list.php.

Final listing: controllers/list.php

<?php
jimport('joomla.application.component.controller');
class MvcControllerList extends JController
{
function __construct()
{
parent::__construct();
$this->registerTask( 'test' , 'new_task' );
}
function display() {
JRequest::setVar('view', 'list');
parent::display();
}
function new_task()
{
JRequest::setVar('view', 'list');
JRequest::setVar('layout','newtask');
parent::display();
}
}
?>

Save all files and now try click test link on list controller homepage.

18.Later I’ll write a lesson on how to use Models and we will create simple guestbook component.

Enjoy. 😀

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s