<?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/"
xmlns:series="http://unfoldingneurons.com/"
><channel><title>Larry Ullman &#187; framework</title> <atom:link href="http://www.larryullman.com/tag/framework/feed/" rel="self" type="application/rss+xml" /><link>http://www.larryullman.com</link> <description>Translating Geek Into English</description> <lastBuildDate>Sun, 05 Feb 2012 17:48:42 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>My Yii Book Update</title><link>http://www.larryullman.com/2011/11/03/my-yii-book-update/</link> <comments>http://www.larryullman.com/2011/11/03/my-yii-book-update/#comments</comments> <pubDate>Fri, 04 Nov 2011 01:48:50 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[books]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[yii]]></category> <category><![CDATA[yiibk]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2853</guid> <description><![CDATA[I&#8217;ve received a few comments and questions lately about my intentions to write a book on the Yii framework. I&#8217;ve never formally discussed the idea, and so it seems like it&#8217;s about time I do so. I first started using the Yii framework about two and half years ago. I&#8217;ve never been much of a framework [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;ve received a few comments and questions lately about my intentions to write a book on the <a
href="http://yiiframework.com/">Yii framework</a>. I&#8217;ve never formally discussed the idea, and so it seems like it&#8217;s about time I do so.</p><p>I first started using the Yii framework about two and half years ago. I&#8217;ve never been much of a framework person, but Yii really felt right to me, quite similar to <a
href="http://rubyonrails.com/">Ruby on Rails</a>, which I also always liked. Being a writer, after learning to use the framework, I wrote an <a
href="http://www.larryullman.com/series/learning-the-yii-framework/">introductory series on the subject</a>, which has been quite popular. In all modesty, many have suggested it&#8217;s the best documentation available. In fact, the creator of Yii liked my series so much that he listed it prominently on the official Yii documentation page (it&#8217;s now under <a
href="http://www.yiiframework.com/tutorials/">tutorials</a>). Some time after writing that series, I started thinking about writing a full book on Yii, because <a
href="http://www.larryullman.com/books/">that&#8217;s what I do</a>.</p><p>When I decided to write a book on Yii, I figured I&#8217;d self-publish it, for a couple of reasons. First, even though I have a wonderful relationship with <a
href="http://peachpit.com/">Peachpit Press</a>, I&#8217;m not sure they&#8217;d want to do a book on Yii, as the market is kind of small. Second, even if Peachpit would publish such a book, I doubt I&#8217;d make much money on the project, considering the small market. By comparison, if I self-publish, I can make 4-5 times per book what I&#8217;d make if I went through a publisher. The higher per copy amount could be enough to make up for the smaller sales, ending up with a project that&#8217;s financially worth my time to do (sorry to be crass about the money, but writing a book is a lot of work and I do have bills to pay!). Fourth, I&#8217;ve been intrigued about self-publishing for some time. And, fifth, self-publishing would give me the opportunity to distribute the book in unique formats and channels, such as a chapter at a time.</p><p>If I had my act together (which is to say, if my life were other than it is, in about ten ways), I would have been on the ball and published this book a year or more ago. Sadly, that has not been the case. I keep fairly busy work-wise, and I don&#8217;t actually have the time (due to personal constraints) to put in 40-hour weeks, so it&#8217;s really hard to add new projects, especially on the level of an entire book. Moreover, self-publishing means no guaranteed money, so I&#8217;d have to not do paying work while not making money working on the Yii book, which is a tough situation to be in.</p><p>All that being said, <em>it is still my intention to write and self-publish a book on Yii</em>. The only question is: when? This is the question I&#8217;m getting asked a lot lately. Before I do anything towards a book on Yii, I still have to:</p><ul><li>Finish my <em>Modern JavaScript: Develop and Design</em> book (which I&#8217;m weeks late on as is)</li><li>Write one more article in support of my <em>PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)</em> [I've written two out of three articles, but I'm weeks late on that, too.]</li><li>Come up with a list of videos to do in support of my <em>Modern JavaScript: Develop and Design</em> book</li><li>Actually do those videos</li><li>Continue doing the Web development and other work I have for my clients</li></ul><p>So&#8230;yikes. Don&#8217;t get my wrong: I&#8217;m quite fortunate to be busy, but yikes! I&#8217;ll be crying if I haven&#8217;t finished all of the above by the end of this year, which means in theory I can begin the Yii book at the beginning of 2012. However, I have the third edition of my <em>PHP 5 Advanced: Visual QuickPro Guide</em> due at the end of April. That does give me four months, but I&#8217;d like actually make that deadline for a change (my publisher is wonderfully understanding, but&#8230;).</p><p>Also, along with writing the Yii book, I&#8217;m going to have to come up with a site and an ecommerce system and so forth (I already have the software that can output PDFs, ePubs, and mobis). If I&#8217;m being optimistic, perhaps in 2012 I can do two Yii chapters per month, but the <em>PHP 5 Advanced</em> book will need to be my first priority. I also don&#8217;t want to start the Yii book, get some people paying for it (in part or in whole), and then have the project drag out. I don&#8217;t know. We shall see.</p><p>I very much thank everyone for their interest in my writing a book on Yii and I hope to make that happen. If you follow the blog and/or subscribe to my newsletter, you&#8217;ll get updates as to how this is progressing, when and if it does actually progress.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/11/03/my-yii-book-update/feed/</wfw:commentRss> <slash:comments>36</slash:comments> </item> <item><title>The CodeLobster PHP IDE</title><link>http://www.larryullman.com/2011/06/23/the-codelobster-php-ide/</link> <comments>http://www.larryullman.com/2011/06/23/the-codelobster-php-ide/#comments</comments> <pubDate>Thu, 23 Jun 2011 14:48:42 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[cakephp]]></category> <category><![CDATA[codeigniter]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[ide]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[smarty]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2627</guid> <description><![CDATA[I&#8217;ve been contacted a couple of times now by the people behind CodeLobster, a PHP-centric IDE, in the hopes that I&#8217;d review/mention CodeLobster on this site. Now, CodeLobster only runs on Windows, which means I haven&#8217;t, and probably won&#8217;t, use it myself (I primarily use a Mac, only using Windows for testing purposes), but I [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;ve been contacted a couple of times now by the people behind <a
href="http://www.codelobster.com/">CodeLobster</a>, a PHP-centric IDE, in the hopes that I&#8217;d review/mention CodeLobster on this site. Now, CodeLobster only runs on Windows, which means I haven&#8217;t, and probably won&#8217;t, use it myself (I primarily use a Mac, only using Windows for testing purposes), but I have no problems mentioning products and sites here that I don&#8217;t personally use, because such things may still be of use to you (you, whoever you are, presumably are a separate entity with your own interests, needs, etc., and are statistically more likely to be running Windows). Anyway&#8230;</p><p>So, CodeLobster is an IDE for PHP that runs on Windows. It&#8217;s available in both a free and &#8220;professional&#8221; version, the professional version costing $100 (US). The free version comes with an HTML editor and inspector, a CSS editor, a JavaScript editor, a PHP editor, and a PHP debugger. This all includes the standard features such as code completion, code collapsing, browser preview, project management, FTP, and so forth. The professional version includes all of those features, plus plug-ins for specific tools and frameworks: CakePHP, CodeIgniter, Drupal, jQuery, Joomla, Smarty, Symfony, WordPress, and Yii. In other words, the professional version gives you code completion, contextual help, and so forth for these additional tools that you may also be programming in.</p><p>As I said, I haven&#8217;t personally used it, but if you&#8217;re looking for a PHP/Web Development IDE, it may be worth checking out.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/06/23/the-codelobster-php-ide/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Using Cookies in the Yii Framework</title><link>http://www.larryullman.com/2011/06/04/using-cookies-in-the-yii-framework/</link> <comments>http://www.larryullman.com/2011/06/04/using-cookies-in-the-yii-framework/#comments</comments> <pubDate>Sat, 04 Jun 2011 14:28:06 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[cookie]]></category> <category><![CDATA[csrf]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[security]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2552</guid> <description><![CDATA[In a previous post, I wrote about . In this one, I&#8217;ll look at using cookies. Neither is that difficult, but as with all things regarding frameworks, the solution may not be obvious at first. And there are some ways to make use cookies in Yii in a more secure manner.To create a cookie in [...]]]></description> <content:encoded><![CDATA[<p>In a previous post, I wrote about <a
href="http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/">using sessions in Yii-based sites</a>. In this one, I&#8217;ll look at using cookies. Neither is that difficult, but as with all things regarding frameworks, the solution may not be obvious at first. And there are some ways to make use cookies in Yii in a more secure manner.<span
id="more-2552"></span>To create a cookie in PHP without using a framework, you just call the <a
href="http://us.php.net/setcookie">setcookie()</a> function. To create a cookie while using the Yii framework, you don&#8217;t use <strong>setcookie()</strong>, but rather create a new element in the <strong>Yii::app()-&gt;request-&gt;cookies</strong> array. (Note that sessions are stored in <strong>Yii::app()-&gt;session</strong>, but cookies are in <strong>Yii::app()-&gt;request-&gt;cookies</strong>, because cookies are part of the HTTP request a browser makes of a Web server).</p><p>What you&#8217;ll want to do to create a cookie is create a new object of type <a
href="http://www.yiiframework.com/doc/api/1.1/CHttpCookie/">CHttpCookie</a>: Yii&#8217;s class for cookies. Here, then, is the syntax for setting a cookie in Yii:</p><pre class="brush: php; title: ; notranslate">Yii::app()-&gt;request-&gt;cookies['name'] = new CHttpCookie('name', 'value');</pre><p>You must use the same <em>name</em> value in both places, replacing it with the actual cookie name. Remember that the cookie&#8217;s name, and value, are visible to users in their browsers, so one ought to be prudent about what name you use and be extra mindful of what values are being stored.</p><blockquote><p>Tip: Because the cookie&#8217;s name must be used twice in the code, you may want to consider assigning the cookie&#8217;s name to a variable that is used in both instances instead.</p></blockquote><p>Once you&#8217;ve created a cookie, you can access it (on subsequent pages, because cookies are never immediately available to the page that set them), using <strong>Yii::app()-&gt;request-&gt;cookies['name']-&gt;value</strong>. You have to use the extra <strong>-&gt;value</strong> part, because the &#8220;cookie&#8221; being created is actually an object of type <strong>CHttpCookie</strong> (and Yii, internally, takes care of actually sending the cookie to the browser and reading the received cookie from the browser).</p><p>To test if a cookie exists, just use <strong>isset()</strong> on <strong>Yii::app()-&gt;request-&gt;cookies['name']</strong>, as you would any other variable.</p><p>To delete an existing cookie, just unset the element as you would any array element:</p><pre class="brush: php; title: ; notranslate">unset(Yii::app()-&gt;request-&gt;cookies['name']);</pre><p>To delete all existing cookies (for that site), use</p><pre class="brush: php; title: ; notranslate">Yii::app()-&gt;request-&gt;cookies-&gt;clear();</pre><p>By default, cookies will be set to expire when the browser window is closed. To change that, you need to modify the properties of the cookie. You can&#8217;t do so when you create the <strong>CHttpCookie</strong> object (i.e., the only arguments to the constructor are the cookie&#8217;s name and value), so you must separately create a new object of type <strong>CHttpCookie</strong>, to be assigned to <strong>Yii::app()-&gt;request-&gt;cookies</strong> later:</p><pre class="brush: php; title: ; notranslate">$cookie = new CHttpCookie('name', 'value');</pre><p>Then adjust the <strong>expire</strong> attribute:</p><pre class="brush: php; title: ; notranslate">$cookie-&gt;expire = time() + (60*60*24); // 24 hours</pre><p>Then add the cookie to the application:</p><pre class="brush: php; title: ; notranslate">Yii::app()-&gt;request-&gt;cookies['name'] = $cookie;</pre><p>You can manipulate other cookie properties using the above syntax: <strong>domain</strong>, <strong>httpOnly</strong>, <strong>path</strong>, and <strong>secure</strong>. Each of these correspond to the arguments to the <strong>setcookie()</strong> function. (You can also manipulate the value of the cookie through <strong>$cookie-&gt;value</strong> and the cookie&#8217;s name through <strong>$cookie-&gt;name</strong>). For example, if you want to limit a cookie to a specific domain, or subdomain, use <strong>domain</strong>; to limit it to a specific folder, use <strong>path</strong>; and to only transmit the cookie over SSL, set <strong>secure</strong> to <strong>true</strong>.</p><p>You can also improve the security of your cookies by setting Yii&#8217;s <strong>enableCookieValidation</strong> to <strong>true</strong>, in the Yii configuration file:</p><pre class="brush: php; title: ; notranslate">return array(
    'components'=&gt;array(
        'request'=&gt;array(
            'enableCookieValidation'=&gt;true,
        ),
    ),
);</pre><p>Cookie validation prevents cookies from being manipulated in the browser. To accomplish that, Yii stores a hashed representation of the cookie&#8217;s value when it gets sent, and then compares the received cookie&#8217;s value to ensure they are the same. Obviously there&#8217;s extra overhead required to do this, but in some instances, the extra effort is justified by the extra security.</p><p>Finally, one good reason to use cookies in a Yii-based site, even if the site is otherwise using sessions, is to prevent <a
href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-site Request Forgery (CSRF)</a> attacks. A CSRF works like this: malicious site A has some code on it, such as an image tag whose <strong>src</strong> attribute points to a page on site B that does something meaningful: <em>http://www.example.com/page.php?action=this</em>. When any viewer loads the page on site A, the use of that <strong>src</strong> attribute has the effect of that user performing a request of the page on site B.</p><p>As an example, let&#8217;s say that an administrator at your site logs in and does whatever but doesn&#8217;t log out. The administrator therefore still has a cookie in her or his browser indicating access to the site (i.e., the user could open the browser and perform admin tasks without logging in again). Now let&#8217;s say that the <strong>src</strong> attribute on malicious site A points to a page on your site that deletes a blog posting. If the administrator with the live cookie loads that page on site A, it will have the same effect as if that administrator went to your site and requested that page directly. This is not good.</p><p>To prevent a CSRF attack on your site, first make sure that all significant form submissions use POST instead of GET. You should be using POST for any form that changes server content anyway, but a CSRF POST attack is a bit harder to pull off than a GET attack.</p><p>Second, set <strong>enableCsrfValidation</strong> to <strong>true</strong> in your configuration file:</p><pre class="brush: php; title: ; notranslate">return array(
    'components'=&gt;array(
        'request'=&gt;array(
            'enableCsrfValidation'=&gt;true,
        ),
    ),
);</pre><p>By doing this, Yii will send a cookie with a unique identifier to the user. All forms will then store that same identifier in a hidden input. The form submission will only be handled then if the two identifiers match. With the case of a CSRF attack, the two identifiers will not match because the form&#8217;s identifier will not be passed as part of the request (I hope this is clear; if not, let me know). Note that this only works if you&#8217;re using <a
href="http://www.yiiframework.com/doc/api/1.1/CHtml">CHtml</a> to create your forms (if you manually create the form tags, Yii won&#8217;t insert the necessary code for preventing CSRF attacks).</p><p>The most important thing to remember about cookies, which I&#8217;ve already stated, is that cookies are visible to the user in the browser. And unless you&#8217;re using SSL for the cookies, they are also visible to anyone else while being transmitted back and forth between the server and the client (which happens on every page request). So be careful of what gets stored in a cookie! If the data is particularly sensitive, use sessions instead of cookies.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/06/04/using-cookies-in-the-yii-framework/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Yii vs Zend vs Code Igniter Compared</title><link>http://www.larryullman.com/2011/06/01/yii-vs-zend-vs-code-igniter-compared/</link> <comments>http://www.larryullman.com/2011/06/01/yii-vs-zend-vs-code-igniter-compared/#comments</comments> <pubDate>Wed, 01 Jun 2011 17:05:39 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[yii]]></category> <category><![CDATA[zend]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2593</guid> <description><![CDATA[I&#8217;m often asked why I like the Yii framework, which is easy enough to answer: for starters it requires PHP 5 and uses jQuery natively. Then I like how it auto-generates a lot of code and folders for you. From there, it just kind of works and makes sense to me. In other words, Yii [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;m often asked why I like the <a
href="http://www.yiiframework.com">Yii framework</a>, which is easy enough to answer: for starters it requires PHP 5 and uses <a
href="http://www.jquery.com">jQuery</a> natively. Then I like how it auto-generates a lot of code and folders for you. From there, it just kind of works and makes sense to me. In other words, Yii feels right. And unless you really investigate the framework&#8217;s underpinnings, how it feels (and can you get it to do what you need to do) is a large part of the criteria in making a selection.</p><p>The question I can&#8217;t really answer is what advantage Yii has over the <em>X</em> framework. The only other PHP framework I&#8217;ve used extensively is the <a
href="http://framework.zend.com">Zend framework</a>. The Zend framework has a lot going for it and is worth anyone&#8217;s consideration. To me, its biggest asset is that you can use it piecemeal and independently (I&#8217;ve often used components of the Zend Framework in Yii-based and non-framework-based sites), but I just don&#8217;t like the Zend Framework as the basis of an entire site. It requires a lot of work, the documentation is overwhelming while still not being that great, and it just doesn&#8217;t &#8220;fell&#8221; right to me.</p><p>Anyway, the point of this post is that there&#8217;s a <a
href="http://www.sheldmandu.com/php/php-mvc-frameworks/yii-vs-zend-vs-code-igniter-compared">nice article at SHELDMANDU</a> from back in January in which the author does a great job of comparing the Yii framework with the Zend framework and <a
href="http://codeigniter.com/">Code Igniter</a> (I&#8217;ve heard many good things about Code Igniter). Moreover, the author lays out some of his criteria for what he wants in a framework, has reasonable and detailed critiques, and also specifically details why he didn&#8217;t consider other frameworks in his comparison. If you&#8217;re looking into frameworks, spend five minutes reading that article to help educate yourself as to what considerations you should have in mind during your research.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/06/01/yii-vs-zend-vs-code-igniter-compared/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>Using Sessions with the Yii Framework</title><link>http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/</link> <comments>http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/#comments</comments> <pubDate>Tue, 03 May 2011 01:16:56 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[cookie]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[session]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2353</guid> <description><![CDATA[I haven&#8217;t written much about the Yii framework lately, mostly because I&#8217;ve been working night and day on the fourth edition of my &#8220;PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide&#8221; book, due out late summer 2011. So I figured I&#8217;d put together another little blurb on the Yii framework (by regularly putting [...]]]></description> <content:encoded><![CDATA[<p>I haven&#8217;t written much about the <a
href="http://www.yiiframework.com">Yii framework</a> lately, mostly because I&#8217;ve been working night and day on the fourth edition of my &#8220;PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide&#8221; book, due out late summer 2011. So I figured I&#8217;d put together another little blurb on the Yii framework (by regularly putting out posts on Yii, it&#8217;ll be that much easier when I go to write a book on Yii later this summer). In this post, I&#8217;m going to talk about using sessions Yii-based sites (in a separate post, I&#8217;ll discuss cookies). While not at all hard, the topic, like quite a few things, is not obvious in Yii, or well documented.</p><p><span
id="more-2353"></span></p><p>The first thing to know about using sessions in Yii is that you don&#8217;t have to do anything to enable them, which is to say you don&#8217;t have to invoke <strong>session_start()</strong>, as you would in a standard PHP script. This is the behavior with Yii&#8217;s <strong>autoStart</strong> session property set to <em>true</em>, which is the default. Even without using <strong>session_start()</strong>, you could, of course, make use of the <strong>$_SESSION</strong> superglobal array, as you would in a standard PHP script, but it&#8217;s best when using frameworks to make total use of the framework. The Yii equivalent to <strong>$_SESSION</strong> is <strong>Yii::app()-&gt;session</strong>:</p><pre class="brush: php; title: ; notranslate">Yii::app()-&gt;session['var'] = 'value';
echo Yii::app()-&gt;session['var']; // Prints &quot;value&quot;</pre><p>And that&#8217;s all there is to it. To remove a session variable, apply <strong>unset()</strong>, as you would to any other variable:</p><pre class="brush: php; title: ; notranslate">unset(Yii::app()-&gt;session['var']);</pre><p>So&#8230;nothing really unexpected there, once you know where to find the session data. The more complex consideration is how to configure sessions for your Yii application. You can do so using the primary configuration file (<strong>protected/config/main.php</strong>). Within that, you would add a &#8220;session&#8221; element to the &#8220;components&#8221; array, wherein you customize how the sessions behave. The key attributes are:</p><ul><li><strong>autoStart</strong>, which defaults to <em>true</em> (i.e., always start sessions)</li><li><strong>cookieMode</strong>, with acceptable values of <em>none</em>, <em>allow</em>, and <em>only</em>, equating to: don&#8217;t use cookies, use cookies if possible, and only use cookies; defaults to <em>allow</em></li><li><strong>cookieParams</strong>, for adjusting the session cookie&#8217;s arguments, such as its lifetime, path, domain, and HTTPS-only</li><li><strong>gCProbability</strong>, for setting the probability of garbage collection being performance, with a default of 1, as in a 1% chance</li><li><strong>savePath</strong>, for setting the directory on the server used as the session directory, with a default of <em>/tmp</em></li><li><strong>sessionName</strong>, for setting the session&#8217;s, um, name, which defaults to <em>PHPSESSID</em></li><li><strong>timeout</strong>, for setting after how many seconds a session is considered idle, which defaults to 1440</li></ul><p>For all of these, the default values are the same as those that PHP sessions commonly run using, except for <strong>autoStart</strong>.</p><p>If your site will not be using sessions at all, you would want to disable them by adding this code to the &#8220;components&#8221; section of <strong>protected/config/main.php</strong>:</p><pre class="brush: php; title: ; notranslate">'session' =&gt; array (
    'autoStart' =&gt; false,
),</pre><p>If you are using sessions, for security purposes, you may want to change the session&#8217;s name, always require cookies, and change the save path:</p><pre class="brush: php; title: ; notranslate">'session' =&gt; array (
    'sessionName' =&gt; 'Site Access',
    'cookieMode' =&gt; 'only',
    'savePath' =&gt; '/path/to/new/directory',
),</pre><p>The save path, in case you&#8217;re not familiar with it, is where the session data is stored on the server. By default, this is a temporary directory, globally readable and writable. Every site running on the sever, if there are many (and shared hosting plans can have dozens on a single server), share this same directory. This means that any site on the server can read any other site&#8217;s stored session data. For this reason, changing the save path to a directory within your own site can be a security improvement. Alternatively, you can store the session data in a database. To do that, add this code to the &#8220;components&#8221; section of <strong>protected/config/main.php</strong>:</p><pre class="brush: php; title: ; notranslate">'session' =&gt; array (
    'class' =&gt; 'system.web.CDbHttpSession',
    'connectionID' =&gt; 'db',
    'sessionTableName' =&gt; 'actual_table_name',
),</pre><p>If you choose this route, Yii will automatically create the table if it does not exist. You can also perform any of the other session configuration changes in that code block, too.</p><p>So&#8230;what else? Frequently, for debugging purposes, and sometimes to store it in the database, I like to know the user&#8217;s current session ID. That value can be found in <strong>Yii::app()-&gt;session-&gt;sessionID</strong>.</p><p>Finally, when the user logs out, you may want to formally eradicate the session. To do so, call <strong>Yii::app()-&gt;session-&gt;clear()</strong> to remove all of the session variables. Then call <strong>Yii::app()-&gt;session-&gt;destroy(</strong>) to get rid of the actual data stored on the server.</p><p>And that&#8217;s what there is to know about using sessions with Yii, at least that&#8217;s all the key information. I hope this helps you with your Yii-based applications. As always, thanks for reading and let me know if you have any comments or questions.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/feed/</wfw:commentRss> <slash:comments>30</slash:comments> </item> <item><title>Rendering View Files in Yii</title><link>http://www.larryullman.com/2011/02/15/rendering-view-files-in-yii/</link> <comments>http://www.larryullman.com/2011/02/15/rendering-view-files-in-yii/#comments</comments> <pubDate>Tue, 15 Feb 2011 16:08:58 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[view]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2139</guid> <description><![CDATA[In the MVC architecture, the Controller reacts to a user request. In doing so, the Controller often loads an instance of a particular Model and then renders a specific View. &#8220;Rendering&#8221; just means compiling all the pieces together, including static text (HTML and such) and the output from executed PHP code. For example, when a [...]]]></description> <content:encoded><![CDATA[<p>In the MVC architecture, the Controller reacts to a user request. In doing so, the Controller often loads an instance of a particular Model and then <em>renders</em> a specific View. &#8220;Rendering&#8221; just means compiling all the pieces together, including static text (HTML and such) and the output from executed PHP code. For example, when a user goes to a page for updating a record, the Controller loads the associated record, and then renders the &#8220;update&#8221; View, which will display the pre-populated form:</p><pre class="brush: php; title: ; notranslate">public function actionUpdate($id) {
    $data=$this-&gt;loadModel($id);

    $this-&gt;render('update',array(
        'model'=&gt;$data
    ));
}</pre><blockquote><p>Note: That method would also have code in it for handling the submission of the update form, but I&#8217;m trying not to complicate the discussion.</p></blockquote><p>As you can see in that code, the <strong>render()</strong> method, defined in the <strong>CController</strong> class, is how a View file is chosen for rendering. The first argument to the method is the View file to be rendered, without its <strong>.php</strong> extension. The <strong>render()</strong> method will render the View file within the appropriate layout file. In other words, the View file will be rendered with its context. The above code renders <strong>update.php</strong>, for the associated Controller, wrapped within the <strong>views/layouts/main.php</strong> layout file (the default).</p><p>The second argument to <strong>render()</strong> is an array of data that can be sent to the View file. In the above code, the Model instance is being passed along. In <strong>update.php</strong>, references to the <strong>$model</strong> variable will refer to the loaded data (note that the View gets its variable names from the indexes used in the array).</p><p>The <strong>render()</strong> method takes an optional third argument, which is a Boolean indicating if the rendered result should be returned to the Controller instead of sent to the Web browser. This would be useful if you wanted to render the page and then send the output in an email or write it to a text file on the server (to act as a cached version).</p><p>Sometimes you&#8217;ll want to render a View file without incorporating the layout. To do that, invoke <strong>renderPartial()</strong>. For example, both the <strong>update.php</strong> and <strong>create.php</strong> View files, auto-generated by Yii, just provide a context, and then include the form file:</p><pre class="brush: php; title: ; notranslate">&lt;?php echo $this-&gt;renderPartial('_form', array('model'=&gt;$model)); ?&gt;</pre><p>Since the initial View file will have already be rendered within the layout context, the layout shouldn&#8217;t be rendered again. The <strong>renderPartial()</strong> method will render just the named View file. Its second argument, as with <strong>render()</strong>, can be used to pass data to the View file. In the above, the Model instance is passed along.</p><blockquote><p>Tip: The <strong>renderPartial()</strong> method is also used for Ajax calls, where the layout isn&#8217;t appropriate.</p></blockquote><p>As mentioned already, the file being rendered comes from the directory associated with the current Controller. For example, when updating an <strong>Employee</strong> record, the URL is something like <span
style="text-decoration: underline;">www.example.com/index.php/employee/update/id/23</span>. This calls the <strong>actionUpdate()</strong> method of the <strong>EmployeeController</strong> class (whose code is partially shown above). That method renders the &#8220;update&#8221; View, which is to say <strong>protected/views/employee/update.php</strong>. But there are rare cases where you&#8217;ll need to render View files from other subdirectories. For example, you may want to include a search form on a page, with that form found within another View directory. To change the reference point, start the View reference with a double slash, which means to start in the <strong>views</strong> folder. Then indicate the subdirectory and file, still omitting the extension:</p><pre class="brush: php; title: ; notranslate">&lt;?php echo $this-&gt;renderPartial('//search/_form'); ?&gt;</pre><p>And that&#8217;s all there is to it!</p><p>View rendering is one of the most important concepts to grasp in an MVC design. Fortunately, it&#8217;s not that hard to follow in Yii. Just remember that if you want your layout file, use <strong>render()</strong>. If not, use <strong>renderPartial()</strong>. If you need to pass data to the View file, use the second argument to send along an array, whose indexes will become the names of the variables within the View. Finally, if you need to change the include path, begin with a double slash.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/02/15/rendering-view-files-in-yii/feed/</wfw:commentRss> <slash:comments>17</slash:comments> </item> <item><title>Yii 1.1.6 Released</title><link>http://www.larryullman.com/2011/01/25/yii-1-1-6-released/</link> <comments>http://www.larryullman.com/2011/01/25/yii-1-1-6-released/#comments</comments> <pubDate>Tue, 25 Jan 2011 22:37:46 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2243</guid> <description><![CDATA[Version 1.1.6 of the Yii framework was released a few days ago. Along with bug fixes, 1.1.6 includes a couple of new features, most notably database migration and a new Query Builder. Database migration is a feature that comes from Ruby on Rails (well, that&#8217;s where I first heard of it) and it allows for [...]]]></description> <content:encoded><![CDATA[<p>Version 1.1.6 of the <a
href="http://www.yiiframework.com/">Yii framework</a> was released a few days ago. Along with bug fixes, 1.1.6 includes a couple of new features, most notably <a
href="http://www.yiiframework.com/doc/guide/1.1/en/database.migration">database migration</a> and a new <a
href="http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder">Query Builder</a>. Database migration is a feature that comes from <a
href="http://www.rubyonrails.com">Ruby on Rails</a> (well, that&#8217;s where I first heard of it) and it allows for better version control. Basically database migration allows you to associate database changes with versions, so that you can better sync updates to the PHP code and the underlying database. It&#8217;s a useful tool for projects being developed by a team or in stages.</p><p>The new Query Builder is an object-oriented way to create custom SQL statements. This isn&#8217;t really a new feature (in the sense of allowing you to do something you couldn&#8217;t do before) but lets you do something you might commonly do but in a different way. See the above link for a thorough discussion and demonstration.</p><p>For some reason, the 1.1.6 release of Yii includes an article on <a
href="http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices">Best MVC Practices</a>. This isn&#8217;t really part of the framework itself, but is a useful read for anyone using Yii or other MVC approaches.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/01/25/yii-1-1-6-released/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>More of &#8220;Learning the Yii Framework&#8221; Series in French</title><link>http://www.larryullman.com/2011/01/23/more-of-learning-the-yii-framework-series-in-french/</link> <comments>http://www.larryullman.com/2011/01/23/more-of-learning-the-yii-framework-series-in-french/#comments</comments> <pubDate>Sun, 23 Jan 2011 03:17:46 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[translate]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2236</guid> <description><![CDATA[Mémorandom, which is translating my popular “Learning the Yii Framework” into French and publishing it online, has recently posted the translated versions of two more parts in the series. The fourth part is Defining Databases for the Yii Application. The fifth part is Creating Models, Views, and Controllers in Yii. My thanks again to Nico [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://www.memorandom.fr/">Mémorandom</a>, which is translating my popular “<a
href="../series/learning-the-yii-framework/">Learning the Yii Framework</a>” into French and publishing it online, has recently posted the translated versions of two more parts in the series. The fourth part is <a
href="http://www.memorandom.fr/php/definir-une-base-de-donnees-pour-notre-application-yii/">Defining Databases for the Yii Application</a>. The fifth part is <a
href="http://www.memorandom.fr/php/creer-les-modeles-les-vues-et-les-controleurs-dans-yii/">Creating Models, Views, and Controllers in Yii</a>. My thanks again to Nico for the  nice words on my series and for the work in translating it!</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/01/23/more-of-learning-the-yii-framework-series-in-french/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Creating Forms with the Yii Framework</title><link>http://www.larryullman.com/2011/01/20/creating-forms-with-the-yii-framework/</link> <comments>http://www.larryullman.com/2011/01/20/creating-forms-with-the-yii-framework/#comments</comments> <pubDate>Thu, 20 Jan 2011 15:40:26 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[form]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2145</guid> <description><![CDATA[HTML forms are one of the key pieces of any Web site, providing an easy way to get data from the user. But, as is the case with many things, creating forms while using a framework such as Yii is significantly different than creating forms using standard HTML alone. In this post, you&#8217;ll learn what [...]]]></description> <content:encoded><![CDATA[<p>HTML forms are one of the key pieces of any Web site, providing an easy way to get data from the user. But, as is the case with many things, creating forms while using a framework such as Yii is significantly different than creating forms using standard HTML alone. In this post, you&#8217;ll learn what you need to know to get started creating HTML forms when using the Yii framework.<span
id="more-2145"></span></p><p>Before getting into the code, let&#8217;s take a minute to think about the MVC architecture. A form itself is found in the View, as a form is part of the user interface. More specifically, when Yii auto-generates a form as part of CRUD creation, the framework writes the form in a file named <strong>_form.php</strong>. This file in turn gets included by other View files (any View file in Yii that starts with an underscore is intended to be an include). Also understand that the same <strong>_form.php</strong> file is intended to be used whether the form is for creating new records or updating existing ones. Naturally, the Controller dictates which primary View file gets rendered.</p><p>Forms, though, are associated with specific Models. A contact form may have its own Model, not tied to a database table (in which case the Model extends <strong>CFormModel</strong>), whereas a form for employees or departments will be based upon a Model that is tied to a database table (in which case the Model extends <strong>CActiveRecord</strong>, most likely). Whether the Model extends <strong>CFormModel</strong> or <strong>CActiveRecord</strong>, the important thing to remember is that the form is tied to a Model. This is significant because it&#8217;s the Model that dictates what elements exist, controls validation of the form, and defines the form&#8217;s labels (e.g., <em>First Name</em> for the <strong>firstName</strong> attribute), and so forth.</p><blockquote><p>Tip: There are instances where you might have a form not associated with a Model, but that is rare. The most common such instance would be a search form.</p></blockquote><p>Before getting to the View and its form, let&#8217;s be clear as to how the View accesses the specific Model. A Controller may have this code:</p><pre class="brush: php; title: ; notranslate">public function actionCreate() {

    $model=new Employee;

    /* Code for validation and redirect upon save. */

    // If not saved, render the create View:
    $this-&gt;render('create',array(
        'model'=&gt;$model, // Model is passed to create.php View!
    ));
}
</pre><p>The <strong>create.php </strong>View file will include <strong>_form.php</strong>, passing along the Model instance in the process:</p><pre class="brush: php; title: ; notranslate">&lt;?php echo $this-&gt;renderPartial('_form', array('model'=&gt;$model)); ?&gt;</pre><p>So now <strong>_form.php</strong> has access to the Model instance and can create the form tied to that Model.</p><p>Of course, to be fair, you could create an HTML form using raw HTML, without Yii at all. The downside to that approach is it creates no tie-in between the Model&#8217;s validation rules, errors, labels, etc., and the form. By creating the form using Yii, labels will be based upon the Model definitions (meaning that changing just the Model changes reference to attributes everywhere), invalid form values can automatically be highlighted, and much, much more. Plus, it&#8217;s not hard to use Yii to create a form, once you understand how.</p><p>The older Yii method for creating a form was simply a matter of invoking the appropriate <strong>CHtml</strong> methods: <strong>activeLabel()</strong>, <strong>activeTextField()</strong>, <strong>activeDropDownList()</strong>, and so forth:</p><pre class="brush: php; title: ; notranslate">&lt;div class=&quot;row&quot;&gt;
     &lt;?php echo CHtml::activeLabel($model,'username'); ?&gt;
     &lt;?php echo CHtml::activeTextField($model,'username') ?&gt;
&lt;/div&gt;</pre><p>The <strong>CHtml::activeLabel()</strong> method creates the label. The other methods create other form elements. Each method takes the Model involved as its first argument and the Model&#8217;s corresponding attribute as its second, thereby tying the form to the Model.</p><p>And here&#8217;s another reason to use Yii&#8217;s system of creating forms: if the form is being used for an update, the values will automatically be prepopulated/pre-selected/pre-checked based upon the existing Model! As you should know, that alone requires a lot of code and logic.</p><p>As of Yii 1.1.1, forms can be created using the <a
href="http://www.yiiframework.com/doc/api/1.1/CActiveForm">CActiveForm</a> widget. Among other benefits, <strong>CActiveForm</strong> is capable of enabling client-side form validation using Ajax. You always start by invoking <strong>beginWidget()</strong>:</p><pre class="brush: php; title: ; notranslate">$form = $this-&gt;beginWidget('CActiveForm');</pre><p>(This code goes in the <strong>_form.php</strong> file.) Now <strong>$form</strong> is an object of the <strong>CActiveForm</strong> widget type and it can be used to generate the form itself:</p><pre class="brush: php; title: ; notranslate">&lt;div&gt;
    &lt;?php echo $form-&gt;labelEx($model,'firstName'); ?&gt;
    &lt;?php echo $form-&gt;textField($model,'firstName',array('size'=&gt;20,'maxlength'=&gt;20)); ?&gt;
    &lt;?php echo $form-&gt;error($model,'firstName'); ?&gt;
&lt;/div&gt;</pre><p>You&#8217;ll see that whereas <strong>CHtml</strong> has the <strong>activeTextField()</strong> method, <strong>CActiveForm</strong> has just <strong>textField()</strong>. The same goes for <strong>activeLabel()</strong> becoming just <strong>labelEx()</strong>. You&#8217;ll see this pattern—dropping the <em>active</em> part—repeated. Still, the Model is passed as the first argument and the attribute involved as the second.</p><p>Returning to the widget itself, you can customize the behavior of the form by passing an array of values when creating it:</p><pre class="brush: php; title: ; notranslate">&lt;?php $form = $this-&gt;beginWidget('CActiveForm', array(
    'id'=&gt;'user-form',
    'enableAjaxValidation'=&gt;true,
    'focus'=&gt;array($model,'firstName'),
)); ?&gt;</pre><blockquote><p>Note: I&#8217;ll address Ajax form validation in a separate post.</p></blockquote><p>The various <strong>CActiveForm</strong> properties can be found in the <a
href="http://www.yiiframework.com/doc/api/1.1/CActiveForm#properties">documentation</a>. Commonly you won&#8217;t need to customize any properties, but you can change the form&#8217;s <em>method</em> and <em>action</em> attributes, or add additional HTML to the opening form tag.</p><p>Finally, the form needs a submit button. Unlike the other form elements, this isn&#8217;t tied to a Model; it is created with just the <strong>CHtml</strong> class:</p><pre class="brush: php; title: ; notranslate">&lt;div&gt;
    &lt;?php echo CHtml::submitButton($model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;
&lt;/div&gt;</pre><p>The <strong>submitButton()</strong>&#8216;s argument is the textual label for the button.</p><p>Then the form is closed by &#8220;ending&#8221; the widget:</p><pre class="brush: php; title: ; notranslate">&lt;?php $this-&gt;endWidget(); ?&gt;</pre><p>So those are the basics for using <strong>CActiveForm</strong>. Once you&#8217;ve taken the above steps, form elements will be prepopulated when updating a record, errors will be clearly indicated upon form submission, and so forth.</p><p>The most important information involves creating the form elements. For the most part, doing so is simple and direct, as shown in the above. By associating the Model instance with the form, Yii will do the rest. Sometimes you&#8217;ll need to customize the look and behavior of an element. I&#8217;ve discussed this in many separate posts, which I&#8217;d recommend you read:</p><ul><li><a
href="http://www.larryullman.com/2010/08/10/handling-related-models-in-yii-forms/">Handling Related Models in Yii Forms</a></li><li><a
href="http://www.larryullman.com/2010/07/25/handling-checkboxes-in-yii-with-non-boolean-values/">Handling Checkboxes in Yii with non-Boolean Values</a></li><li><a
href="http://www.larryullman.com/2009/12/01/configuring-fckeditor-for-yii-driven-sites/">Configuring FCKEditor for Yii-Driven Sites</a></li></ul><p>And my <a
href="http://www.larryullman.com/2009/11/12/basic-view-edits-in-yii/">Basic View Edits</a> post, part of my <a
href="http://www.larryullman.com/series/learning-the-yii-framework/">Learning the Yii Framework</a> series, demonstrates how to use <strong>CHtml::listData()</strong> to populate a drop down menu. I&#8217;ll no doubt post more threads particular to forms in Yii, as its such a critical topic. Potential subjects include: Ajax validation and handling file uploads.</p><p>In a separate post, I&#8217;ll talk about Form Builder, added to Yii in version 1.1.0. It allows you to create forms in a different way, primarily in the Controller (not unlike using the <a
href="http://pear.php.net/package/HTML_QuickForm2">PEAR HTML_QuickForm </a>class).</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/01/20/creating-forms-with-the-yii-framework/feed/</wfw:commentRss> <slash:comments>37</slash:comments> </item> <item><title>&#8220;Learning the Yii Framework&#8221; Series Now in French</title><link>http://www.larryullman.com/2011/01/14/learning-the-yii-framework-series-now-in-french/</link> <comments>http://www.larryullman.com/2011/01/14/learning-the-yii-framework-series-now-in-french/#comments</comments> <pubDate>Fri, 14 Jan 2011 01:44:23 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[translate]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2206</guid> <description><![CDATA[Nico, from Mémorandom, recently contacted me about translating my popular &#8220;Learning the Yii Framework&#8221; into French and publishing it on that site. I&#8217;m pleased to say that the first three parts of the series—Introduction to the Yii Framework, Getting Started with the Yii Framework, and Configuring Yii—have already been translated and are available online. This [...]]]></description> <content:encoded><![CDATA[<p>Nico, from <a
href="http://www.memorandom.fr">Mémorandom</a>, recently contacted me about translating my popular &#8220;<a
href="http://www.larryullman.com/series/learning-the-yii-framework/">Learning the Yii Framework</a>&#8221; into French and publishing it on that site. I&#8217;m pleased to say that the first three parts of the series—<a
href="http://www.memorandom.fr/php/introduction-au-framework-yii/">Introduction to the Yii Framework</a>, <a
href="http://www.memorandom.fr/php/commencer-avec-yii-framework/">Getting Started with the Yii Framework</a>, and <a
href="http://www.memorandom.fr/php/configuration-de-yii/">Configuring Yii</a>—have already been translated and are available online. This should hopefully help expand Yii&#8217;s international influence. My thanks to Nico for the nice words on my series and for the work in translating it!</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/01/14/learning-the-yii-framework-series-now-in-french/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
<!-- Served from: www.larryullman.com @ 2012-02-05 14:31:59 by W3 Total Cache -->
