Joomla Component Development Introductory Tutorial _php instance

Source: Internet
Author: User
Tags getting started with php smarty template
This article describes the Joomla component development knowledge points. Share to everyone for your reference, as follows:

Before you encode, there are some 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 methods of accessing components. Finally, you'll join the toolbar like any other component.

Structure of the joomla! component

All components of the joomla! adhere to 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. Therefore, you will create two com_reviews folders of the same name, one to the front-end components and the other to the backend administrator/components. When the component is loaded by the front end, joomla! will look for files that are uniquely named by the component and end with the. php extension. Build the review.php file under Components/com_reviews. Similarly, files created on the backend need to be preceded by admin. To establish admin.reviews.php under the administrator/components/com_reviews.

Execution components

joomla! front-End all requests go through the root directory of the index.php file, loading different components by setting the URL GET to the option variable. Assuming that 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:

<?phpdefined (' _jexec ') or Die (' Restricted access '); Echo ' Restaurant reviews ';? >

You will see a similar page:

For the time being, do not provide a picture display, please refer to "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 components/com_reviews/reviews.php.

Add the following code to the back-end administrator/components/com_reviews/admin.reviews.php file:

<?phpdefined (' _jexec ') or Die (' Restricted access '); Echo ' Restaurant reviews ';? >

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

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

joomla! separation of front and rear end

All of the components of joomla!, whose code makes the backend part well separated from the front-end part of the code, in some cases, such as database table classes, the backend uses some files from the front end, but they are independent. Security is enhanced when you don't let back-end functions mix with the front-end code. This is a very important feature at the same time that the backend and front-end structures are similar. The following shows the charts for the root and administrator folders of joomla!:

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

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

Registering components in the database

You now know how to access the front-end and back-end components, although each time you can execute your code by manually entering the URL, your users are not acceptable to you. If you register a component in a database, that is, add a record to the components table, you can use 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, which can include spaces and punctuation, and can specify links to the front-end and back-end, which can be used to specify the icon for the backend component menu. When you create a basic directory and add a file, some components are ready to be executed without having to write any SQL statements. This allows you to add a link to a component in the backend, or to add a link at the appropriate location in the front-end without requiring a hard-coded URL. Refresh your backend page, drop-down component menu, and you will see the submenu items of your component:

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

Now that the component is registered, you can create a link on the front end to
"Menu" | Main menu, then click the New button, select "Restaurant reviews" from this page and enter the link name as follows:

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

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

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

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

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

Creating toolbars

At the back end of the joomla!, all the core components implement the same buttons for saving, deleting, editing, and publishing items, and you can use them in your components so that the administrator has a seamless experience. First, create the toolbar.reviews.html.php file under 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::p ublishlist (); Jtoolbarhelper::unpublishlist (); Jtoolbarhelper::editlist (); Jtoolbarhelper::d eletelist (); Jtoolbarhelper::addnew ();}}? >

Files that include output codes 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 toolbar HTML elements, and you can also add custom HTML. What you need to understand is that the toolbar is built with HTML tables, and you might want to add tags to your navigation.

Toolbars are now defined, but you need to add some code to decide which buttons to display. joomla! automatically loads files that begin with the component name and end with. reviews.php. 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 ')); s Witch ($task) {case ' edit ': Case ' Add ': toolbar_reviews::_new (); Break;default:toolbar_reviews::_default (); break;}? >

This line contains require_once (), using the member function of the Japplicationhelper class GetPath () to get the path to the toolbar.reviews.php 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 the first two files were created for toolbar.reviews.php and toolbar.reviews.html.php. The coding style preferred by component developers is to completely separate the processing logic from the output, so it's easy to add features and share the code with others later.

toolbar.reviews.php you load a file with an 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 that directs the component. Now refresh the backend page, go to the Restaurant reviews component, and you should be able to see the following toolbars:

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

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

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

When your users want to use your component, they certainly don't want to manually add a task variable 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 associated 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 on the back end, these toolbar buttons won't work. The next chapter will teach you how to make these buttons effective.

A valid toolbar button

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

For the time being, do not provide a picture display, please refer to "joomla! Extension development "

Description

If you want to create a custom button like a 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 related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "thinkphp Common Methods Summary", "PHP object-oriented Programming introduction Tutorial" , "PHP String Usage Summary", "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"

It is hoped that this article is helpful to everyone based on Joomla program design.

  • Related Article

    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.