794_The.Definitive.Guide.to.Magento.pdf

This directory name can be misleading

Info icon This preview shows pages 183–186. Sign up to view the full content.

\app\code\local\Definitivemagento\appname\sql : This directory name can be misleading. Its purpose is twofold: to define custom database tables and process any upgrades to your extension. \etc\modules : This is a stepchild directory, located outside the confines of the package. Files located here register your module into Magento’s system.
Image of page 183

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

CHAPTER 10 MAGENTO EXTENSIONS 162 Simple Hello World Module Now you’ll dive deeper and build a simple Hello World module. You’ll start small and use only the directories needed. Once you have a basic understanding, reading the core complex modules won’t be a shot in the dark. Files for Hello World Module \app \code \local \Definitivemagento \Helloworld \Block \Hi.php \etc \config.xml \etc \modules \Definitivemagento_All.xml As you can see, this is very bare bones. But it’s important to start small and build up from there. \app\code\local\Definitivemagento\Helloworld\Block\Hi.php: <?php class Definitivemagento_Helloworld_Block_Hi extends Mage_Core_Block_Template { rotected function _toHtml() { return "Hello World"; } } Notice the naming convention used here. The class name is Definitivemagento_Helloworld_Block_Hi. Each underscore represents the directory structure. This is Magento/Zend’s way of handling namespaces. This knowledge is good to know, especially if you’ll be reading Magento’s core files and figuring out which file is being accessed. This method extends Magento’s Block class so you can call it within a CMS block element. For simplicity’s sake, you’ll be using the _toHtml() method to output the data. If you don’t use the _toHtml() method and define your own, you need to call it directly: $this->methodName(). \app\code\local\Definitivemagento\Helloworld\etc\config.xml <?xml version="1.0"?> <config> <modules> <Definitivemagento_Helloworld> <version>1.0</version> </Definitivemagento_Helloworld> </modules>
Image of page 184
CHAPTER 10 MAGENTO EXTENSIONS 163 <global> <blocks> <Definitivemagento_Helloworld> <class>Definitivemagento_Helloworld_Block</class> </Definitivemagento_Helloworld> </blocks> </global> </config> This is the configuration file. The first set of tags, <modules>, defines the name/path and version of your custom module. Everything else will go under the <global> tag, which defines the resources, classes, and blocks. Because you’re building a Hello World block, you’ll define the <blocks> tag and define the class name here. If you were building a Model, they would link in here. Note Unfortunately, Magento’s documentation is lacking. But by viewing the core Magento files, you can get a sense of which other parameters can exist within this file. Take a look at the Catalog core file (\app\code\core\Mage\Catalog\etc\config.xml). You can see how <admin> defines the input text fields for the Admin Panel, <adminhtml> creates the drop down menu of the Admin Panel, and <frontend> allows you to access catalog through the URL.
Image of page 185

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 186
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern