Magento

Magento for Developers: Part 4 – Magento Layouts, Blocks and Templatese

Developers new to Magento are often confused by the Layout and View system. This article will take a look at Magento’s Layout/Block approach, and show you how it fits into Magento MVC worldview. Unlike many popular MVC systems, Magento’s Action Controller does not pass a data object to the view or set properties on the view object. Instead, the View component directly references system models to get the information it needs for display. One consequence of this design decision is that the View has been separated into Blocks and Templates. Blocks are PHP objects, Templates are “raw” PHP files (with a .phtml extension) that contain a mix of HTML and PHP (where PHP is used as a templating language). Each Block is tied to a single Template file. Inside a phtml file, PHP’s $this keyword will contain a reference to the Template’s Block object. A quick example. Take a look a the default product Template at app/design/frontend/base/default/template/catalog/product/list.phtml You’ll see the following PHP template code. <?php $_productCollection=$this->getLoadedProductCollection() ?>         <?php if(!$_productCollection->count()): ?> <div class=”note-msg”>         <?php echo $this->__(“There are no products matching the selection.”) ?>    </div>     <?php else: ?> The getLoadedProductCollection method can be found in the Template’s Block, Mage_Catalog_Block_Product_List as shown: File: app/code/core/Mage/Catalog/Block/Product/List.php … public function getLoadedProductCollection() {     return $this->_getProductCollection(); }    … The block’s _getProductCollection then instantiates models and reads their data, returning … Continue Reading…

Magento

Magento for Developers: Part 3 – Magento Controller Dispatch

The Model-View-Controller (MVC) architecture traces its origins back to the Smalltalk Programming language and Xerox Parc. Since then, there have been many systems that describe their architecture as MVC. Each system is slightly different, but all have the goal of separating data access, business logic, and user-interface code from one another. The architecture of most PHP MVC frameworks will looks something like this. A URL is intercepted by a single PHP file (usually called a Front Controller). This PHP file will examine the URL, and derive a Controller name and an Action name (a process that’s often called routing). The derived Controller is instantiated. The method name matching the derived Action name is called on the Controller. This Action method will instantiate and call methods on models, depending on the request variables. The Action method will also prepare a data structure of information. This data structure is passed on to the view. The view then renders HTML, using the information in the data structure it has received from the Controller. While this pattern was a great leap forward from the “each php file is a page” pattern established early on, for some software engineers, it’s still an ugly hack. Common … Continue Reading…

Magento

Magento for Developers: Part 2 – The Magento Config

The config is the beating heart of the Magento System. It describes, in whole, almost any module, model, class, template, etc. than you’ll need to access. It’s a level of abstraction that most PHP developers aren’t used to working with, and while it adds development time in the form of confusion and head scratching, it also allows you an unprecedented amount of flexibility as far as overriding default system behaviors go. To start with, we’re going to create a Magento module that will let us view the system config in our web browser. Follow along by copying and pasting the code below, it’s worth going through on your own as a way to start getting comfortable with things you’ll be doing while working with Magento, as well as learning key terminology. In this article… Setting up a Module Directory Structure Creating a Module Config What Am I Looking at? Why Do I Care? Setting up a Module Directory Structure We’re going to be creating a Magento module. A module is a group of php and xml files meant to extend the system with new functionality, or override core system behavior. This may meaning adding additional data models to track sales … Continue Reading…

Magento

Magento for Developers: Part 1 – Introduction to Magento

What is Magento? It’s the most powerful online eCommerce platform in the universe and is changing the face of eCommerce forever. Of course, you already know that. What you may not realize is Magento’s also an object-oriented PHP Framework that can be used to develop modern, dynamic web applications that tap into Magento’s powerful eCommerce features. This is the first in a series of articles in which wee’re going to go on a whirlwind tour of Magento’s programming framework features. Don’t worry if you don’t follow everything immediately. As you study the system more everything in this article will start to make sense, and you’ll soon be the envy of your colleagues stuck working with more primitive PHP systems. In this article… Code Organized in Modules Configuration-Based MVC Controllers Context-Based URI Model Loading Models Helpers Layouts Observers Class Overrides Wrap Up Or for the more visually oriented Magento_MVC.pdf. Code Organized in Modules Magento organizes its code into individual Modules. In a typical PHP Model-View-Controller (MVC) application, all the Controllers will be in one folder, all the Models in another, etc. In Magento, files are grouped together based on functionality, which are called modules in Magento. Magento’s Code For example, you’ll … Continue Reading…