Joomla Component Development Primer Tutorial _php Example

Source: Internet
Author: User
Tags naming convention php and smarty template

This article tells the Joomla component development knowledge point. Share to everyone for your reference, specific as follows:

Before you encode, there are files and folders that need to be created and some query statements to run. Not only can you create components, but you can try different features without additional configuration. You can also see an overview of the joomla! organization and access component methods. Finally, you'll join the toolbar like any other component.

Structure of the joomla! component

All components of joomla! comply with the specified naming convention. Each system component has a unique name, and the name does not include spaces. The code is divided into two folders, and the folder begins with Com_, followed by the name of the component. So you want to create two com_reviews folders of the same name, one placed under the front components and the other under the back end administrator/components. When the component is loaded by the front end, joomla! will look for files that have been uniquely named by the component and ended with a. php extension. Create a review.php file under Components/com_reviews. Similarly, files built on the back end need to be preceded by admin. , the establishment of under Administrator/components/com_reviews.

Executing components

Joomla! front-end all requests are passed through the root directory of the index.php file, loading different components is by setting the URL get option variable. Assuming your local joomla! site address is http://localhost/joomla/index.php, then the address of the component you are loading should be http://localhost/joomla/index.php?option= Reviews, open the reviews.php file and add the following code:

defined (' _jexec ') or Die (' restricted access ');
Echo ' <div class= ' componentheading ' >restaurant reviews</div> ';
? >

You will see a similar page:

For the time being, please refer to the joomla! Extension development "

You may want to know what the purpose of the first call to the defined () function is to ensure that the code is not accessed directly through the components/com_reviews/reviews.php.

Add the following code to the administrator/components/com_reviews/ file at the back end:

defined (' _jexec ') or Die (' restricted access ');
Echo ' restaurant reviews ';
? >

Browse Address:
Http://localhost/joomla/administrator/index.php?option=com_reviews, comparing the output of the page:

For the time being, please refer to the joomla! Extension development "

Separation of the front and rear end of joomla!

All of the components of joomla!, whose code separates the backend part from the front-end code, in some cases, such as database table classes, where the backend uses some of the front-end files, but they are independent. Security is enhanced when you do not allow the backend function to mix with the front-end code. This is a very important feature at the same time as the structure of the back end and the front end are similar. The following shows the joomla! 's root directory and the chart of the Administrator folder expansion:

For the time being, please refer to the joomla! Extension development "

Note that the Administrator folder has a similar structure to the root directory. It's important to distinguish between them, otherwise you might put your code in the wrong place and fail unless you put it back in the right place.

Registering components in a database

You now know how to access front-end and back-end components, although each time you can manually enter the URL to execute your code, but your users you can not accept. If you register a component in a database, add a record to the components datasheet, you can use the navigation. Use the following SQL statement to register the component:

INSERT into jos_components (name, link, Admin_menu_link,
admin_menu_alt, ' option ', admin_menu_img, params)
VALUES (' Restaurant reviews ', ' option=com_reviews ', '
option=com_reviews ', ' Manage reviews ', ' com_reviews ',
' Js/themeoffice/component.png ', ');

This declares the name of the component, can include spaces and punctuation, you can specify a link to the front-end and back-end, and you can specify the icon for the back-end component menu. When you create a basic directory and add files, some components are ready to be executed without having to write any SQL statements. This allows you to add a link to a component at the back end, or you can add a link to the appropriate location on the front end without having to hard-code the URL. Refresh your back-end page, drop down the Component menu, and you'll see the submenu items for your component:

For the time being, please refer to the joomla! Extension development "

Now that the components have been registered, you can create links on the front end and go to
"Menu" | Main menu, click the New button, select Restaurant reviews from the page, and then enter the link name as follows:

For the time being, please refer to the joomla! Extension development "

Click "Save" and then go to the front end and you should see the "Reviews" link:

For the time being, please refer to the joomla! Extension development "

You can prepare your PHP tips to start writing components. Also make sure that all front-end requests are passed through the http://localhost/joomla/index.php?option=com_views, and that the backend request passes through the Http://localhost/joomla/administrator /index.php?option=com_reviews.

Joomla! is very flexible and allows you to do the things you like to do. In this case, we'll teach you to start with a new component, then design toolbars, users, database classes, libraries, and so on, and once you understand how they work, these elements will save you a lot of time.

Create a toolbar

At the back end of the joomla!, all of the core components implement the same buttons for saving, deleting, editing, and publishing items, and you can use these buttons in your component so that administrators can have a seamless experience. First, create the file under the Administartor/components/com_reviews folder and enter the code:

defined (' _jexec ') or Die (' restricted access ');
Class Toolbar_reviews {
function _new () {
jtoolbarhelper::save ();
Jtoolbarhelper::apply ();
Jtoolbarhelper::cancel ();
function _default () {
jtoolbarhelper::title (jtext::_ (' restaurant reviews '),
' generic.png ');
Jtoolbarhelper::p ublishlist ();
Jtoolbarhelper::unpublishlist ();
Jtoolbarhelper::editlist ();
Jtoolbarhelper::d eletelist ();
Jtoolbarhelper::addnew ();
? >

Files that include the output code are usually organized into classes, like the toolbar_reviews here. Each member function will display a different toolbar. The Jtoolbarhelper class contains all the functions that create HTML elements for toolbars, and you can also add custom HTML. What you need to know is that the toolbar is built with HTML tables, and you may want to add <td> tags to your navigation.

The toolbar is now defined, but you need to add some code to decide which buttons to display. joomla! automatically loads files that start with the component name and end with. reviews.php. Add the following code to the file under Administrator/components/com_reviews:

defined (' _jexec ') or Die (' restricted access ');
Require_once (Japplicationhelper::getpath (' toolbar_html '));
Switch ($task)
' edit ': Case
' Add ':
toolbar_reviews::_new ();
Toolbar_reviews::_default ();
? >

This line contains require_once (), using the member function GetPath () of the Japplicationhelper class to get the path to the file without including the name of the component, even if you change the name of the component. You do not need to modify the code or you can load the file normally.

Description :

You may wonder why you created the two and files at the outset. The preferred coding style for component developers is to completely separate the processing logic from the output, so it is easy to add features and share code with others later. you need to decide which toolbar to display after loading the file with the output class. The requested variable $task automatically registers as a global variable and has a logical flow to guide the component. Now refresh the back-end page and enter the restaurant reviews component, and then you should be able to see the following toolbar:

For the time being, please refer to the joomla! Extension development "

To see the other toolbars, add &task=add to the URL and reload the page, you should see the following toolbar:

For the time being, please refer to the joomla! Extension development "

When your users want to use your components, they certainly do not want to manually add task variables after the URL, so how can they use the second toolbar? Each toolbar corresponds to a different task, and when a button is clicked, the relevant task is added to the form and automatically submitted.

Once the appropriate form is in place, clicking the New button will see the second toolbar, and since we don't have any forms at the back end, these toolbar buttons won't work. The next chapter will show you how to get these buttons in effect.

A valid toolbar button

Joomla! allows you to overwrite any button with your own task and label, passing in the first and second arguments respectively. The following are valid buttons provided by the joomla! Standard version:

For the time being, please refer to the joomla! Extension development "


If you want to create a custom button like the core button, you can use the Jtoolbarhelper member function custom () and pass the task, icon, mouse-over picture, and text description as parameters.

More about Joomla interested readers can view the site topics: "The introduction of YII framework and common skills summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "thinkphp Common Methods Summary", "PHP object-oriented Program Design Introductory Course" , PHP string (String) Usage summary, "PHP+MYSQL Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article is helpful to everyone based on the Joomla program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.