Sunday, May 27, 2018

Can't we just do a "Hello, world"?

The Joomla! component tutorial walks you through creating a Model-View-Controller (MVC) component. However, the Absolute basics of a component page also mentions that a component doesn't have to be MVC; it claims that a flat model is supported, but then spends no time explaining how it's supposed to work.

Quite simply, actually. A component has a startup file with the name that matches the component's element without the initial com_ (e. g. the startup file for com_foobar would be foobar.php). Joomla executes that file, intercepts whatever output it generates, and inserts that output into the content area of the HTML document, surrounded by the template and the modules.

Joomla knows which component to invoke by checking the option parameter in the query string. So navigating to index.php?option=com_foobar would execute our component. I haven't figured out yet if/how one can expose a flat model component via SEO friendly URLs. I haven't figured out, either, if/how components like this can register their menu item types. MVC ones can, that's covered in the tutorial.

Joomla uses output buffering to capture the output of the component, so none of the document's initial HTML is emitted before the component is done working. This way, a component can also manipulate HTTP headers, including redirection - something it won't be able to do, were Joomla to render the document in order. If the component doesn't want the rest of the document around its output, it's free to call exit in the end. That will flush the output buffer to the wire instead of letting Joomla capture and handle it.

A flat component like this only needs an XML manifest, the startup file under the "site" subfolder, and index.html under site to comply with the Joomla convention.

No comments:

Post a Comment