<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Vance Lucas</title>
	
	<link>http://www.vancelucas.com</link>
	<description>Web Entrepreneur and Freelance PHP/Javascript Developer</description>
	<lastBuildDate>Mon, 04 Mar 2013 17:23:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/VanceLucas" /><feedburner:info uri="vancelucas" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Valitron: The Simple Validation Library That Doesn’t Suck</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/qvwG1NoY6Wg/</link>
		<comments>http://www.vancelucas.com/blog/valitron-the-simple-validation-library-that-doesnt-suck/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 17:23:19 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[validator]]></category>
		<category><![CDATA[valitron]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=982</guid>
		<description><![CDATA[Valitron is a simple, minimal and elegant stand-alone PHP validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax. Why Another Validation Library? Valitron was created out of frustration with other validation libraries that have dependencies on large components from other frameworks unrelated to validation like [...]]]></description>
				<content:encoded><![CDATA[<p><a href="https://github.com/vlucas/valitron">Valitron</a> is a simple, minimal and elegant stand-alone PHP validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax.</p>
<h3>Why Another Validation Library?</h3>
<p><a href="https://github.com/vlucas/valitron">Valitron</a> was created out of frustration with other validation libraries that have dependencies on large components from other frameworks unrelated to validation like <a href="https://github.com/illuminate/validation">Illuminate Validation</a> (laravel 4) requiring <a href="http://symfony.com/doc/master/components/http_foundation/index.html">Symfony HttpFoundation</a>, pulling in a ton of extra files that aren&#8217;t needed for basic validation. It also has purposefully simple syntax used to run all validations in one call instead of individually validating each value by instantiating new classes and validating values one at a time like <a href="https://github.com/Respect/Validation">Respect Validation</a> requires you to do. Valitron also has a focus on being concise &#8211; validation rules are just a single line per rule, and can include multiple fields in an array. This is handy, because in most use cases, a single validation rule &#8211; like &#8220;required&#8221; will be applied to many fields, so it doesn&#8217;t make sense to start with the field first like <a href="https://github.com/fuelphp/validation">Fuel Validation</a> does.</p>
<p>In short, Valitron is everything you&#8217;ve been looking for in a validation library but haven&#8217;t been able to find until now: simple pragmatic syntax, lightweight code that makes sense, extensibility for custom callbacks and validations, good test coverage, and no dependencies.</p>
<h3>Usage Example</h3>
<p>Valitron is made to setup all your validation rules on the fields you need, and then run all the validations in one call. This is better than validating the fields one-by-one, because that approach causes a lot of &#8220;if&#8221; statements and branching logic that doesn&#8217;t make the resulting code any better than doing the validations by hand (which obviously sucks).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$v</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Valitron\Validator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Input array</span>
<span style="color: #000088;">$v</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'date_start'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$v</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Email uses filter_var</span>
<span style="color: #000088;">$v</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dateAfter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'date_start'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> \DateTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// After today</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Yay! We're all good!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Errors</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>More usage examples and documentation can be found on the <a href="https://github.com/vlucas/valitron">Valitron GitHub Page</a>. Valitron is <a href="https://packagist.org/packages/vlucas/valitron">on Packagist</a>, and can be installed via <a href="http://getcomposer.org/">Composer</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/valitron-the-simple-validation-library-that-doesnt-suck/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/valitron-the-simple-validation-library-that-doesnt-suck/</feedburner:origLink></item>
		<item>
		<title>Introducing Bullet: The Functional PHP Micro-Framework</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/lwQ5CIV0l5Q/</link>
		<comments>http://www.vancelucas.com/blog/introducing-bullet-the-functional-php-micro-framework/#comments</comments>
		<pubDate>Thu, 20 Dec 2012 14:48:47 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[micro-framework]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=904</guid>
		<description><![CDATA[Bullet is a new PHP micro-framework with a unique functional approach to URL routing that allows for more flexibility and requires less verbosity than the more typical full route+callback approach found in other micro-frameworks. The Problem with Independent Scope The main problem with most micro-frameworks and even full-stack MVC frameworks that leads to code duplication is [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://bulletphp.com">Bullet</a> is a new PHP micro-framework with a unique functional approach to URL routing that allows for more flexibility and requires less verbosity than the more typical full route+callback approach found in other micro-frameworks.</p>
<h3>The Problem with Independent Scope</h3>
<p>The main problem with most micro-frameworks and even full-stack MVC frameworks that leads to code duplication is that the callback or method executed to perform the action and respond to the URL route lives fully within its own scope. This means that you are forced to repeat a lot of setup code across URL route handlers that load the same resource, authorize it, etc.</p>
<p>Some typical micro-framework code might look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// View single post</span>
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/posts/:id'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> Post<span style="color: #339933;">::</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    check_user_acl_for<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Delete post</span>
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/posts/:id'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> Post<span style="color: #339933;">::</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    check_user_acl_for<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Edit post</span>
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/posts/:id/edit'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> Post<span style="color: #339933;">::</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    check_user_acl_for<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You may be able to move the ACL check to a middleware layer or &#8220;before&#8221; hook if the framework supports it, but there is always a certain amount of duplicate code you will either never be able to get rid of, or have to jump through hoops to get rid of (like adding more abstraction or re-checking the current URL, etc).</p>
<h3>The Benefits of Shared Scope</h3>
<p>Bullet uses a unique nested callback style that splits the URL by directory separator and only handles a single part of the URL at a time with it&#8217;s own callback. At first blush, this approach might seem like more work, but the key to how Bullet works is that nested closures &#8211; by definition &#8211; can use variables defined in the scope of their parent. This leads to some pretty powerful and profund capabilities that can only be done using the same nested closure style that Bullet uses.<span id="more-904"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'int'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> Post<span style="color: #339933;">::</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        check_user_acl_for<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// View (GET)</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// ...            </span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Delete</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// ...</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Edit ('edit' path added after id)</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// ...   </span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice in the example here how the code to load the desired post and perform the ACL check only have to be run ONCE. Any other code or URL routes below that point will automatically be safe and can <code>use($post)</code> to get access to the already loaded Post object.</p>
<h3>Other Advantages and Positive Side-Effects</h3>
<p>Since Bullet&#8217;s URL routes handle only a single path segment at a time and are relative to the parent execution scope, it opens up all kinds of possibilities for code re-use unimaginable in most other PHP frameworks today. The first and perhaps most obvious one is that URL routes can be crafted however you want, and can be nested unlimited levels deep with no restrictions beyond your imagination. The second one is that it becomes trivially easy to do things that are inexplicably difficult with other frameworks like create a base version folder for an API like &#8220;v1&#8243; or &#8220;v2&#8243; that then includes the other main paths below it like &#8220;v1/posts&#8221; and &#8220;v2/events&#8221;.</p>
<p>Perhaps the most significant benefit of this approach is that if you logically separate your routes into different include files (&#8216;posts.php&#8217;, &#8216;events.php&#8217;, &#8216;comments.php&#8217;, etc.), you can include them inside other route handlers, and since both PHP includes and closures are context-sensitive, they will work perfectly and will act as nested routes from whatever path you include them in. Bullet even has a built-in <code>url</code> method that helps build context-sensitive URLs that can be dynamically nested in-context from the current URL path.</p>
<p>The classic use-case for this nesting functionality is an &#8216;admin&#8217; path:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    some_acl_check_to_ensure_admin_that_throws_exception_if_not<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'posts.php'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// For /admin/posts ...</span>
    <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'events.php'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// For /admin/events ...</span>
    <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'comments.php'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// For /admin/comments ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>A lot of other frameworks &#8211; if they can even support doing this &#8211; use additional concepts like route namespaces to solve this problem. With Bullet, the usage is simple and straightforward, and the logic is simple and easy to understand &#8211; this is how bullet already works, so there are no new concepts in play. There are no routing tables or pre-determined rules that make this impossible to do, and the concepts here are all native to PHP and fully leverage how PHP already works &#8211; It&#8217;s just one more path to declare.</p>
<h3>Polymorphic Code Re-Use</h3>
<p>Taking this logic even further, you can create a file with routes that are intended for polymorphic-style code re-use, like allowing &#8216;comments.php&#8217; to be nested within any other path &#8211; in our case, both &#8216;posts&#8217; and &#8216;events&#8217;. Such use might look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'int'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> Post<span style="color: #339933;">::</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        check_user_acl_for<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// View (GET)</span>
        <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Use array notation for variable passing on the $app instance</span>
            <span style="color: #666666; font-style: italic;">// Tell comments to load comments for post_id</span>
            <span style="color: #000088;">$app</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'comments'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'type_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Include our nested comments</span>
            <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'comments.php'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will be 'posts/42/comments'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Method handlers ensure the FULL path is matched, so comments.php will not get included twice</span>
    <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Use array notation for variable passing (via Pimple)</span>
        <span style="color: #000088;">$app</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'comments'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// all comments for 'post' type</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Include our nested comments</span>
        <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'comments.php'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will be 'posts/comments'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This allows the re-use of specific paths and common functionality by nesting them in multiple contexts with a simple PHP include/require. Bullet is setup for this out of the box, and even encourages this type of code re-use through features like relative URL building &#8211; another unique feature among PHP frameworks:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// RELATIVE url (/posts/25/comments/57, /events/9/comments/57, /comments/57)</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./comments/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ROOT url (always /comments/57)</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/comments/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Wrapping Up</h3>
<p>This has been a brief overview of the main benefits, but there&#8217;s a lot more to get excited about regarding Bullet, and there is <a href="http://bulletphp.com">lots of explanation and documentation up on the Bullet site</a> to dig through. It&#8217;s a very unique PHP framework that fully embraces and helps your app automatically conform to the HTTP spec, and I think you&#8217;ll love using it.</p>
<h3>Get Bullet</h3>
<p>View or fork the <a href="https://github.com/vlucas/bulletphp">Source on GitHub</a></p>
<p>Visit the main <a href="http://bulletphp.com">Bullet Website</a></p>
<p>See the <a href="https://packagist.org/packages/vlucas/bulletphp">Composer Package</a> on Packagist</p>
<p>Make sure to <a href="http://twitter.com/vlucas">let me know</a> if you start using it or if you have any questions or awesome ideas.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/introducing-bullet-the-functional-php-micro-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/introducing-bullet-the-functional-php-micro-framework/</feedburner:origLink></item>
		<item>
		<title>Handling Exceptions in Gearman Tasks (Even Background Ones)</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/lc3Fs7v2YBo/</link>
		<comments>http://www.vancelucas.com/blog/handling-exceptions-in-gearman-tasks-even-background-ones/#comments</comments>
		<pubDate>Tue, 31 Jul 2012 17:03:58 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[background jobs]]></category>
		<category><![CDATA[gearman]]></category>
		<category><![CDATA[workers]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=905</guid>
		<description><![CDATA[I recently had some issues with Gearman tasks throwing exceptions and killing the whole Gearman daemon. This made it nearly impossible to trace errors back to their origin, because the logged exception stack trace didn&#8217;t provide much useful information, because it just logged where it failed in Gearman &#8211; not the actual file and line [...]]]></description>
				<content:encoded><![CDATA[<p>I recently had some issues with Gearman tasks throwing exceptions and killing the whole Gearman daemon. This made it nearly impossible to trace errors back to their origin, because the logged exception stack trace didn&#8217;t provide much useful information, because it just logged where it failed in Gearman &#8211; not the actual file and line of code that was doing the work. I dug into the code and started trying things like <a href="http://us.php.net/manual/en/gearmanclient.setexceptioncallback.php">GearmanClient::setExceptionCallback</a> and running the tasks, but since the tasks were being run with <a href="http://us.php.net/manual/en/gearmanclient.addtaskbackground.php">addTaskBackground</a> instead of just <a href="http://us.php.net/manual/en/gearmanclient.addtask.php">addTask</a>, the callbacks were never getting executed, and I still was not able to do anything to handle exceptions for the jobs that were being run (and they were still killing the Gearman daemon). Clearly, I was going to have to get a little more creative.</p>
<p>The only other place to add code that will catch exceptions for all jobs run is in the GearmanWorker::addFunction method. So I looked at the following one-liner for adding named job callbacks:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$worker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFunction</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$task</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And replaced it with a closure that uses a try/catch and then logs any exceptions to <a href="http://www.exceptional.io/">Exceptional</a> so we can see the full stack trace and exact point of failure for any job &#8211; even background jobs:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$worker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFunction</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$task</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	try <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$task</span><span style="color: #339933;">,</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>\Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> GEARMAN_WORK_EXCEPTION<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Gearman: CAUGHT EXCEPTION: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Send exception to Exceptional so it can be logged with details</span>
		Exceptional<span style="color: #339933;">::</span><span style="color: #004000;">handle_exception</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And it works beautifully. Now all the jobs are run, the Gearman daemon is never killed by a PHP process, and all the exceptions are logged with full granular details that makes it easy to troubleshoot and fix any errors.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/handling-exceptions-in-gearman-tasks-even-background-ones/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/handling-exceptions-in-gearman-tasks-even-background-ones/</feedburner:origLink></item>
		<item>
		<title>Confoo 2012 Montreal</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/r0m7urpFIg4/</link>
		<comments>http://www.vancelucas.com/blog/confoo-2012-montreal/#comments</comments>
		<pubDate>Sat, 14 Apr 2012 04:15:23 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Speaking Engagements]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=875</guid>
		<description><![CDATA[My second year speaking at Confoo was more fun than my first. This year I met a lot of new people and had a lot of interesting discussions, particularly in the CMS room and at lunch after my Stackbox CMS presentation. The whole philosophy and approach of Stackbox seemed to have stuck a chord with [...]]]></description>
				<content:encoded><![CDATA[<p>My second year speaking at <a href="http://confoo.ca">Confoo</a> was more fun than my first. This year I met a lot of new people and had a lot of interesting discussions, particularly in the CMS room and at lunch after my <a title="Stackbox CMS" href="http://stackboxcms.com">Stackbox CMS</a> presentation. The whole philosophy and approach of Stackbox seemed to have stuck a chord with other people passionate about CMSes and a lot of discussion emerged about different CMS concepts and how to integrate them into the various CMSes around. The whole edit-on-page approach of Stackbox isn&#8217;t new, but it&#8217;s striking how much simpler it is for the end user than most existing CMSes that are used today &#8211; <a href="http://drupal.org">Drupal</a>, <a href="http://joomla.org">Joomla</a>, and <a href="http://wordpress.org">WordPress</a> included. I think <strong>all</strong> CMSes should strive to enable on-page and in-place editing wherever possible &#8211; it really makes a huge difference in usability.</p>
<p>The second talk I gave at Confoo was <a href="http://confoo.ca/en/2012/session/hierarchical-mvc-hmvc-what-why-and-how">Hierarchical MVC (HMVC) &#8211; What, Why, and How</a> - an architectural talk I have been wanting to give at several conferences for a little while now (but had until now not been accepted). The talk was very well received, and hopefully helped at least a few people though some of the tougher architectural decisions they might be facing in their own projects. The gist of the talk is that HMVC can help break up code into &#8220;widget&#8221; type blocks, and can go further down the path of fully separating concerns than more strict traditional MVC can. HMVC is all about code re-use across multiple places, like a comment module that is dispatched to anywhere you display comments across multiple types of content (blog posts, articles, pages, events, etc.). Traditional MVC forces you to use view partials, different layouts, duplicate code, or some other separate widget system to achieve the same level of flexibility you get from HMVC.</p>
<p>One of the best things I like about Confoo is the sheer diversity of the schedule. There were 10 tracks this year, with talks spanning across all types of technologies, markets, and languages, like PHP, Ruby, Python, .NET, Java, and JavaScript to Scaling, Startups, CMSes and Agile. Confoo is the most technologically diverse conference I have had the privilege of speaking at, and I benefit from that diversity every year by expanding my horizons a little bit. While I was there, I attended a <a href="http://confoo.ca/en/2012/session/renee">talk</a> on a Ruby framework called <a href="http://reneerb.com/">Renee</a> by <a href="https://github.com/joshbuddy">Joshua Hull</a>. Even though most of my background is in PHP and I was presenting with PHP examples and projects for my talks, I use Ruby on Rails quite a bit for client work at my company <a href="http://brightb.it">Brightbit</a> and was working on a Rails API at the time, so I thought I&#8217;d check it out. I had been thinking about better REST frameworks beyond MVC for a little while, and the talk inspired me to try the same nested callback style with closures in PHP, and I started hacking together the very first (crude) implementation of <a href="https://github.com/vlucas/bulletphp">Bullet</a> on the plane ride back home the next day, just to see if it was possible. I had the basic concept working well in under an hour thanks to PHP 5.3&#8242;s awesome closures. That&#8217;s why I like and value the diversity at Confoo so much. You get to see things that are going on in other languages and expand your horizon a bit, then you can bring those benefits back to another language you work in, and benefit even more people. I&#8217;ve fleshed out Bullet a little more now and made it a proper project, but the details of that are for another blog post. For now, I leave you with the slide decks I presented at Confoo.</p>
<h3>Slide Decks</h3>
<p><a href="https://speakerdeck.com/u/vlucas/p/stackbox-cms-next-generation-content-management">Stackbox CMS Slides</a></p>
<p><a href="https://speakerdeck.com/u/vlucas/p/hierarchical-mvc-what-why-how">Hierarchical MVC (HMVC) Slides</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/confoo-2012-montreal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/confoo-2012-montreal/</feedburner:origLink></item>
		<item>
		<title>Nginx + PHP-FPM Blank Pages with PHAR Packages</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/zf6BapyqlAE/</link>
		<comments>http://www.vancelucas.com/blog/nginx-php-fpm-blank-pages-with-phar-packages/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 16:02:20 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[phar]]></category>
		<category><![CDATA[php 5.3]]></category>
		<category><![CDATA[php-fpm]]></category>
		<category><![CDATA[suhosin]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=858</guid>
		<description><![CDATA[Ran into this issue when setting up a new VPS for AutoRidge. This happens when using Nginx and PHP-FPM with PHP 5.3+ and the Suhosin patch when trying to run a PHP script using a PHAR package. From what I can gather, the Suhosin patch basically blocks PHP include/require functions from executing files ending with [...]]]></description>
				<content:encoded><![CDATA[<p>Ran into this issue when setting up a new VPS for <a href="http://autoridge.com">AutoRidge</a>. This happens when using Nginx and PHP-FPM with PHP 5.3+ and the Suhosin patch when trying to run a PHP script using a PHAR package. From what I can gather, the Suhosin patch basically blocks PHP include/require functions from executing files ending with .phar, which results in a PHP segfault that leaves no trace of any error at all. This is what makes this error so frustratingly difficult to track down &#8211; there is no trace left in any logs about what is happening or that any PHP error even occurred at all.</p>
<p>The solution is to open your &#8220;suhosin.ini&#8221; file to ensure the Suhosin patch is allowing PHP to open and execute PHAR files.</p>
<p>Mine was at:</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">/etc/php5/conf.d/suhosin.ini</pre></td></tr></table></div>

<p></code><br />
If your suhosin config file is not there and you don&#8217;t know where it is, run this:</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">locate suhosin.ini</pre></td></tr></table></div>

<p></code><br />
Find the config key &#8220;suhosin.executor.include.whitelist&#8221; and add &#8220;phar&#8221; to it.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">suhosin.executor.include.whitelist =&quot;phar&quot;</pre></td></tr></table></div>

<p></code><br />
Then restart PHP-FPM and Nginx and you should be good to go!</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">/etc/init.d/php5-fpm restart
/etc/init.d/nginx restart</pre></td></tr></table></div>

<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/nginx-php-fpm-blank-pages-with-phar-packages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/nginx-php-fpm-blank-pages-with-phar-packages/</feedburner:origLink></item>
		<item>
		<title>Excessive Data Usage with iPhone 4S / iOS5</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/_GNR0Ja8JNg/</link>
		<comments>http://www.vancelucas.com/blog/excessive-data-usage-with-iphone-4s-ios5/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 23:38:18 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[ios5]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=818</guid>
		<description><![CDATA[Got a new iPhone 4S or recently upgraded your iPhone 4 or 3GS to IOS5 and noticing unusually high cellular data usage? Are you close to exceeding your data usage limit when you never have before? Have you already exceeded it? You are not alone. My wife recently traded in her old iPhone 3G for [...]]]></description>
				<content:encoded><![CDATA[<p>Got a new iPhone 4S or recently upgraded your iPhone 4 or 3GS to IOS5 and noticing unusually high cellular data usage? Are you close to exceeding your data usage limit when you never have before? Have you already exceeded it? You are <a href="http://forums.wireless.att.com/t5/Apple-iPhone/Insane-data-usage-with-the-new-iPhone-4s/td-p/2952781">not</a> <a href="https://discussions.apple.com/thread/3410990">alone</a>.</p>
<p>My wife recently traded in her old iPhone 3G for a shiny new iPhone 4S, and within 10 days had exceeded her 200MB AT&amp;T data plan limit when she has never exceeded it before. So what gives? It&#8217;s a new phone, and Siri does transmit voice data back to Apple&#8217;s servers, but could that really have caused the usage?</p>
<h3>Fixing The Data Usage Problem</h3>
<p>Turns out the problem is that Apple ships iOS5 with iCloud data syncing over cellular networks ON by default. This means all the documents and data stored on your phone for all the applications you have installed on your phone will be automatically uploaded to iCloud, decimating your puny cellular data plan you thought you&#8217;d never burn through.</p>
<p>If you are experiencing super high data usage on iOS5, just follow these steps:</p>
<ol>
<li>Navigate to: Settings -&gt; iCloud -&gt; Documents &amp; Data -&gt; Use Cellular</li>
<li>Turn it OFF</li>
<li>Fight with AT&amp;T (or other wireless carrier) about your excessive data usage charges</li>
</ol>
<div>Why Apple would ever think its a good default to sync data and documents for all your phone&#8217;s applications over a cellular network in the world of severely limited data plans and usage based billing is beyond me. This, and the <a href="https://discussions.apple.com/thread/3391947">widespread</a> <a href="http://gizmodo.com/5859278/the-iphone-4s-battery-problem-is-probably-not-because-of-faulty-hardware">battery</a> <a href="http://www.macworld.com/article/163200/2011/10/troubleshoot_iphone4s_battery.html">issues</a> with the iPhone 4S and iOS5 seem to indicate Apple didn&#8217;t do nearly enough field testing with the new phone or OS. Let&#8217;s home they learn from this in the future.</div>
<h3>Usage Before iPhone 4S</h3>
<p><a href="http://www.vancelucas.com/wp-content/uploads/2011/11/Screen-shot-2011-11-16-at-5.08.03-PM.png"><img class="alignnone size-full wp-image-821" title="Screen shot 2011-11-16 at 5.08.03 PM" src="http://www.vancelucas.com/wp-content/uploads/2011/11/Screen-shot-2011-11-16-at-5.08.03-PM.png" alt="" width="529" height="409" /></a></p>
<p>Some months have clearly higher data usage than others depending on how long we were away from home or what we did while we were out with out phones, but we were never in any real danger of going over the low 200MB limit before.</p>
<h3>Data Usage after 10 days with the iPhone 4S</h3>
<p><a href="http://www.vancelucas.com/wp-content/uploads/2011/11/Screen-shot-2011-11-16-at-5.09.04-PM.png"><img class="alignnone size-full wp-image-822" title="Screen shot 2011-11-16 at 5.09.04 PM" src="http://www.vancelucas.com/wp-content/uploads/2011/11/Screen-shot-2011-11-16-at-5.09.04-PM.png" alt="" width="283" height="170" /></a><br />
On the left (green) is my data usage with an Android device, Right (red) is my wife&#8217;s data usage this month with the new iPhone 4S.</p>
<p>My wife got a text alert for 65% data plan usage on Monday, and then one on Friday for 80%, and one the day after on Saturday saying she had gone over her limit (exceeding 100% of the plan) and that we would be billed an additional $15 by AT&amp;T for the overage.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/excessive-data-usage-with-iphone-4s-ios5/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/excessive-data-usage-with-iphone-4s-ios5/</feedburner:origLink></item>
		<item>
		<title>How to add Photos to the iPhone Simulator</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/3ifAzqaD7f0/</link>
		<comments>http://www.vancelucas.com/blog/how-to-add-photos-to-the-iphone-simulator/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 19:20:04 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[ios simulator]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone simulator]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=796</guid>
		<description><![CDATA[Building an app that needs to access the photo library but don&#8217;t have any photos in the iPhone simulator? No problem. Follow these simple steps to import photos into the iPhone Simulator: Open the iPhone Simulator Browse to the photo you want to put into the simulator (Finder or web browser) Click and drag the [...]]]></description>
				<content:encoded><![CDATA[<p>Building an app that needs to access the photo library but don&#8217;t have any photos in the iPhone simulator? No problem. Follow these simple steps to import photos into the iPhone Simulator:</p>
<ol>
<li>Open the iPhone Simulator</li>
<li>Browse to the photo you want to put into the simulator (Finder or web browser)</li>
<li>Click and drag the photo over the simulator window. A green &#8220;plus&#8221; icon should appear under your cursor with the simulator frame highlighted. Drop the photo.</li>
<li>Mobile Safari should open on the simulator to the location of the image you just dragged and dropped over it</li>
<ol>
<li>Note that if the image you dropped is linked in a webpage, it will open the link instead of the image URL.</li>
</ol>
<li>Click the mouse down over the image and hold it until a popup window appears.</li>
<li>Click &#8220;Save Image&#8221;</li>
</ol>
<div>The photo will now reside in the &#8220;Saved Photos&#8221; album on the iPhone Simulator. Rince and repeat as many times as you need to get all your photos in the album.</div>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/how-to-add-photos-to-the-iphone-simulator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/how-to-add-photos-to-the-iphone-simulator/</feedburner:origLink></item>
		<item>
		<title>Easier Titanium XHR and AJAX Requests</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/wF0Lga3M_kM/</link>
		<comments>http://www.vancelucas.com/blog/easier-titanium-xhr-and-ajax-requests/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 15:15:16 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Titanium]]></category>
		<category><![CDATA[mobile app]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=787</guid>
		<description><![CDATA[One question I see a lot on the Appcelerator Titanium Developer Q&#38;A is how to perform AJAX requests and/or work with APIs, etc. There is a built-in way to do this with the Ti.Network.HTTPClient module that is pretty easy, but it does have a few drawbacks and &#8220;gotchas&#8221;, like executing the &#8220;success&#8221; event for ANY [...]]]></description>
				<content:encoded><![CDATA[<p>One question I see a lot on the <a href="http://developer.appcelerator.com/questions">Appcelerator Titanium Developer Q&amp;A</a> is how to perform AJAX requests and/or work with APIs, etc. There is a built-in way to do this with the <a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network.HTTPClient-object.html">Ti.Network.HTTPClient</a> module that is pretty easy, but it does have a few drawbacks and &#8220;gotchas&#8221;, like executing the &#8220;success&#8221; event for ANY returned status code &#8211; even 500 errors. Since working with APIs is so common with mobile apps, I made a wrapper function modeled after <a href="http://api.jquery.com/jQuery.ajax/">jQuery&#8217;s $.ajax method</a> that I use in all my apps. It shortens the syntax quite a bit and is much more familiar to those who are used to using jQuery.</p>
<p>The usage looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Geocode input text location</span>
app.<span style="color: #660066;">utils</span>.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://maps.googleapis.com/maps/api/geocode/json?address='</span> <span style="color: #339933;">+</span> txtLocation.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">'&amp;region=us&amp;sensor=true'</span><span style="color: #339933;">,</span>
    method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'get'</span><span style="color: #339933;">,</span>
    success<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xhr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>xhr.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;OK&quot;</span> <span style="color: #339933;">==</span> data.<span style="color: #660066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">var</span> res <span style="color: #339933;">=</span> data.<span style="color: #660066;">results</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Location: &quot;</span> <span style="color: #339933;">+</span> res.<span style="color: #660066;">geometry</span>.<span style="color: #660066;">location</span>.<span style="color: #660066;">lat</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">', '</span> <span style="color: #339933;">+</span> res.<span style="color: #660066;">geometry</span>.<span style="color: #660066;">location</span>.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #006600; font-style: italic;">// Do something with coordinates</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createAlertDialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Geocode Error'</span><span style="color: #339933;">,</span>
                message<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Unable to geocode location input'</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    error<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xhr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createAlertDialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Geocode Error'</span><span style="color: #339933;">,</span>
            message<span style="color: #339933;">:</span> <span style="color: #3366CC;">'No location matches found. Please try something else.'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And the actual code for the utility function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Application Utilities and Helper Methods
 **/</span>
<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>_app<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    _app.<span style="color: #660066;">utils</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// AJAX method that mimmicks jQuery's</span>
    _app.<span style="color: #660066;">utils</span>.<span style="color: #660066;">ajax</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>_props<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Merge with default props</span>
        <span style="color: #000066; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> _app.<span style="color: #660066;">combine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span>
            url<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
            data<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
            contentType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'application/json'</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Ti API Options</span>
            async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
            autoEncodeUrl<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Callbacks</span>
            success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
            error<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
            beforeSend<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
            complete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> _props<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;XHR &quot;</span> <span style="color: #339933;">+</span> o.<span style="color: #660066;">method</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: <span style="color: #000099; font-weight: bold;">\n</span>'&quot;</span> <span style="color: #339933;">+</span> o.<span style="color: #660066;">url</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">Network</span>.<span style="color: #660066;">createHTTPClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            autoEncodeUrl<span style="color: #339933;">:</span> o.<span style="color: #660066;">autoEncodeUrl</span><span style="color: #339933;">,</span>
            async<span style="color: #339933;">:</span> o.<span style="color: #660066;">async</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// URL</span>
        xhr.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">method</span><span style="color: #339933;">,</span> o.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Request header</span>
        xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">,</span> o.<span style="color: #660066;">contentType</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">beforeSend</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            o.<span style="color: #660066;">beforeSend</span><span style="color: #009900;">&#40;</span>xhr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Errors</span>
        xhr.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        xhr.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'XHR &quot;onerror&quot; ['</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">status</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']: '</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">responseText</span><span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> o.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">return</span> o.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Success</span>
        xhr.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// Log</span>
            Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'XHR &quot;onload&quot; ['</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">status</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']: '</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">responseText</span><span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Success = 1xx or 2xx (3xx = redirect)</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">status</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">400</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> o.<span style="color: #660066;">success</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">return</span> o.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'XHR success function threw Exception: '</span> <span style="color: #339933;">+</span> e <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #006600; font-style: italic;">// Error = 4xx or 5xx</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'XHR error ['</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">status</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']: '</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">responseText</span><span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> o.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> o.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Send</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'application/x-www-form-urlencoded'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Completed</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> o.<span style="color: #660066;">complete</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> o.<span style="color: #660066;">complete</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// And it does depend on this code below to combine object properties:</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Extend an object with the properties from another </span>
    <span style="color: #006600; font-style: italic;">// (thanks Dojo - http://docs.dojocampus.org/dojo/mixin)</span>
    <span style="color: #000066; font-weight: bold;">var</span> empty <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">function</span> mixin<span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*Object*/</span> target<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*Object*/</span> source<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> name<span style="color: #339933;">,</span> s<span style="color: #339933;">,</span> i<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>name <span style="color: #000066; font-weight: bold;">in</span> source<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            s <span style="color: #339933;">=</span> source<span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>name <span style="color: #000066; font-weight: bold;">in</span> target<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> s <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>name <span style="color: #000066; font-weight: bold;">in</span> empty<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> empty<span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                target<span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> target<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Object</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    _app.<span style="color: #660066;">mixin</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*Object*/</span> obj<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*Object...*/</span> props<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> l<span style="color: #339933;">=</span>arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            mixin<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> obj<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Object</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Create a new object, combining the properties of the passed objects with the last arguments having</span>
    <span style="color: #006600; font-style: italic;">// priority over the first ones</span>
    _app.<span style="color: #660066;">combine</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*Object*/</span> obj<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*Object...*/</span> props<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> newObj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l<span style="color: #339933;">=</span>arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            mixin<span style="color: #009900;">&#40;</span>newObj<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> newObj<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>app<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The particular organization of this utilities file assumes that your app structure follows the organization model shown in the <a href="https://github.com/appcelerator-titans/tweetanium">Tweetanium example app</a> (using the &#8216;app&#8217; namespace within a single window context), but is easy to adapt if you are not.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/easier-titanium-xhr-and-ajax-requests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/easier-titanium-xhr-and-ajax-requests/</feedburner:origLink></item>
		<item>
		<title>Count the Number of Object keys/properties in Node.js</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/eiQt-U-WUZA/</link>
		<comments>http://www.vancelucas.com/blog/count-the-number-of-object-keysproperties-in-node-js/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 17:26:25 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=768</guid>
		<description><![CDATA[When using the excellent formidable library to handle file uploads, I needed to get a count of the number of files unloaded in a multi-part form. Javascript arrays have a .length property that you can use, but objects do not. I instinctively typed: 1 files.length Which returned undefined. So if there is no length property present, an [...]]]></description>
				<content:encoded><![CDATA[<p>When using the excellent <a href="https://github.com/felixge/node-formidable">formidable</a> library to handle file uploads, I needed to get a count of the number of files unloaded in a multi-part form. Javascript arrays have a <code>.length</code> property that you can use, but objects do not. I instinctively typed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="js" style="font-family:monospace;">files.length</pre></td></tr></table></div>

<p>Which returned <code>undefined</code>. So if there is no length property present, an easy way to count the number of keys or properties of an object in ES5-compliant javascript environments like <a href="http://nodejs.org">Node.js</a> is to use the Object prototype directly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="js" style="font-family:monospace;">Object.keys(files).length</pre></td></tr></table></div>

<p>A little more typing, but it is fast, efficient, and most importantly: already built-in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/count-the-number-of-object-keysproperties-in-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/count-the-number-of-object-keysproperties-in-node-js/</feedburner:origLink></item>
		<item>
		<title>php|tek 2011</title>
		<link>http://feedproxy.google.com/~r/VanceLucas/~3/YFA89oagPR0/</link>
		<comments>http://www.vancelucas.com/blog/php-tek-2011/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 14:35:23 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Speaking Engagements]]></category>
		<category><![CDATA[Titanium]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[speaking]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=802</guid>
		<description><![CDATA[Chicago &#8211; May 24-27 php&#124;tek in Chicago was fun as always. It is the best PHP conference I have ever been to, which makes sense, given that it is focused solely on PHP and surrounding technologies. The best thing about the conference is the community feeling in general. You get a real sense that everyone [...]]]></description>
				<content:encoded><![CDATA[<div style="float: right; padding: 10px;"><a href="http://tek11.phparch.com/"><img src="http://www.vancelucas.com/wp-content/uploads/2011/05/tek11_SPEAKER_badge_150x150.png" alt="php|tek 2011 Speaker" /></a><br />Chicago &#8211; May 24-27</div>
<p>php|tek in Chicago was fun as always. It is the best PHP conference I have ever been to, which makes sense, given that it is focused solely on PHP and surrounding technologies. The best thing about the conference is the community feeling in general. You get a real sense that everyone there really cares about PHP and is heavily invested in it, which is good for moving the whole language and ecosystem forward.</p>
<p>I myself gave two talks &#8211; one about <a href="http://stackboxcms.com">Stackbox CMS</a>, a new CMS project I have been working on, and one about <a href="http://appcelerator.com">Apppcelerator Titanium Mobile</a> since I have been working with it a lot lately. The presentations are embedded below.<span id="more-802"></span></p>
<h3><a href="http://www.slideshare.net/vlucas/crossplatform-mobile-development-with-titanium" title="Cross-Platform Mobile Development with Titanium" target="_blank">Cross-Platform Mobile Development with Titanium</a></h3>
<div style="width:425px" id="__ss_7238077"><iframe src="http://www.slideshare.net/slideshow/embed_code/7238077" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></div>
<h3><a href="http://www.slideshare.net/vlucas/stackbox-cms-nextgeneration-content-management" title="Stackbox CMS: Next-Generation Content Management" target="_blank">Stackbox CMS: Next-Generation Content Management</a></h3>
<div style="width:425px" id="__ss_8153672"><iframe src="http://www.slideshare.net/slideshow/embed_code/8153672" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></div>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/php-tek-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vancelucas.com/blog/php-tek-2011/</feedburner:origLink></item>
	</channel>
</rss>
