Difference between revisions of "Developing an MVC Component/Adding a menu type to the site part"
From Joomla! Documentation
< J3.x:Developing an MVC Component
Betweenbrain (talk | contribs) |
(Marked this version for translation) |
||
(54 intermediate revisions by 17 users not shown) | |||
Line 1: | Line 1: | ||
− | {{:J3.1:Developing | + | <noinclude><languages /></noinclude> |
+ | {{:J3.1:Developing an MVC Component/<translate><!--T:1--> | ||
+ | en</translate>}} | ||
− | + | <translate>==Note== <!--T:19--></translate> | |
+ | <translate><!--T:20--> | ||
+ | * This tutorial is part of the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction|Developing a MVC Component for Joomla! 3.x]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate> | ||
− | + | <translate><!--T:21--> | |
+ | * You can follow the steps below to add a menu item to the Hello World! component, or you can directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-3-adding-a-site-menu.zip archive]</translate> | ||
− | + | <translate><!--T:64--> * You can watch a video associated with this step in the tutorial at [https://www.youtube.com/watch?v=f51yt689pWk Step 3, Adding a Menu Part].</translate> | |
− | |||
− | = | + | {{#widget:YouTube|id=f51yt689pWk}} |
− | + | <translate> | |
+ | == Adding a Menu Item to Hello World == <!--T:22--> | ||
+ | </translate> | ||
+ | <translate><!--T:23--> | ||
+ | In this article we will cover how to add a menu item to a basic Joomla! component. For this example we will be continuing our work on the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component| Hello World!]] component.</translate> | ||
− | + | <translate><!--T:24--> | |
+ | There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.</translate> | ||
+ | {| border=1 | ||
+ | | 1 | ||
+ | | <translate><!--T:25--> | ||
+ | Manually add the files into</translate> <tt><path_to_joomla>/</tt> | ||
+ | |- | ||
+ | | 2 | ||
+ | | <translate><!--T:26--> | ||
+ | Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install</translate> | ||
+ | |- | ||
+ | |3 | ||
+ | | <translate><!--T:27--> | ||
+ | Update using the Joomla! Extension Manager and an [[S:MyLanguage/Deploying_an_Update_Server| Update Server]]</translate> | ||
+ | |} | ||
− | + | <translate><!--T:28--> | |
+ | To add a menu item you will need to navigate to <tt>com_helloworld</tt>, which is the original directory we made for our component. You must use the updated directory structure from the last [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part | tutorial]]. Using your preferred file manager, create or update the following files; as you create or update the files, add the source code for each file which is found in [[#File Details|File Details]].</translate> | ||
− | + | {| border=1 | |
+ | | 1 | ||
+ | | <translate><!--T:29--> | ||
+ | Create:</translate> [[#site/views/helloworld/tmpl/default.xml| default.xml]] | ||
+ | | <tt><path_to_com_helloworld/site/views/helloworld/tmpl/default.xml<tt> | ||
+ | |- | ||
+ | | 2 | ||
+ | | <translate><!--T:30--> | ||
+ | Update:</translate> [[#helloworld.xml| helloworld.xml]] | ||
+ | | <tt><path_to_com_helloworld/helloworld.xml</tt> | ||
+ | |} | ||
− | + | <translate> | |
+ | === Updating the Hello World! Component === <!--T:31--> | ||
+ | </translate> | ||
+ | <translate><!--T:32--> | ||
+ | To update the Hello World! Component in the Joomla! website, please follow the same steps for the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component#Installing the Hello World! Component |original installation]].</translate> | ||
+ | <translate><!--T:33--> | ||
+ | After the component has been updated, we will be able to add a Menu Item to it. This will allow us to access our component a menu rather than having to remember what to type into the address bar. We'll do this using the Menu Manager of Joomla!.</translate> | ||
− | + | <translate><!--T:34--> | |
− | + | *Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <tt><yoursite>/joomla/administrator/index.php</tt>. For this example we will navigate to <tt>localhost/joomla/administrator/index.php</tt>.</translate> | |
+ | <translate><!--T:35--> | ||
+ | *Add a new menu item by clicking on Menu {{rarr}} <existing menu> {{rarr}} Add New Menu Item. A new screen will appear.</translate> | ||
+ | <translate><!--T:36--> | ||
+ | *In the "Menu Title" field, enter "Hello World!"</translate> | ||
+ | <translate><!--T:37--> | ||
+ | *In the "Menu Type" field, click on the "Select" button and choose Hello World! {{rarr}} Hello World from the selection screen.</translate> | ||
+ | <translate><!--T:38--> | ||
+ | *Click Save & Close</translate> | ||
+ | <translate> | ||
+ | <!--T:65--> | ||
+ | You should now be able to access the component through the menu that you just created.</translate> | ||
− | < | + | <translate> |
− | + | == File Details == <!--T:40--> | |
− | <source lang="xml | + | </translate> |
+ | {{vanchor|site/views/helloworld/tmpl/default.xml}} | ||
+ | <source lang="xml> | ||
<?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||
<metadata> | <metadata> | ||
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE"> | <layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE"> | ||
− | <message> | + | <message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message> |
− | |||
− | |||
</layout> | </layout> | ||
</metadata> | </metadata> | ||
</source> | </source> | ||
− | |||
− | |||
− | + | {{vanchor|helloworld.xml}} | |
− | + | <source lang="xml" highlight="13"> | |
− | < | + | <?xml version="1.0" encoding="utf-8"?> |
− | + | <extension type="component" version="3.0" method="upgrade"> | |
− | |||
− | <extension type="component" version=" | ||
<name>Hello World!</name> | <name>Hello World!</name> | ||
<!-- The following elements are optional and free of formatting constraints --> | <!-- The following elements are optional and free of formatting constraints --> | ||
− | <creationDate> | + | <creationDate>January 2018</creationDate> |
<author>John Doe</author> | <author>John Doe</author> | ||
<authorEmail>john.doe@example.org</authorEmail> | <authorEmail>john.doe@example.org</authorEmail> | ||
Line 59: | Line 105: | ||
<description>Description of the Hello World component ...</description> | <description>Description of the Hello World component ...</description> | ||
− | <update> <!-- Runs on update; New | + | <update> <!-- Runs on update; New since J2.5 --> |
<schemas> | <schemas> | ||
<schemapath type="mysql">sql/updates/mysql</schemapath> | <schemapath type="mysql">sql/updates/mysql</schemapath> | ||
Line 78: | Line 124: | ||
<administration> | <administration> | ||
<!-- Administration Menu Section --> | <!-- Administration Menu Section --> | ||
− | <menu>Hello World!</menu> | + | <menu link='index.php?option=com_helloworld'>Hello World!</menu> |
<!-- Administration Main File Copy Section --> | <!-- Administration Main File Copy Section --> | ||
<!-- Note the folder attribute: This attribute describes the folder | <!-- Note the folder attribute: This attribute describes the folder | ||
Line 91: | Line 137: | ||
</files> | </files> | ||
</administration> | </administration> | ||
− | |||
</extension> | </extension> | ||
</source> | </source> | ||
− | |||
− | == | + | <translate> |
+ | == Code Explanation == <!--T:41--> | ||
+ | </translate> | ||
+ | <translate><!--T:42--> | ||
+ | In case you were curious as to why this works the way it does.</translate> | ||
− | + | === default.xml === | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <translate><!--T:43--> | |
− | < | + | Note - For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.</translate> |
− | == | + | === helloworld.xml=== |
− | + | <source lang="xml"> | |
+ | <version>0.0.3</version> | ||
+ | </source> | ||
− | + | <translate><!--T:44--> | |
+ | Updates the version number.</translate> | ||
− | == | + | <translate> |
− | + | == Component Contents == <!--T:45--> | |
− | + | </translate> | |
− | + | <translate><!--T:46--> | |
− | + | At this point in the tutorial, your component should contain the following files:</translate> | |
+ | {| border=1 | ||
+ | | 1 | ||
+ | | ''[[#helloworld.xml|helloworld.xml]]'' | ||
+ | | <translate><!--T:47--> | ||
+ | this is an XML (manifest) file that tells Joomla! how to install our component.</translate> | ||
+ | |- | ||
+ | | 2 | ||
+ | | ''[[#site/helloworld.php|site/helloworld.php]]'' | ||
+ | | <translate><!--T:48--> | ||
+ | this is the site entry point to the Hello World! component</translate> | ||
+ | |- | ||
+ | | 3 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]'' | ||
+ | | <translate><!--T:49--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 4 | ||
+ | | ''[[#site/controller.php|site/controller.php]]'' | ||
+ | | <translate><!--T:50--> | ||
+ | file representing the controller</translate> | ||
+ | |- | ||
+ | | 5 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]'' | ||
+ | | <translate><!--T:51--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 6 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]'' | ||
+ | | <translate><!--T:52--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 7 | ||
+ | | ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]'' | ||
+ | | <translate><!--T:53--> | ||
+ | file representing the view</translate> | ||
+ | |- | ||
+ | | 8 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]'' | ||
+ | | <translate><!--T:54--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 9 | ||
+ | | ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]'' | ||
+ | | <translate><!--T:55--> | ||
+ | the default view</translate> | ||
+ | |- | ||
+ | | 10 | ||
+ | | ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]'' | ||
+ | | <translate><!--T:56--> | ||
+ | file that adds menu item</translate> | ||
+ | |- | ||
+ | | 11 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]'' | ||
+ | | <translate><!--T:57--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 12 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]'' | ||
+ | | <translate><!--T:58--> | ||
+ | this is the administrator entry point to the Hello World! component</translate> | ||
+ | |- | ||
+ | | 13 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]'' | ||
+ | | <translate><!--T:59--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 14 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]'' | ||
+ | | <translate><!--T:60--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 15 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]'' | ||
+ | | <translate><!--T:61--> | ||
+ | prevents web server from listing directory content</translate> | ||
+ | |- | ||
+ | | 16 | ||
+ | | ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]'' | ||
+ | | <translate><!--T:62--> | ||
+ | file allowing to initialise schema version of the com_helloworld component.</translate> | ||
+ | |} | ||
+ | <div class="row"> | ||
+ | <div class="large-6 columns">{{Basic button|<translate><!--T:3--> | ||
+ | S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part|Prev: Adding a view to the site part</translate>|class=expand success}}</div> | ||
+ | <div class="large-6 columns">{{Basic button|<translate><!--T:4--> | ||
+ | S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_model_to_the_site_part|Next: Adding a model to the site part</translate>|class=expand}}</div> | ||
+ | </div> | ||
+ | __NOTOC__ | ||
+ | <noinclude> | ||
+ | <translate> | ||
+ | <!--T:63--> | ||
+ | [[Category:Joomla! 3.x]] | ||
[[Category:Joomla! 3.0]] | [[Category:Joomla! 3.0]] | ||
[[Category:Joomla! 3.1]] | [[Category:Joomla! 3.1]] | ||
[[Category:Joomla! 3.2]] | [[Category:Joomla! 3.2]] | ||
+ | [[Category:Joomla! 3.3]] | ||
+ | [[Category:Joomla! 3.4]] | ||
[[Category:Beginner Development]] | [[Category:Beginner Development]] | ||
[[Category:Component Development]] | [[Category:Component Development]] | ||
+ | [[Category:Tutorials]] | ||
+ | [[Category:Tutorials in a Series]] | ||
+ | </translate> | ||
+ | </noinclude> |
Latest revision as of 00:30, 29 March 2018
Articles in This Series
- Introduction
- Developing a Basic Component
- Adding a View to the Site Part
- Adding a Menu Type to the Site Part
- Adding a Model to the Site Part
- Adding a Variable Request in the Menu Type
- Using the Database
- Basic Backend
- Adding Language Management
- Adding Backend Actions
- Adding Decorations to the Backend
- Adding Verifications
- Adding Categories
- Adding Configuration
- Adding ACL
- Adding an Install/Uninstall/Update Script File
- Adding a Frontend Form
- Adding an Image
- Adding a Map
- Adding AJAX
- Adding an Alias
- Using the Language Filter Facility
- Adding a Modal
- Adding Associations
- Adding Checkout
- Adding Ordering
- Adding Levels
- Adding Versioning
- Adding Tags
- Adding Access
- Adding a Batch Process
- Adding Cache
- Adding a Feed
- Adding an Update Server
- Adding Custom Fields
- Upgrading to Joomla4
This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! Version.
Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).
Note[edit]
- This tutorial is part of the Developing a MVC Component for Joomla! 3.x tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
- You can follow the steps below to add a menu item to the Hello World! component, or you can directly download the archive
- You can watch a video associated with this step in the tutorial at Step 3, Adding a Menu Part.
Adding a Menu Item to Hello World[edit]
In this article we will cover how to add a menu item to a basic Joomla! component. For this example we will be continuing our work on the Hello World! component.
There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.
1 | Manually add the files into <path_to_joomla>/ |
2 | Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install |
3 | Update using the Joomla! Extension Manager and an Update Server |
To add a menu item you will need to navigate to com_helloworld, which is the original directory we made for our component. You must use the updated directory structure from the last tutorial. Using your preferred file manager, create or update the following files; as you create or update the files, add the source code for each file which is found in File Details.
1 | Create: default.xml | <path_to_com_helloworld/site/views/helloworld/tmpl/default.xml |
2 | Update: helloworld.xml | <path_to_com_helloworld/helloworld.xml |
Updating the Hello World! Component[edit]
To update the Hello World! Component in the Joomla! website, please follow the same steps for the original installation.
After the component has been updated, we will be able to add a Menu Item to it. This will allow us to access our component a menu rather than having to remember what to type into the address bar. We'll do this using the Menu Manager of Joomla!.
- Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <yoursite>/joomla/administrator/index.php. For this example we will navigate to localhost/joomla/administrator/index.php.
- Add a new menu item by clicking on Menu → <existing menu> → Add New Menu Item. A new screen will appear.
- In the "Menu Title" field, enter "Hello World!"
- In the "Menu Type" field, click on the "Select" button and choose Hello World! → Hello World from the selection screen.
- Click Save & Close
You should now be able to access the component through the menu that you just created.
File Details[edit]
site/views/helloworld/tmpl/default.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
<message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
</layout>
</metadata>
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2018</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.3</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component ...</description>
<update> <!-- Runs on update; New since J2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
</files>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>
</extension>
Code Explanation[edit]
In case you were curious as to why this works the way it does.
default.xml[edit]
Note - For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.
helloworld.xml[edit]
<version>0.0.3</version>
Updates the version number.
Component Contents[edit]
At this point in the tutorial, your component should contain the following files:
1 | helloworld.xml | this is an XML (manifest) file that tells Joomla! how to install our component. |
2 | site/helloworld.php | this is the site entry point to the Hello World! component |
3 | site/index.html | prevents web server from listing directory content |
4 | site/controller.php | file representing the controller |
5 | site/views/index.html | prevents web server from listing directory content |
6 | site/views/helloworld/index.html | prevents web server from listing directory content |
7 | site/views/helloworld/view.html.php | file representing the view |
8 | site/views/helloworld/tmpl/index.html | prevents web server from listing directory content |
9 | site/views/helloworld/tmpl/default.php | the default view |
10 | site/views/helloworld/tmpl/default.xml | file that adds menu item |
11 | admin/index.html | prevents web server from listing directory content |
12 | admin/helloworld.php | this is the administrator entry point to the Hello World! component |
13 | admin/sql/index.html | prevents web server from listing directory content |
14 | admin/sql/updates/index.html | prevents web server from listing directory content |
15 | admin/sql/updates/mysql/index.html | prevents web server from listing directory content |
16 | admin/sql/updates/mysql/0.0.1.sql | file allowing to initialise schema version of the com_helloworld component. |