forked from magento/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmage-for-dev-2.html
243 lines (184 loc) · 11 KB
/
mage-for-dev-2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
---
---
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<link rel="stylesheet" type="text/css" href="{{ site.baseurl }}common/css/stylesheet.css"/>
<link rel="stylesheet" href="{{ site.baseurl }}common/css/stylesheet-fonts.css" type="text/css" charset="utf-8">
<link rel="icon" href="{{ site.baseurl }}common/css/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="{{ site.baseurl }}common/css/favicon.ico" type="image/x-icon">
<title> Magento for Developers: Part 2—The Magento Config</title>
</head>
<body>
<a name="top"></a>
<img src="{{ site.baseurl }}common/images/m1x/m1xheader.png" width="1024" alt="header" />
<body>
<a name="top"></a>
<div id="content">
<h1> Magento for Developers: Part 2—The Magento Config</h1>
<p><em>by Alan Storm</em>, updated for Magento 1.12</p>
<p><a href="{{ site.m1xgithuburl }}/magefordev/mage-for-dev-2.html" target="_blank">Edit this page on GitHub <img src="{{ site.baseurl }}common/images/github_icon.png" height="15px"></a></p>
<!-- Magento for Developers: Part 2—The Magento Config -->
<!-- magento-for-dev-part-2-the-magento-config -->
<!-- <style type="text/css">
#kb-entry .series {float: right; width: 175px; margin-left: 10px; font-size: 12px; }
#kb-entry .series ul {margin-left: 15px;}
</style>
<div class="r-box-rss series"> -->
<h5>Other articles in this series:</h5>
<ul>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-1.html">Part 1—Introduction to Magento</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-2.html">Part 2—The Magento Config</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-3.html">Part 3—Magento Controller Dispatch</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-4.html">Part 4—Magento Layouts, Blocks and Templates</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-5.html">Part 5—Magento Models and ORM Basics</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-6.html">Part 6—Magento Setup Resources</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-7.html">Part 7—Advanced ORM: Entity Attribute Value</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-8.html">Part 8—Varien Data Collections</a>
</ul>
</div>
<p class="para1">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. </p>
<p>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.</p>
<h5>In this article...</h5>
<ul><li><a href="#1" title="Setting up a Module Directory Structure">Setting up a Module Directory Structure</a>
<li><a href="#2" title="Creating a Module Config">Creating a Module Config</a>
<li><a href="#3" title="What Am I Looking at?">What Am I Looking at?</a>
<li><a href="#4" title="Why Do I Care?">Why Do I Care?</a>
</ul>
<a name="1"></a>
<h2>Setting up a Module Directory Structure</h2>
<p>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 information, changing the behavior of existing classes, or adding entirely new features. </p>
<p>It's worth noting that most of the base Magento system is built using the same module system you'll be using. If you look in </p>
<pre>app/code/core/Mage</pre>
<p>each folder is a separate module built by the Magento team. Together, these modules form the community shopping cart system you're using. Your modules should be placed in the following folder</p>
<pre>app/code/local/Packagename</pre>
<p>"Packagename" should be a unique string to Namespace/Package your code. It's an unofficial convention that this should be the name of your company. The idea is to pick a string that no one else in the world could possibly be using.</p>
<pre>app/code/local/Microsoft</pre>
<p>We'll use "Magentotutorial".</p>
<p>So, to add a module to your Magento system, create the following directory structure</p>
<pre>app/code/local/Magentotutorial/Configviewer/Block
app/code/local/Magentotutorial/Configviewer/controllers
app/code/local/Magentotutorial/Configviewer/etc
app/code/local/Magentotutorial/Configviewer/Helper
app/code/local/Magentotutorial/Configviewer/Model
app/code/local/Magentotutorial/Configviewer/sql</pre>
<p>You won't need all these folder for every module, but setting them all up now is a smart idea. </p>
<p>Next, there's two files you'll need to create. The first, config.xml, goes in the etc folder you just created. </p>
<pre>app/code/local/Magentotutorial/Configviewer/etc/config.xml</pre>
<p>The second file should be created at the following location</p>
<pre>app/etc/modules/Magentotutorial_Configviewer.xml</pre>
<p>The naming convention for this files is Packagename_Modulename.xml.</p>
<p>The config.xml file should contain the following XML. Don't worry too much about what all this does for now, we'll get there eventually</p>
<pre>
<config>
<modules>
<Magentotutorial_Configviewer>
<version>0.1.0</version>
</Magentotutorial_Configviewer>
</modules>
</config>
</pre>
<p>Finally, Magentotutorial_Configviewer.xml should contain the following xml.</p>
<pre>
<config>
<modules>
<Magentotutorial_Configviewer>
<active>true</active>
<codePool>local</codePool>
</Magentotutorial_Configviewer>
</modules>
</config>
</pre>
<p>That's it. You now have a bare bones module that won't do anything, but that Magento will be aware of. To make sure you've done everything right, do the following:
<ol>
<li>Clear your Magento cache</li>
<li>In the Magento Admin, go to <strong>System->Configuration->Advanced</strong></li>
<li>In the "Disable modules output" panel verify that Magentotutorial_Configviewer shows up</li>
</ol>
<p>Congratulations, you've built your first Magento module!</p>
<a name="2"></a>
<h2>Creating a Module Config</h2>
<p>Of course, this module doesn't do anything yet. When we're done, our module will
<ol>
<li>Check for the existence of a "showConfig" query string variable</li>
<li>If showConfig is present, display our Magento config and halt normal execution</li>
<li>Check for the existence of an additional query string variable, showConfigFormat that will let us specify text or xml output.</li>
</ol>
<p>First, we're going to add the following <global> section to our config.xml file.</p>
<pre>
<config>
<modules>...</modules>
<global>
<events>
<controller_front_init_routers>
<observers>
<Magentotutorial_configviewer_model_observer>
<type>singleton</type>
<class>Magentotutorial_Configviewer_Model_Observer</class>
<method>checkForConfigRequest</method>
</Magentotutorial_configviewer_model_observer>
</observers>
</controller_front_init_routers>
</events>
</global>
</config>
</pre>
<p>Then, create a file at </p>
<pre>Magentotutorial/Configviewer/Model/Observer.php</pre>
<p>and place the following code inside</p>
<pre>
<?php
class Magentotutorial_Configviewer_Model_Observer {
const FLAG_SHOW_CONFIG = 'showConfig';
const FLAG_SHOW_CONFIG_FORMAT = 'showConfigFormat';
private $request;
public function checkForConfigRequest($observer) {
$this->request = $observer->getEvent()->getData('front')->getRequest();
if($this->request->{self::FLAG_SHOW_CONFIG} === 'true'){
$this->setHeader();
$this->outputConfig();
}
}
private function setHeader() {
$format = isset($this->request->{self::FLAG_SHOW_CONFIG_FORMAT}) ?
$this->request->{self::FLAG_SHOW_CONFIG_FORMAT} : 'xml';
switch($format){
case 'text':
header("Content-Type: text/plain");
break;
default:
header("Content-Type: text/xml");
}
}
private function outputConfig() {
die(Mage::app()->getConfig()->getNode()->asXML());
}
}
</pre>
<p>That's it. Clear your Magento cache again and then load any Magento URL with a <tt>showConfig=true</tt> query string</p>
<pre>http://magento.example.com/?showConfig=true</pre>
<a name="3"></a>
<h2>What am I looking at?</h2>
<p>You should be looking at a giant XML file. This describes the state of your Magento system. It lists all modules, models, classes, event listeners or almost anything else you could think of.</p>
<p>For example, consider the config.xml file you created above. If you search the XML file in your browser for the text <tt>Configviewer_Model_Observer</tt> you'll find your class listed. Every module's config.xml file is parsed by Magento and included in the global config.</p>
<a name="4"></a>
<h2>Why Do I Care?</h2>
<p>Right now this may seem esoteric, but this config is key to understanding Magento. Every module you'll be creating will add to this config, and anytime you need to access a piece of core system functionality, Magento will be referring back to the config to look something up. </p>
<p>A quick example: As an MVC developer, you've likely worked with some kind of helper class, instantiated something like </p>
<pre>
$helper_sales = new HelperSales();
</pre>
<p>One of the things Magento has done is abstract away PHP's class declaration. In Magento, the above code looks something like </p>
<pre>
$helper_sales = Mage::helper('sales');
</pre>
<p>In plain english, the static helper method will:
<ol>
<li>Look in the <helpers /> section of the Config.</li>
<li>Within <helpers />, look for a <sales /> section</li>
<li>Within the <sales /> section look for a <class /> section</li>
<li>Append the part after the slash to the value found in #3 (defaulting to <tt>data</tt> in this case)</li>
<li>Instantiate the class found in #4 (Mage_Sales_Helper_Data)</li>
</ol>
<p>While this seems like a lot of work (and it is), the key advantage is by always looking to the config file for class names, we can override core Magento functionality <strong>without</strong> changing or adding to the core code. This level of meta programming, not usually found in PHP, allows you to cleanly extend only the parts of the system you need to.</p>