how to make component in joomla 1.5

Upload: mitesh-shah

Post on 09-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 How to make Component In Joomla 1.5

    1/7

    How to make Component In Joomla 1.5 ?

    Creating a Component

    For our basic component, we only require five files:

    site/hello.php - this is the entry point to our component site/controller.php - this file contains our base controller site/views/hello/view.html.php - this file retrieves the necessary data and pushes it

    into the template site/views/hello/tmpl/default.php - this is the template for our output hello.xml - this is an XML (manifest) file that tells Joomla! how to install our

    component.

    Remember that the filename for the entry point must have the same name of thecomponent. For example, if you call your component "Very Intricate Name Component",at install time (see below in the hello.xml section) Joomla! will create the foldercom_veryintricatenamecomponent and the entry point php file must be namedveryintricatenamecomponent.php otherwise it will not work. Be aware that use of somespecial characters, notibly the underscore '_', may have special meaning in Joomla andshould be avoided in component names or files.

    The site directory here is for the parts of the component which are installed in the frontend site.

    Naming conventions

    Main article:Naming conventions

    At this point it is important to say that some words are reserved for using in componentor its class names, and violating some of that will produce a hard for debugging error.One of them is "view" (in any character case) for view class (subclass of JView) andcontroller class (subclass of JController), because view class need to have first part ofname the same as controller class name, and component name (although violating of lastone won't produce an error, it's just a useful convention).

    All filenames and foldernames for models, views and controllers must be lower-case inorder to operate well on Unix/Linux-systems.

    http://docs.joomla.org/Naming_conventionshttp://docs.joomla.org/Naming_conventions
  • 8/7/2019 How to make Component In Joomla 1.5

    2/7

    Creating the Entry Point

    This recently added article requires a review

    Joomla! is always accessed through a single point of entry: index.php for the SiteApplication or administrator/index.php for the Administrator Application. Theapplication will then load the required component, based on the value of 'option' in theURL or in the POST data. For our component, the URL would be:

    index.php?option=com_hello&view=hello

    This will load our main file, which can be seen as the single point of entry for ourcomponent: components/com_hello/hello.php.

    The code for this file is fairly typical across components.

    site/hello.php:

  • 8/7/2019 How to make Component In Joomla 1.5

    3/7

    // Redirect if set by the controller$controller->redirect();

    The first statement is a security check.

    JPATH_COMPONENT is the absolute path to the current component, in our casecomponents/com_hello. If you specifically need either the Site component or theAdministrator component, you can use JPATH_COMPONENT_SITE orJPATH_COMPONENT_ADMINISTRATOR.

    DS is the directory separator of your system: either '/' or '\'. This is automatically set bythe framework so the developer doesn't have to worry about developing different versionsfor different server OSs. The 'DS' constant should always be used when referring to fileson the local server.

    After loading the base controller, we check if a specific controller is needed. In this

    component, the base controller is the only controller, but we will leave this conditionalcheck "in place" for future use.

    JRequest:getWord() finds a word variable in the URL or the POST data. So if our URL isindex.php?option=com_hello&controller=controller_name, then we can retrieve ourcontroller name in our component using: echo JRequest::getWord('controller');

    Now we have our base controller 'HelloController' in com_hello/controller.php, and, ifneeded, additional controllers like 'HelloControllerController1' incom_hello/controllers/controller1.php. Using this standard naming scheme will makethings easy later on: '{Componentname}{Controller}{Controllername}'

    After the controller is created, we instruct the controller to execute the task, as defined inthe URL: index.php?option=com_hello&task=sometask. If no task is set, the default task'display' will be assumed. When display is used, the 'view' variable will decide what willbe displayed. Other common tasks are save, edit, new...

    The controller might decide to redirect the page, usually after a task like 'save' has beencompleted. This last statement takes care of the actual redirection.

    The main entry point (hello.php) essentially passes control to the controller, whichhandles performing the task that was specified in the request.

    Note that we don't use a closing php tag in this file: ?>. The reason for this is that we willnot have any unwanted whitespace in the output code. This is default practice sinceJoomla! 1.5, and will be used for all php-only files.

  • 8/7/2019 How to make Component In Joomla 1.5

    4/7

    Creating the Controller

    Our component only has one task - greet the world. Therefore, the controller will be verysimple. No data manipulation is required. All that needs to be done is the appropriateview loaded. We will have only one method in our controller: display(). Most of the

    required functionality is built into the JController class, so all that we need to do isinvoke the JController::display() method.

    The code for the base controller site/controller.php is:

  • 8/7/2019 How to make Component In Joomla 1.5

    5/7

    The JController::display() method will determine the name of the view and layout fromthe request and load that view and set the layout. When you create a menu item for yourcomponent, the menu manager will allow the administrator to select the view that theywould like the menu link to display and to specify the layout. A view usually refers to aview of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event).

    A layout is a way that that view is organized.

    In our component, we will have a single view called hello, and a single layout (default).

    Creating the View

    The task of the view is very simple: It retrieves the data to be displayed and pushes it intothe template. Data is pushed into the template using the JView::assignRef method. (Note:The key (the first argument) passed to the assignRef method cannot be preceded by an underscore i.e. $this->assignRef('_greeting',$greeting). Doing so will cause the assignRef method to return false and yourvariable will not be pushed into the template.)

    The code for the view at site/views/hello/view.html.php:

  • 8/7/2019 How to make Component In Joomla 1.5

    6/7

    Creating the Template

    Joomla! templates/layouts are regular PHP files that are used to layout the data from theview in a particular manner. The variables assigned by the JView::assignRef method canbe accessed from the template using $this->{propertyname} (see the template code below

    for an example).

    Our template is very simple: we only want to display the greeting that was passed in fromthe view - this file is:site/views/hello/tmpl/default.php:

    Wrapping It All Up - Creating the hello.xml File

    It is possible to install a component manually by copying the files using an FTP clientand modifying the database tables. It is more efficient to create a package file that willallow the Joomla! Installer to do this for you. This package file contains a variety ofinformation:

    basic descriptive details about your component (i.e. name), and optionally, adescription, copyright and license information.

    a list of files that need to be copied.

    optionally, a PHP file that performs additional install and uninstall operations. optionally, an SQL file which contains database queries that should be executed

    upon install/uninstall

    The format of the XML file at hello.xml is as follows:

    Hello2007-02-22John [email protected]://www.example.orgCopyright InfoLicense Info1.01Description of the component ...

  • 8/7/2019 How to make Component In Joomla 1.5

    7/7

    controller.phphello.phpindex.htmlviews/index.htmlviews/hello/index.htmlviews/hello/view.html.phpviews/hello/tmpl/default.phpviews/hello/tmpl/index.html

    Hello World!

    hello.phpindex.html

    Put this xml file, also called manifest, in the root of your package (because theinstaller will take its path as the root path for all other files).Don't include itself under ...

    You may have noticed the manifest source above mentioned files we have not discussed.

    These are the index.html files and the admin files. An index.html file is placed in eachdirectory to prevent prying users from getting a directory listing. If there is no index.htmlfile, some web servers will list the directory contents. This is often undesirable. Thesefiles have the simple line:

    It will simply display a blank page.

    The hello.php file in the admin folder is the entry point for the our component's adminsection. Since our component has no administrator needs (yet), this file will have the

    same content as the index.html files for now.

    If you've followed along, you can visit URL index.php?option=com_hello to see yourwork.

    Url : http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1

    http://docs.joomla.org/Image:Info_non-talk.png