<?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>PHP Magazine &#187; Zend Framework</title>
	<atom:link href="http://www.phpmag.ru/category/programming/php5/zf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmag.ru</link>
	<description>Suum cuique</description>
	<lastBuildDate>Tue, 01 Jun 2010 14:42:17 +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>Subversion Gems: Sparse Directories</title>
		<link>http://www.phpmag.ru/2010/06/01/subversion-sparse-directories/</link>
		<comments>http://www.phpmag.ru/2010/06/01/subversion-sparse-directories/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 14:41:20 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[PHP5]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=1041</guid>
		<description><![CDATA[Recently, I become involved in Zend Framework even more &#8211; this time not only developing using it (which I am doing for some 3 years now), but actually contributing back whenever I can. And I plan to dedicate even more hrs/week to work on this undoubtedly nice, yet complex piece of software. So, lyrics aside [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I become involved in Zend Framework even more &#8211; this time not only developing <em>using</em> it (which I am doing for some 3 years now), but actually <em>contributing</em> back whenever I can. And I plan to dedicate even more hrs/week to work on this undoubtedly nice, yet complex piece of software. </p>
<p>So, lyrics aside &#8211; today I had to learn new Subversion trick (the hard way), so I am writing it down here, in hope that it saves some other hacker time to play PacMan.</p>
<p><strong>Problem</strong>:<br />
I needed to checkout into single working copy all relevant (to me) parts of ZF repository. Not doing separate checkouts for the trunk, then branches, then incubator, but having single checkout of repo which I can keep up to date by simple</p>
<pre name="code" class="bash">
svn up</pre>
<p>directly from the root of working copy.</p>
<p>Now the problematic part: you never ever want to checkout whole ZF repository &#8211; need I mention that it is HUGE? So, what I actually wanted is to checkout the whole repo <em>excluding</em> some paths &#8211; such as lots of previous releases in tags/branches folders.<br />
<span id="more-1041"></span><br />
<strong>Solution</strong>:<br />
That&#8217;s where <a href="http://svnbook.red-bean.com/en/1.5/svn.advanced.sparsedirs.html">Sparse Directories chapter</a> from svn book comes handy.</p>
<p>I will not repeat what&#8217;s written there, as I doubt I can explain it better. Yet I would add couple of notes, that might be a time savers I was talking about:</p>
<p>First, as of Subversion 1.6 you can actually exclude directories <em>explicitly</em> (go on and actually read that chapter, to find out that 1.5 supports what they call implicit exclusion).</p>
<p>And second, if you are tempted to run</p>
<pre name="code" class="bash">
$ svn co http://framework.zend.com/svn/framework/standard zf-standard --depth immediates
</pre>
<p>it is  <strong>probably</strong> a bad idea. </p>
<p>In theory it should work like a charm, in practice on my Ubuntu box it takes for ever to bring those 4 immediate directories located in &#8220;standard&#8221; repo. I checked those directories with <strong>svn info</strong> and they have Depth attribute set (quite correctly) to &#8220;empty&#8221; &#8211; yet it takes a lot of network traffic and time to checkout them (once that&#8217;s done &#8211; <strong>svn up</strong> takes no time at all though). It takes so much time, as to make all this sparse directory magic not worth using. </p>
<p>Fortunately, there&#8217;s a work around: proceed one directory at a time, using &#8220;empty&#8221; as depth:</p>
<pre name="code" class="bash">
$ svn co http://framework.zend.com/svn/framework/standard zf-standard --depth empty
$ cd zf-standard
$ svn co http://framework.zend.com/svn/framework/standard/branches --depth empty
$ svn co http://framework.zend.com/svn/framework/standard/tags --depth empty
$ svn co http://framework.zend.com/svn/framework/standard/trunk --depth infinity
</pre>
<p>As you see, I made sure that <strong>trunk</strong> directory is fetched recursively. That&#8217;s because I work on trunk and need it in full.</p>
<p>Now let&#8217;s fetch some branch that we are interested in, while ignoring (and as such implicitly excluding) those that are too outdated to waste time and traffic on them. </p>
<p>Descend to <strong>zf-standard/branches</strong> directory of your newly created working copy and issue following command:</p>
<pre name="code" class="bash">
$ svn up release-1.10
</pre>
<p>As you don&#8217;t provide any depth specifiers &#8220;infinity&#8221; is assumed, and whole branch is fetched. Once that done, you can use your regular svn up/ci workflow &#8211; directory depth parameter is sticky, so you don&#8217;t have to worry about any directory that you didn&#8217;t fetch. Everything works as in normal checkouts, you just don&#8217;t have some of files (and in our case number of files tends to be 6-7 digit number) from your central repo.</p>
<p>Although I tend to <a href="http://github.com/farazdagi">git</a> more and more lately, subversion can still amaze me (after all these years!) <img src='http://www.phpmag.ru/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2010/06/01/subversion-sparse-directories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Uri and spaces/unwise chars in URLs</title>
		<link>http://www.phpmag.ru/2010/01/13/zend_uri-and-unwise-chars-in-urls/</link>
		<comments>http://www.phpmag.ru/2010/01/13/zend_uri-and-unwise-chars-in-urls/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 09:15:06 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Uri]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=928</guid>
		<description><![CDATA[Usage of Zend_Http_Client is pretty straight-forward: $client = new Zend_Http_Client($uri); $client->request(Zend_Http_Client::HEAD); // HTTP HEAD request However, today, on my Ubuntu box I had real problems as HTTP client constantly ended in segmentation fault (SIGTERM in apache logs). When debugging, I noticed that it does so only if $uri contains so called &#8220;unwise&#8221; characters, such as [...]]]></description>
			<content:encoded><![CDATA[<p>Usage of Zend_Http_Client is pretty straight-forward:</p>
<pre name="code" class="php">
$client = new Zend_Http_Client($uri);
$client->request(Zend_Http_Client::HEAD); // HTTP HEAD request
</pre>
<p>However, today, on my Ubuntu box I had real problems as HTTP client constantly ended in segmentation fault (SIGTERM in apache logs). When debugging, I noticed that it does so only if $uri contains so called &#8220;unwise&#8221; characters, such as spaces, &#8220;{&#8220;, &#8220;}&#8221;, &#8220;^&#8221; etc.</p>
<p>Going deeper, I reviewed the source code of Zend_Http_Client::setUri($uri) method, only to discover that it relies on Zend_Uri::factory($uri), to initialize the URLs. From there it was pretty simple. Zend_Uri is aware of &#8220;unwise&#8221; chars problem and disallows them by default (why exception thrown by Zend_Uri ended in SIGTERM is yet to discover, however). In order to allow such a characters:</p>
<pre name="code" class="php">
Zend_Uri::setConfig(array('allow_unwise' => true));
$client = new Zend_Http_Client($uri); // $uri may contain unwise chars now
</pre>
<p>After issuing above instruction Zend_Uri accepts those unwisely formed URIs. Plus, you have to URL-encode white-spaces (i.e. make sure that white-spaces in query string, or actually in URI part after the host name are replaced with &#8220;%20&#8243; char) to make them acceptable.</p>
<p>Further info available in <a href="http://framework.zend.com/manual/en/zend.uri.html#zend.uri.validation.allowunwise">offical documentation</a>.</p>
<p>Please note, that I still think that having unreliable chars in URI is really bad practice. If you work with some system that already gone far on this path, it&#8217;s nice to have a method to actually consume such a wildly formed URIs. Kudos to Zend_Uri maintainers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2010/01/13/zend_uri-and-unwise-chars-in-urls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework via svn:externals</title>
		<link>http://www.phpmag.ru/2009/12/30/zend-framework-via-svnexternals/</link>
		<comments>http://www.phpmag.ru/2009/12/30/zend-framework-via-svnexternals/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 21:41:52 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=921</guid>
		<description><![CDATA[I have used ZF in several projects, and think is is quite safe to use svn:externals to attach Zend and ZendX as external dependency. It might not be the good idea for other projects, but when it comes to ZF &#8211; what is in trunk is pretty stable. So, go into folder which stores ZF, [...]]]></description>
			<content:encoded><![CDATA[<p>I have used ZF in several projects, and think is is quite safe to use svn:externals to attach Zend and ZendX as external dependency. It might not be the good idea for other projects, but when it comes to ZF &#8211; what is in trunk is pretty stable.</p>
<p>So, go into folder which stores ZF, and safely remove the library. Then just add ZF as external dependency (which would be updated every time you update the working copy):</p>
<pre name="code" class="bash">
svn pe svn:externals .
</pre>
<p>in and editor opened, enter the dependencies:</p>
<pre name="code" class="bash">
Zend http://framework.zend.com/svn/framework/standard/trunk/library/Zend
ZendX http://framework.zend.com/svn/framework/extras/trunk/library/ZendX
</pre>
<p>finally commit everything and obtain.</p>
<pre name="code" class="bash">
svn ci -m 'ZF set as external dep'
svn up
</pre>
<p>The beauty of this approach &#8211; you always have up to date version of ZF.<br />
The danger of this approach &#8211; you always have up to date version of ZF!</p>
<p>Word of caution:<br />
Sometimes this may break things (when backward-compatibility is broken in ZF trunk), but actually if you have unit tests which check your build before deploying, you are pretty safe &#8211; you spot the problem, you resolve it.</p>
<p>NB: This approach supposes that you tag your releases, so that when new release is coming out, you create a tag, export everything into newly created tag (thus freezing the code), and checkout from tag on production server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2009/12/30/zend-framework-via-svnexternals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Authorize.net CIM proposal</title>
		<link>http://www.phpmag.ru/2009/08/07/zend-framework-authorize-net-cim-proposal/</link>
		<comments>http://www.phpmag.ru/2009/08/07/zend-framework-authorize-net-cim-proposal/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 10:06:28 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[PHP5]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[ANet CIM]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=853</guid>
		<description><![CDATA[I have finally completed proposal for Authorize.net CIM API. Hopefully, this API gets included into ZF (in one form or another), as ANet is one of the best services to work with user payment profiles, let alone CC processing. On proposal page you can review use cases, and if you are interested in internals feel [...]]]></description>
			<content:encoded><![CDATA[<p>I have finally completed <a href="http://zendframework.com/wiki/display/ZFPROP/Zend_Service_AuthorizeNet_Cim+-+Victor+Farazdagi">proposal</a> for <a href="http://www.authorize.net/solutions/merchantsolutions/merchantservices/cim/">Authorize.net CIM</a> API. Hopefully, this API gets included into ZF (in one form or another), as ANet is one of the best services to work with user payment profiles, let alone CC processing. On proposal page you can review use cases, and if you are interested in internals feel free to download fully-working component&#8217;s <a href="http://www.phpmag.ru/AuthorizeNet.tar.gz">prototype</a> (it is still a prototype as most probably it would be updated in accordance with peer review on ZF wiki).</p>
<p>Let me know your suggestions on how the component can be improved.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2009/08/07/zend-framework-authorize-net-cim-proposal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework: dynamically change view script</title>
		<link>http://www.phpmag.ru/2009/07/31/zend-framework-dynamically-change-view-script/</link>
		<comments>http://www.phpmag.ru/2009/07/31/zend-framework-dynamically-change-view-script/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 13:55:47 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=731</guid>
		<description><![CDATA[Rendering view script for particular controller action is really easy. Indeed, thanks to (enabled by default) ViewRenderer helper, scripts are auto-loaded: renderer searches script directory(ies) for a view script called CONTROLLER_NAME/ACTION_NAME.SCRIPT_EXTENSION once found, view is auto-rendered. However, sometimes it&#8217;s not what you want, instead you need to render some other view depending on some internal [...]]]></description>
			<content:encoded><![CDATA[<p>Rendering view script for particular controller action is really easy. Indeed, thanks to (enabled by default) ViewRenderer helper, scripts are auto-loaded: renderer searches script directory(ies) for a view script called <code>CONTROLLER_NAME/ACTION_NAME.SCRIPT_EXTENSION</code> once found, view is auto-rendered. </p>
<p>However, sometimes it&#8217;s not what you want, instead you need to render some other view depending on some internal criteria. For example, you may want to render some custom error displaying view. So, if we are positive on what we want, let me show you how to do it:</p>
<pre name="code" class="php">
class TestController extends Zend_Controller_Action
{
    /**
     * test/index.phtml would be rendered by default
     * @return void
     */
    public function indexAction()
    {
        // render test/foo.phtml
        $this->_helper->viewRenderer('foo');

        // render bar/foo.phtml to the default response segment, without using a
        // controller view script subdirectory ("test" in this case):
        // so you may use scripts from other controllers
        $this->_helper->viewRenderer('bar/foo', null, true);

        // now render the script, as previous calls do not render anything,
        // they are just used so that default script is swapped and not rendered:
        $this->render('foo');
        // OR
        $this->render('bar/foo', null, true);
    }
}
</pre>
<p>Please, note that you do not need to provide view extension (foo but not foo.phtml or any extension you happen to use for your views) &#8211; what renderer expects is action name, and we simply swapping the current action. Internally, direct() method is called when you update view renderer, which in turn is proxy for setRender():</p>
<pre name="code" class="php">
// allows you to set any of scriptAction, responseSegment,
// and noController in one pass.
setRender($action = null, $name = null, $noController = false)
</pre>
<p>So, you may use </p>
<pre name="code" class="php">
$this->_helper->viewRenderer->setRender('foo');
</pre>
<p>to change current view, but this form is less common, as it requires more typing <img src='http://www.phpmag.ru/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
NB: If you need more info on ViewRenderer there&#8217;s no better place but <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html">ZF Manual</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2009/07/31/zend-framework-dynamically-change-view-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework: models auto-loading</title>
		<link>http://www.phpmag.ru/2009/03/23/zend-framework-models-auto-loading/</link>
		<comments>http://www.phpmag.ru/2009/03/23/zend-framework-models-auto-loading/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 00:42:00 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[PHP5]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=511</guid>
		<description><![CDATA[Zend Framework (ZF) is the MVC framework. Of course you can use its components in a non-MVC way (and I actually do so in my WP UMapper plugin), but in that case, I suppose, you do not have to worry about models auto-loading. Frameworks make our coding life easier, and ZF is not an exception [...]]]></description>
			<content:encoded><![CDATA[<p>Zend Framework (ZF) is the MVC framework. Of course you can use its components in a non-MVC way (and I actually do so in my <a href="http://wordpress.org/extend/plugins/umapper/">WP UMapper plugin</a>), but in that case, I suppose, you do not have to worry about models auto-loading.</p>
<p>Frameworks make our coding life easier, and ZF is not an exception &#8211; you need very little code to get your application skeleton working. And when it comes to MVC, ZF handles almost everything &#8211; your action controllers are triggered, your views are loaded, w/o you having to worry about them. Not the same with models. If you comply to directory layout advised by ZF, you have &#8220;models&#8221; folder, but framework doesn&#8217;t interact with it in any way. </p>
<p>Models are simply classes, containing application logic, and to initialize one of them you have to make sure that:<br />
a) containing directory is withing include_path<br />
b) you require_once the class file before using it</p>
<p>I wanted my models to be available without any additional hassle, so I decided to find a way to auto-load models from within my controller actions.<br />
<span id="more-511"></span><br />
What I did is the following:</p>
<h4>1. Extend Zend_Controller_Action</h4>
<p>I extended abstract controller action class, and let it remain abstract:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> Afc_Controller_Action <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Every controller action now should be a subclass of Afc_Controller_Action, instead of Zend_Controller_Action. That way, if I need to put some method common for all my controllers, I could put it into Afc_Controller_Action, and all of them would inherit the functionality. One good example of such functionality might be auto-loading the model <img src='http://www.phpmag.ru/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>2. Add loadModel() method into abstract controller action</h4>
<p>Here is the full code of my base controller action (there are quite a few other methods, but they are not necessary for this discussion):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> Afc_Controller_Action <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Loads specified model from module's &quot;models&quot; directory
     * 
     * @param   string  $class  Model classname
     * @param   string  $module Module name. By default, current module is assumed
     * @throws  Zend_Exception  In case model class not found exception would be thrown
     * @return  string Class name loaded
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadModel<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$module</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$modelDir</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFrontController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#41;</span>
                       <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'models'</span><span style="color: #339933;">;</span>
        Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modelDir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// if we got here - then file is included</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$class</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I doubt it could be easier! getModuleDirectory() returns me the full directory path to my modules folder, within which (according to ZF directory recommendations) I have my models:</p>
<pre land="shell">
application
  modules
    default               <---- module name
      controller
      models              <----- models directory
        Account             <----- I separate models by controllers
          Profile.php         <----- model
      views
</pre>
<p>Please note that I set modules directory in my bootstrap with:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$frontController</span> <span style="color: #339933;">=</span> Zend_Controller_Front<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$frontController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addModuleDirectory</span><span style="color: #009900;">&#40;</span>FULL_PATH_TO_MODULES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here is sample action which uses model auto-loading:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AccountController <span style="color: #000000; font-weight: bold;">extends</span> Afc_Controller_Action  
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * /default/account/profile/
     *
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> profileAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        try <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// application/modules/default/models/Account/Profile.php is loaded</span>
            <span style="color: #000088;">$model</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Account_Profile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// module is not found</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That's it, we auto-loading our models w/o any additional efforts to locate the models directory.</p>
<h4>3. (Optional) Enable class auto-loading</h4>
<p>This one is not strictly necessary, it just goes in line with model auto-loading, as it allows to auto-load any other classes within your include_path. At the beginning of bootstrap process:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Loader.php'</span><span style="color: #339933;">;</span>
Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">registerAutoload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now if class is not found, Zend_Loader would try to locate it. According to ZF naming convention underscores would be considered directory separators. So, if I try to initialize My_Cool_Class() Zend_Loader would look for My/Cool/Class.php within include_path directories (well it would actually include that file, and if it is within include_path, then, obviously, it would be included). </p>
<p>On UMapper, I added our library path into include_path within bootstrap file, so I have no require_once() left in my code, and classes are loaded when they are necessary, not when the file with tons of require_once() is loaded.</p>
<h4>Alternatives:</h4>
<p>Are there any alternative approaches? Plenty! Here what I have considered (and ruled out due to various reasons) when looking for a solution:</p>
<p>a) Zend_Load_PluginLoader - plugin loader is used extensively in Zend_Form_* as it helps loading filters, validators, and decorators. I considered the idea to just register prefix path for all my model folders, but plugin loader is not a good option performance-wise. If you looking for such a solution reading <a href="http://framework.zend.com/manual/en/zend.loader.pluginloader.html">respective section</a> of the original documentation should be enough to get you going.</p>
<p>b) Zend_Load subclassing - based on class name analysis (say MyModelNameModel) decide that what needs to be loaded is the model, and look within "models" folder. The detailed solution could be found <a href="http://www.zfforums.com/zend-framework-general-discussions-1/concepts-ideas-planning-39/autoloading-models-889.html">here</a>. I did like the idea, but solution seemed to be <em>patchy</em> to me. Granted, Zend_Loader is the way to load classes adhering to certain convention, but if "underscores are directory separators" assumption is logical enough "all models should have keyword Model in class name" is not that intuitive. What is worse - it is not a universal convention.</p>
<p>c) Use controller plugin. Just <a href="http://www.zfforums.com/zend-framework-general-discussions-1/concepts-ideas-planning-39/autoloading-models-889.html">extend Zend_Controller_Plugin_Abstract</a> and put include_path changing logic into preDispatch(). Not a bad solution, but I didn't want to update include_path with "models" folders from my modules. Ideally, my include_path should contain reference to ZF, my own library, and current folder! And right now it is exactly like this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2009/03/23/zend-framework-models-auto-loading/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
