Custom Joomla Backend MVC Component

Print E-mail

In this article we discuss the outlines of building a custom backend Joomla component structured on the MVC model.

The details and complete code are covered in the members area

We make a component that will write text to a text file. The user will enter the text in the component administrator interface. We shall name the component mytext. We will use our localhost installation to develop the component.

Step 1

Make a folder called com_mytext in the admin components folder.

administrator/components /com_mytext


Step 2

Create the component entry file. This is a php file with the name admin.mytext

administrator/components/com_mytext/admin.mytext.php


This file invokes the controller class and performs the requested task

// Create the controller
$classname = 'mytextController'.$controller;
$controller = new $classname( );

// Perform the Request task
$controller->execute( JRequest::getVar( 'task' ) );


Step 3

Create the controller file

administrator/components/com_mytext/controller.php


Here we extend the standard Jcontroller class by adding specific functions we want to perform. For example we may wish to add an edit function.


class mytextController extends JController

function edit()
{
JRequest::setVar( 'view', 'mytext' );
JRequest::setVar( 'layout', 'form' );
JRequest::setVar('hidemainmenu', 1);

parent::display();
}


Step 4

Make a folder called models and the model php file

administrator/components/com_mytext/models


Here we create the file mytext.php

administrator/components com_mytext/models/mytext.php


Our model file will deal with the file handling. We extend the standard JModel class. In this case we will use PHP file handling routines to open our text file and write new text to the file


Example

class mytextModelmytext extends JModel

function getFile()
{
$file=JPATH_SITE.DS."mytext.txt";
$fh = fopen($file, 'r');
$this->_text=fread($fh, filesize($file));

fclose($fh);

return $this->_text;
}


Step 5

Create a folder views

administrator/components/com_mytext/views

Create the view file.

administrator/components/ com_mytext/views/view.html.php

The view file gets data from the model and passes it into the view output (template) file.


Step 6

Create a folder called tmpl

administrator/components com_mytext/views/tmpl


Create the template view file eg form.php

administrator/components/com_mytext/views/tmpl/form.php


The template view file displays the output and accepts input from the user. In our eaxmple we use an html form for this. The form will display the current text and allow the user to enter the new text. The form action method will invoke the corresponding action from the controller file.


Step 7

Create the install xml file

administrator/components/com_mytext/mytext.xml

Here we tell the Joomla installer what files exist for this component

In our example we will manually install this component.


Step 8

After testing your component locally, FTP the folders and files to the actual Joomla site


Step 9

Register the component in the database (jos_components)


That concludes this article on creating a custom back-end Joomla MVC component.

Comments (0)add comment

Write comment

security image
Write the displayed characters


busy
 

Subscribe To Our RSS

Stay informed: Please subscribe to our RSS feed to be informed about new Turn Key Web Solution tips, tutorials, special promotions and other goodies.


subscribe for rss feeds

Get Feed Via Email

You can get your feed directly to your email by feedburner. We respect your privacy and will never distribute your email address to anyone.

Contact Us

Joomsnap.com
6 Harbor Way #209
Santa Barbara, CA 93101
Phone: (800)605-4988
Click Here to E-Mail Us