Joomla Day India 2009 Business Logic With The Mvc

Embed Size (px)

Citation preview

  1. 1. Building Custom Components in Joomla 1.5
  2. 2. Presentation name change me in master Introduction Toby Patterson Executive Director of GMITC http://www.gmitc.biz
  3. 3. Presentation name change me in master Purpose Explain how to implement your business logic using a custom Joomla! component; Introduce to the Joomla MVC and other system resources; Demonstrate some code. The purpose of this presentation is to:
  4. 4. Presentation name change me in master Do you need an extension? How many people ... ? Already have Joomla Websites? Use Joomla 3PD extensions? Want an extension that doesn't ( yet ) exist? Are developers, have developers, or will hire developers to create an extension?
  5. 5. Presentation name change me in master What is an extension? A Joomla! Extension ... Implements your business logic, Does not alter the Joomla core, so that you can easily perform upgrades, Should ( but does not have to ) use the Joomla Framework.
  6. 6. Presentation name change me in master Why Write an Extension You may wish to write an extension if you have specific business logic that an existing extension does not perform or does not perform the task as you need.
  7. 7. Presentation name change me in master Examples of Extension Tasks Displaying customer information. Placing an order Entering information about a person Checking the status of an application Changing a client's request Sending a notification many more ... ( this is where I ask for suggestions )
  8. 8. Presentation name change me in master Extensions in the Joomla Framework Components - Interactive screens representing business logic. Modules - Displayed in specific areas of a template. Plugins - Extend the Joomla Framework. It is easy to implement business logic using custom extensions.
  9. 9. Presentation name change me in master Joomla 1.5's MVC Model-View-Controller Software Design Pattern Business, presentation, and control logic are separate You can change one part without affecting another ( yay ! ) Joomla 1.5 is the first version of Joomla that uses a MVC design pattern ( double yay ! )
  10. 10. Presentation name change me in master What is an MVC Model - The model is the part of the component that encapsulates the applications data and business logic it does the work. View - The view is the part of the component that is used to render the data from the model using a layout file it creates the interface. Controller - The controller will determine what request has been made by the user and respond appropriately using the model and view it controls who does what and when.
  11. 11. Presentation name change me in master Just Remember Just remember: Components implement custom business logic rules. You may need to write your own component to express business logic specific to you. Joomla 1.5 provides an MVC to help you write a component.
  12. 12. Presentation name change me in master Implementing the Simplest Component 1. Create the directory JOOMLA/components/com_hello/ 2. Create the file JOOMLA/components/com_hello/hello.php 3. Add the following code to the file 4. Execute the following SQL query insert into jos_components values ( NULL, 'Hello', '', 0, 0, '', '', 'com_hello', 0, '', 0, '', 1 ); 5. Access the page using the following URL index.php?option=com_hello
  13. 13. Presentation name change me in master New to the MVC? The Joomla Dev Website offers a sample hello component that you can experiment with.
  14. 14. Presentation name change me in master File System Layout hello.php base controller.php Controller models/hello.php Model view.html.php View default.php Layout File Files that you will see in the hello tutorial once installed. http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,tutorials:components/
  15. 15. Presentation name change me in master Typical Flow of Execution Controller View Model Layout Determine what the user wants to do. Prepare the response. Perform the business logic. Display the results.
  16. 16. Presentation name change me in master What Does a Model Look Like Class that extends Joomla's JModel Your method representing business logic model.php
  17. 17. Presentation name change me in master View from the Frontend index.php?option=com_hello
  18. 18. Presentation name change me in master Modification to the View Add code to fetch a specific greeting. hello.php
  19. 19. Presentation name change me in master View from the Frontend 2 index.php?option=com_hello&id=2 Our presentation logic has not changed.
  20. 20. Presentation name change me in master Quick Review So far we've learned that: A component typically consists of a model, view, and controller; A component should, but does not have to, use the MVC; and Joomla offers an API for many common resources, such as the database and request variables.
  21. 21. Presentation name change me in master Displaying Data We still need to display the data. That is where the views and the layout files come in. Generally speaking, models are accessed from the view class, not the layout files, but either style works.
  22. 22. Presentation name change me in master Accessing the Business Logic The method getGreeting() is called from the view class. view.html.php
  23. 23. Presentation name change me in master Modifying the Presentation Logic The view contains the logic for displaying the data.
  24. 24. Presentation name change me in master Layout Files Layout files are used to display the data. Your template files should be the few that contain HTML. nogreeting.php default.php
  25. 25. Presentation name change me in master Quick Review Now we know that: Models represent business logic; Views represent presentation logic; Layouts are for markup language.
  26. 26. Presentation name change me in master Controller tasks You may not need to customize your controller because Joomla provides most of the control logic for you. However you can define your own methods if you want. You may want to throw an error if the use is not logged in.
  27. 27. Presentation name change me in master Customizing the Controller controller.php
  28. 28. Presentation name change me in master Using Controller Methods index.php?option=com_hello&task=showGreeting
  29. 29. Presentation name change me in master Abstract Programming Interface Important APIs that we've seen. MVC - JController, JModel, JView Language - JText Input - JRequest User - JUser
  30. 30. Presentation name change me in master Other Factory Resources JFactory - Source for global objects JApplication - The current application JMail - Mailer object JSession - Information about the current session
  31. 31. Presentation name change me in master Conclusion The important concepts to take away are: Controllers represent control logic; Models represent business logic; Views represent presentation logic; Layouts are for markup language. Joomla 1.5 provides new facilities for you to easily represent your business logic using a custom component.