Joomla component development tutorial _ PHP

Source: Internet
Author: User
This article mainly introduces the joomla component development method, and analyzes the structure, functions, and usage skills of the joomla component in the form of instances. For more information, see Joomla.

This article describes the knowledge of joomla component development. We will share this with you for your reference. The details are as follows:

Before coding, some files and folders need to be created and some query statements need to be run. You can not only create components, but also try different features without additional configuration. You can also see Joomla! Organization and access component method overview. Finally, you will add the toolbar like other components.

Joomla! Component Structure

Joomla! All components comply with the specified naming conventions. Each system component has a unique name. The name must not contain spaces. The code is divided into two folders. the folder starts with com _, followed by the component name. Therefore, you need to create two com_reviews folders with the same name, one under front-end components and the other under backend administrator/components. Joomla! The files whose names are unique and whose names end with the. php extension will be searched. Create a review. php file under components/com_reviews. Similarly, the admin. reviews. php file created at the backend must be prefixed with admin. and admin. reviews. php file created under administrator/components/com_reviews.

Execution component

Joomla! All front-end requests go through the index. php file in the root directory, and different components are loaded by setting the option variable of url get. Suppose your local joomla! The site address is http: // localhost/joomla/index. php. then, the address of the component you load should be http: // localhost/joomla/index. php? Option = reviews: Open the reviews. php file and add the following code:

<?phpdefined( '_JEXEC' ) or die( 'Restricted access' );echo '

Restaurant Reviews

';?>

You will see a similar page:

Image display is not provided for the time being. see Joomla! Extension development

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

Add the following code to the backend administrator/components/com_reviews/admin. reviews. php file:

<?phpdefined( '_JEXEC' ) or die( 'Restricted access' );echo 'Restaurant Reviews';?>

Browsing address:
Http: // localhost/joomla/administrator/index. php? Option = com_reviews, compare the output of the page:

Image display is not provided for the time being. see Joomla! Extension development

Joomla! Frontend and backend separation

Joomla! And their code makes the back-end part of the code well separated from the front-end part of the code, in some cases, such as database table class, the back-end will use some front-end files, but they are independent. When you do not allow backend functions to be mixed into front-end code, the security is enhanced. This is a very important feature with similar backend and front-end structures. The following figure shows Joomla! And the administrator folder:

Image display is not provided for the time being. see Joomla! Extension development

Note that the administrator folder has a similar structure with the root directory. It is important to differentiate them. Otherwise, you may put your code in the wrong position and fail to execute it unless you put them back in the correct position.

Register components in the database

You now know how to access front-end and back-end components. even though you can manually enter a URL to execute your code, you cannot accept it. If you have registered a component in the database, that is, adding a record to the components data table, you can use the navigation bar. Use the following SQL statement to register a 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', '');

The component name is declared here, which can contain spaces and punctuation. you can specify the frontend and backend links and backend component menu icons. When you create a basic directory and add files, some components are ready to be executed without writing any SQL statements. In this way, you can add a component link to the backend. you can also add a link at the appropriate position of the front-end without the need for a hardcoded URL. Refresh your backend page and drop down the component menu. you will see the sub-menu items of your component:

Image display is not provided for the time being. see Joomla! Extension development

Now that the component has been registered, you can create a link on the front-end and go
"Menu" | "Main Menu", click "new", select "Restaurant Reviews" from the page, and enter the link name as follows:

Image display is not provided for the time being. see Joomla! Extension development

Click "save" and go to the front-end. you should see the "Reviews" link:

Image display is not provided for the time being. see Joomla! Extension development

You can prepare your PHP skills to write components. Make sure that all front-end requests are sent through http: // localhost/joomla/index. php? Option = com_views. the backend request is sent through http: // localhost/joomla/administrator/index. php? Option = com_reviews.

Joomla! It is very flexible and allows you to do what you like. In this example, we will teach you to create a new component, and then design the toolbar, users, database classes, and libraries. once you understand how they work, these elements will save you a lot of time.

Create toolbar

In Joomla! All the core components in the backend are implemented with the same save, delete, edit, and release buttons. you can use these buttons in your components so that the administrator can have a seamless experience. First, create the toolbar.reviews.html. php file in the administartor/components/com_reviews folder and enter the code:

<?phpdefined( '_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::publishList();JToolBarHelper::unpublishList();JToolBarHelper::editList();JToolBarHelper::deleteList();JToolBarHelper::addNew();}}?>

Files that include output code are usually organized into classes, such as TOOLBAR_reviews. Each member function will display a different toolbar. The JtoolBarHelper class contains all functions for creating HTML elements in the toolbar. you can also add custom HTML elements. What you need to understand is that the toolbar is built with HTML tables. you may want to addLabel.

The toolbar is now defined, but you need to add some code to decide which buttons to display. Joomla! Files whose names start with the component name and end with. reviews. php are automatically loaded. Add the following code to the toolbar. reviews. php file under administrator/components/com_reviews:

<?phpdefined( '_JEXEC' ) or die( 'Restricted access' );require_once( JApplicationHelper::getPath( 'toolbar_html' ) );switch($task){case 'edit':case 'add':TOOLBAR_reviews::_NEW();break;default:TOOLBAR_reviews::_DEFAULT();break;}?>

This line contains require_once (). use the member function getPath () of the JapplicationHelper class to obtain the toolbar. reviews. php file path without including the component name. even if you change the component name, you do not need to modify the code or load the file normally.

Description:

You may want to know why the toolbar. reviews. php and toolbar.reviews.html. php files were created at the beginning. The preferred encoding style for component developers is to completely separate the processing logic from the output, so it is very easy to add features and share code with others later.

After toolbar. reviews. php loads files using the output class, you need to decide which toolbar to display. The requested variable $ task is automatically registered as a global variable and has a logical flow to direct the component. Refresh the backend page and enter the Restaurant Reviews component. then you should see the following toolbar:

Image display is not provided for the time being. see Joomla! Extension development

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

Image display is not provided for the time being. see Joomla! Extension development

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

Once the appropriate form is in the appropriate position, click "new" to see the second toolbar. since we do not have any forms on the backend, these toolbar buttons will not work. The next chapter will teach you how to make these buttons take effect.

Valid toolbar buttons

Joomla! You can use your own task and label to overwrite any buttons and input the first and second parameters to overwrite them. The following is Joomla! Effective buttons provided in the standard version:

Image display is not provided for the time being. see Joomla! Extension development

Note:

If you want to create a custom button that is the same as the core button, you can use the JtoolBarHelper member function custom () and pass the task, icon, mouse-over image, and text description as parameters.

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: info-contact@alibabacloud.com 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.