<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SpinOneSolutions &#187; Module</title>
	<atom:link href="http://www.spinonesolutions.com/tag/module/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.spinonesolutions.com</link>
	<description>I waste my time so that you don't have to...</description>
	<lastBuildDate>Fri, 16 Apr 2010 21:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Magento &#8211; Part I : Custom Module</title>
		<link>http://www.spinonesolutions.com/2009/09/magento-custom-module-part-i/</link>
		<comments>http://www.spinonesolutions.com/2009/09/magento-custom-module-part-i/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 04:00:01 +0000</pubDate>
		<dc:creator>Will Wright</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Module]]></category>

		<guid isPermaLink="false">http://www.spinonesolutions.com/?p=336</guid>
		<description><![CDATA[This article was written while using Magento v1.3.2.3. Greetings!  This is installment number one of the the exiting Magento Custom Module series.  There&#8217;s basically two paths to the end of the fairytale: Path 1: You follow this series and get down and dirty with Magento, learning a lot along the way. Path 2: You go [...]]]></description>
			<content:encoded><![CDATA[<p>This article was written while using Magento v1.3.2.3.</p>
<p>Greetings!  This is installment number one of the the exiting <em>Magento Custom Module</em> series.  There&#8217;s basically two paths to the end of the fairytale:</p>
<p>Path 1: You follow this series and get down and dirty with Magento, learning a lot along the way.</p>
<p>Path 2: You go out and download the Module Creator (<a href="http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table">http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table</a>) and head straight to GO!</p>
<p>Your choice!</p>
<p>Note: Magento is an MVC application so I&#8217;m going to assume you have a basic understanding of <a title="MVC" href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" target="_blank">MVC</a>.  Also, Magento is built (I believe) on the <a title="Zend Framework" href="http://framework.zend.com/" target="_blank">Zend framework</a>, so there&#8217;s some Zend specific syntax that I&#8217;m also not going to dive into.  Huzzzah!</p>
<h2>The Problem:</h2>
<p>You need to create a custom Module in Magento!  Why&#8230;?  Hell, I don&#8217;t know; you just do.  That&#8217;s enough of that, let&#8217;s get down to it.</p>
<h2>The Solution:</h2>
<p>All of your customizations need to go into the [root]\app\code\local folder.  This is where Magento expects to find them and it&#8217;s also a factor in maintaining your upgrade path.  When the next version of Magento is released you&#8217;ll be able to backup your &#8220;local&#8221; folder and blow away everything else.</p>
<p>Before we get too far into this let’s setup our development environment, go to:<br />
Admin -&gt; System -&gt; Cache Management: Cache Control – All Cache = Disable</p>
<p>This will save you many hours of self-torture while you’re developing.</p>
<p>A couple of other developer settings that I’ve found helpful:<br />
Admin -&gt; System -&gt; Configuration -&gt; Developer: Debug &#8211; Profiler = yes<br />
Log Settings &#8211; Enabled = yes</p>
<p>Now open up [root]\index.php and at the very end you&#8217;ll see three lines that are commented out; un-comment them and save.</p>
<p>Finally, turn SEO friendly URLs on:<br />
Admin -&gt; System -&gt; Configuration -&gt; Web: Search Engine Optimization &#8211; Use Web Server Rewrites = Yes</p>
<p>All of the URLs that I’ll be presenting as examples below assume that SEO friendly URLs are enabled.</p>
<p>Magento is now correctly configured for development so let’s dive into building a custom module.</p>
<p>Sample file structure:<br />
[root]\app\code\local\{Namespace}\{Modulename}<br />
[root]\app\code\local\{Namespace}\{Modulename}\controllers<br />
[root]\app\code\local\{Namespace}\{Modulename}\etc<br />
[root]\app\code\local\{Namespace}\{Modulename}\etc\config.xml<br />
[root]\app\code\local\{Namespace}\{Modulename}\Helper<br />
[root]\app\code\local\{Namespace}\{Modulename}\Helper\Data.php<br />
[root]\app\code\local\{Namespace}\{Modulename}\Model<br />
[root]\app\code\local\{Namespace}\{Modulename}\{Modulename}.php</p>
<p><strong>{Namespace}</strong> is a user defined variable.  Basically it&#8217;s just a mechanism that allows the user to create disparate classes that would otherwise have the same names.<br />
<strong>{Modulename}</strong> this is the name of your module.</p>
<p>For my examples I&#8217;m going to use &#8220;Spinonesolutions&#8221; as my namespace and &#8220;Helloworld&#8221; as my Module.  I&#8217;m not entirely sure if it matters or not, but as a general rule I always capitalize the first letter and leave the rest lower case.  Don&#8217;t use underscores in your names either, we&#8217;ll see why later.</p>
<p><strong>controllers</strong> is where all the controllers go, crazy right?  I’ll get into the controller structure in another part of this series.  For now a sample will suffice.</p>
<pre class="php" name="code">&lt;?php
//IndexController is the default controller
//EG: http://localhost/spinonesolutions/
//Notice there's no parameters being passed as a parameter (Nothing after trailing "/")
//IndexController will be called since it's the default.
//"spinonesolutions" is the frontname as defined in confg.xml
class Spinonesolutions_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
  //indexAction is the default Action for any controller
  function indexAction() {
    echo "indexAction";
    $helloworld = Mage::getModel("spinonesolutions_helloworld/helloworld");
    $helloworld-&gt;helloworld("helloworld");
  }
}</pre>
<p><strong>Data.php</strong> is just a helper file for the Magento internals, I&#8217;m not exactly sure what it does but it appears to be exactly the same in every module except for its class definition.</p>
<pre class="php" name="code">&lt;?php
class Spinonesolutions_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract {

}</pre>
<p><strong>Model</strong> holds all of the models for your module.  It&#8217;s customary to name your main model after your {Modulename}.  You’ll most likely have at least one model and if you browse through the Magento core you&#8217;ll notice that most modules contain many models.</p>
<pre class="php" name="code">&lt;?php
class Spinonesolutions_Helloworld_Model_Helloworld extends Varien_Object {
  function __construct() {
    parent::__construct();
  }

  function helloworld($arg) {
    echo "&lt;br&gt;Hello World! My argument is : " . $arg;
  }
}</pre>
<p><strong>config.xml</strong> is where you connect all your code up to Magento.  There&#8217;s a lot of voodoo going on in here and I&#8217;m not entirely sure how it all works.  I might even write a full post on the <strong>config.xml</strong> as a forthcoming part of this series.</p>
<pre class="xml" name="code">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
&lt;config&gt;
 &lt;modules&gt;
   &lt;Spinonesolutions_Helloworld&gt;
   &lt;version&gt;0.1.0&lt;/version&gt;
   &lt;/Spinonesolutions_Helloworld&gt;
 &lt;/modules&gt;
 &lt;frontend&gt;
   &lt;routers&gt;
     &lt;spinonesolutions&gt;    &lt;!-- I make this match my front name but I'm not sure it matters --&gt;
       &lt;use&gt;standard&lt;/use&gt;    &lt;!-- Use standard routing as opposed to admin.  IE: frontend vs admin --&gt;
       &lt;args&gt;
         &lt;module&gt;Spinonesolutions_Helloworld&lt;/module&gt;    &lt;!-- The module to search for controllers --&gt;
         &lt;frontName&gt;spinonesolutions&lt;/frontName&gt;    &lt;!-- The first descriminator in the path.  "spinonesolutions" in http://localhost/spinonesolutions/ --&gt;
       &lt;/args&gt;
     &lt;/spinonesolutions&gt;
   &lt;/routers&gt;
 &lt;/frontend&gt;
 &lt;global&gt;
   &lt;models&gt;
     &lt;spinonesolutions_helloworld&gt;    &lt;!-- This is used when istanting your Model EG: Mage::getModel("spinonesolutions_helloworld/hellworld") --&gt;
       &lt;class&gt;Spinonesolutions_Helloworld_Model&lt;/class&gt;    &lt;!-- That class to use when isntanting objects of type above. --&gt;
     &lt;/spinonesolutions_helloworld&gt;
   &lt;/models&gt;
 &lt;/global&gt;
&lt;/config&gt;</pre>
<p>That takes care of all the code needed in the module directory. There’s one more configuration file we need to create and it’s an exception to the local folder rule:<br />
[root]\app\etc\modules\{Namespace}_{Modulename}.xml</p>
<pre class="xml" name="code">&lt;?xml version="1.0"?&gt;
&lt;config&gt;
 &lt;modules&gt;
   &lt;Spinonesolutions_Helloworld&gt;
     &lt;active&gt;true&lt;/active&gt;
     &lt;codePool&gt;local&lt;/codePool&gt;
   &lt;/Spinonesolutions_Helloworld&gt;
 &lt;/modules&gt;
&lt;/config&gt;</pre>
<p>This file tells Magento that there&#8217;s a new module out there that it needs to load.  Alternatively, you can name your file [root]\app\etc\modules\{Namespace}_All.xml to tell Magento to load up all the modules in that namespace.</p>
<p>Once you have this file in place you can navigate to Admin-&gt;System -&gt; Configuration -&gt; Advanced and you should see your module enabled here.</p>
<p>You now have a functioning custom module, hurray!</p>
<p>Download the source &#8211; <a href="http://www.spinonesolutions.com/wp-content/uploads/2009/09/Helloworld.rar">Helloworld</a></p>
<ol>
<li><a title="Part I : Custom Module" href="http://www.spinonesolutions.com/2009/09/magento-custom-module-part-i/">Part I : Custom Module</a></li>
<li><a title="Part II : Models" href="http://www.spinonesolutions.com/2009/10/magento-part-ii-models/">Part II : Models</a></li>
<li><a title="Part III : Controllers" href="http://www.spinonesolutions.com/2009/10/magento-part-iii-controllers/">Part III : Controllers</a></li>
<li><a title="Part IV : Extending Models" href="http://www.spinonesolutions.com/2009/10/magento-part-iv-extending-models/">Part IV : Extending Models</a></li>
<li><a title="Part V: Data Layer" href="http://www.spinonesolutions.com/2009/10/magento-part-v-data-layer/">Part V : Data Layer</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.spinonesolutions.com/2009/09/magento-custom-module-part-i/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
