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

<channel>
	<title>Hasheado Blog</title>
	<atom:link href="http://www.hasheado.com/feed/" rel="self" type="application/rss+xml"/>
	<link>http://www.hasheado.com</link>
	<description>Just another Developer blog</description>
	<lastBuildDate>Fri, 22 Apr 2016 03:36:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.8.28</generator>
	<itunes:explicit>no</itunes:explicit><itunes:subtitle>Just another Developer blog</itunes:subtitle><item>
		<title>Build simple REST API with PHP – Part 3</title>
		<link>http://www.hasheado.com/build-simple-rest-api-with-php-part-3/</link>
		<comments>http://www.hasheado.com/build-simple-rest-api-with-php-part-3/#comments</comments>
		<pubDate>Sun, 20 Mar 2016 02:24:49 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Slim]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[slim]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=291</guid>
		<description><![CDATA[This is the last part of our post serie about building simple REST API with PHP. We can find the two previous ones here: Part 1 Part 2 In the last part we&#8217;ll add Middlewares to add more features to our API and also customize some error outputs to make them more consumable. &#160; Middlewares [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>This is the last part of our post serie about building simple REST API with PHP. We can find the two previous ones here:</p>
<ol>
<li><a href="http://www.hasheado.com/build-simple-rest-api-with-php-part-1/" target="_blank">Part 1</a></li>
<li><a href="http://www.hasheado.com/build-simple-rest-api-with-php-part-2/" target="_blank">Part 2</a></li>
</ol>
<p>In the last part we&#8217;ll add <a href="http://www.slimframework.com/docs/concepts/middleware.html" target="_blank">Middlewares</a> to add more features to our <strong>API</strong> and also customize some error outputs to make them more consumable.</p>
<p>&nbsp;</p>
<h2>Middlewares</h2>
<p>We will add the following three middlewares:</p>
<ul>
<li>ContentTypes: Parses JSON-formatted body from the client (the innermost);</li>
<li>JSON: utility middleware for &#8220;JSON only responses&#8221; and &#8220;JSON encoded bodies&#8221;;</li>
<li>Authentication (outermost).</li>
</ul>
<p>&nbsp;</p>
<h3>ContentType Middleware</h3>
<p>For the ContentType middleware we&#8217;ll use an existing <strong>Slim</strong> middleware, so we just need to add it in our <em>bootstrap.php</em> file:</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">...</span><br />
<span style="color: #666666; font-style: italic;">// Middlewares</span><br />
<span style="color: #666666; font-style: italic;">// Content Type (inner)</span><br />
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> \Slim\Middleware\ContentTypes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This middleware intercepts the HTTP request body and parses it into the appropriate <strong>PHP</strong> data structure if possible; else it returns the HTTP request body unchanged. This is particularly useful for preparing the HTTP request body for an XML or JSON API.</p>
<p>&nbsp;</p>
<h3>JSON Middleware</h3>
<p>Our JSON middleware achieves two best practices: &#8220;JSON only responses&#8221; and &#8220;JSON encoded bodies&#8221;. So, lets add it in our <em>bootstrap.php</em> file:</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> API\Middleware\JSON<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>As we can see if we review a classic Middleware class, the <em><strong>call()</strong></em> method of a Slim middleware is where the action takes place. Here is our JSON middleware class:</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">namespace</span> API\Middleware<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> JSON <span style="color: #000000; font-weight: bold;">extends</span> \Slim\Middleware<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; try <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Force response headers to JSON</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$mediaType</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMediaType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate JSON format</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isJSON</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> \Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JSON data is malformed&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'put'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'patch'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">''</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mediaType</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$mediaType</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">halt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">415</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSON_PRETTY_PRINT<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>If the request method is one of the write-enabled ones (<em>PUT</em>, <em>POST</em>, <em>PATCH</em>) the request content type header must be <em>application/json</em>, if not the application exits with a 415 Unsupported Media Type HTTP status code. If all is right the statement <em><strong>$this->next->call()</strong></em> runs the next middleware in the chain.</p>
<p>&nbsp;</p>
<h3>Authentication Middleware</h3>
<p>To authenticate our api users we&#8217;ll use a third-party library: <a href="https://github.com/tuupola/slim-jwt-auth" target="_blank">JWT Authentication Middleware for Slim</a></p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> \Slim\Middleware\JwtAuthentication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">&quot;secret&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;supersecretkeyyoushouldnotcommittogithub&quot;</span><br />
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This middleware implements JSON Web Token Authentication for Slim Framework. It does not implement OAuth 2.0 authorization server nor does it provide ways to generate, issue or store authentication tokens. It only parses and authenticates a token when passed via header or cookie.</p>
<p>&nbsp;</p>
<h2>Pretty outputs for Errors</h2>
<p>Our API should show useful error messages in pretty format. We need a minimal payload that contains an error code and message. With Slim we can redefine both 404 errors and server errors with the <em>$app->notFound()</em> and <em>$app->error()</em> method respectively.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</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: #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><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$mediaType</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMediaType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$isAPI</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|^/api/v.*$|'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'application/json'</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$mediaType</span> <span style="color: #339933;">||</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$isAPI</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">404</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Not found'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>For other errors we can use and customize the <em>error()</em> method:</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>\Exception <span style="color: #000088;">$e</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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$mediaType</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMediaType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$isAPI</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|^/api/v.*$|'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Standard exception data</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</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><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Custom error data (e.g. Validations)</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/method_exists"><span style="color: #990000;">method_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'getData'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'errors'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$errors</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$log</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#40;</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: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'application/json'</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$mediaType</span> <span style="color: #339933;">||</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$isAPI</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The <em>$app->error()</em> method receives the thrown exception as argument and print it in a pretty way.</p>
<p>Well that&#8217;s, we&#8217;ve developed a basic API with common best practices. I hope these post series are helpful for you guys.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/build-simple-rest-api-with-php-part-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Build simple REST API with PHP – Part 2</title>
		<link>http://www.hasheado.com/build-simple-rest-api-with-php-part-2/</link>
		<comments>http://www.hasheado.com/build-simple-rest-api-with-php-part-2/#respond</comments>
		<pubDate>Thu, 03 Mar 2016 01:31:52 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Slim]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[slim]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=273</guid>
		<description><![CDATA[In our first part of this post series we saw how to bootstrap our API and we defined the end points for it. Now, we&#8217;ll add the necessary logic to make our end points to work. End Points GET /users Here we will get ALL our existing users. 12345678910111213// GET route for all users $app-&#62;get&#40;'/users', [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>In our <a href="http://www.hasheado.com/build-simple-rest-api-with-php-part-1/" target="_blank">first part of this post series</a> we saw how to bootstrap our API and we defined the end points for it. Now, we&#8217;ll add the necessary logic to make our end points to work.</p>
<h2>End Points</h2>
<h3>GET /users</h3>
<p>Here we will get ALL our existing users.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// GET route for all users</span><br />
<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;">'/users'</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;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$users</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$users</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Here, we can add sorting, filtering and/or searching logic</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$users</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>GET /users/:id</h3>
<p>Given a user ID we return that user if exists, if not we return a <em>404</em> error.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// GET route</span><br />
<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;">'/users/: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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Observe how the &#8220;:id&#8221; url parameter is passed as an argument of the callable function.</p>
<p>&nbsp;</p>
<h3>POST /users</h3>
<p>Now, sending POST data to the /users end point we will be able to create a new User.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// POST route, for creating</span><br />
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users'</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;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get POSTed data in body</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate data here</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">for_table</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> \Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Something went wrong.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>PUT /users/:id</h3>
<p>Update an existing user with passed data given a user ID.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// PUT route, for updating</span><br />
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/: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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate data here</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> \Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Something went wrong.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>DELETE /users/:id</h3>
<p>Here given a user ID we will delete the user.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// DELETE route</span><br />
<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;">'/users/: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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$user</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> \Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Something went wrong.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h2>User&#8217;s Phonenumbers</h2>
<p>Now, we will define the end points for our second collection <em><strong>Phonenumbers</strong></em>.</p>
<h3>GET /users/:id/phonenumbers</h3>
<p>In this end point we will return all the associated phonenumbers to a user.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// GET route for all users's phonenumbers</span><br />
<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;">'/users/:id/phonenumbers'</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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumbers</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">orderByDesc</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumbers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phonenumbers</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>GET /users/:id/phonenumbers/:pid</h3>
<p>Now given a user ID and a phone ID we will return that specific phonenumber for the given user.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// GET route for specific user's phonenumber</span><br />
<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;">'/users/:id/phonenumbers/:pid'</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: #339933;">,</span> <span style="color: #000088;">$phoneId</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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">andWhere</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$phoneId</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phonenumber</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>POST /users/:id/phonenumbers</h3>
<p>With this end point we will be able to create/add a phonenumber to a user.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// POST route, for creating a new user's phonenumber</span><br />
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/:id/phonenumbers'</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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get POSTed data in body</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate data here</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">for_table</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumber</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phonenumber</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> \Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Something went wrong.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>PUT /users/:id/phonenumbers/:pid</h3>
<p>PUT route to update a user&#8217;s phonenumber.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// PUT route, for updating a user's phonenumber</span><br />
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/:id/phonenumbers/:pid'</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: #339933;">,</span> <span style="color: #000088;">$phoneId</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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phoneId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get POSTed data in body</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate data here</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumber</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phonenumber</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> \Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Something went wrong.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>DELETE /users/:id/phonenumbers/:pid</h3>
<p>And at the end, we define the route to delete an specific user&#8217;s phonenumber.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// DELETE route, for delete a user's phonenumber</span><br />
<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;">'/users/:id/phonenumbers/:pid'</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: #339933;">,</span> <span style="color: #000088;">$phoneId</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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> \ORM<span style="color: #339933;">::</span><span style="color: #004000;">forTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phoneId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$phonenumber</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> JSON_PRETTY_PRINT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> \Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Something went wrong.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notFound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>Well, we finished defining all our end points to interect against our API. At this point we have a basic API working but still there are thing to do to make it more fully functional. For instance, what if we want to make our API secure? We should add authentication, and we&#8217;ll do it using really great Slim&#8217;s functionality <em><strong>Middlewares</strong></em>. Also, we can ensure all our requests/responses are being talking JSON and/or add some rate limit to don&#8217;t allow overwhelming our API.</p>
<p>In the next and last post we will cover all this. See you soon! :D.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/build-simple-rest-api-with-php-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build simple REST API with PHP – Part 1</title>
		<link>http://www.hasheado.com/build-simple-rest-api-with-php-part-1/</link>
		<comments>http://www.hasheado.com/build-simple-rest-api-with-php-part-1/#comments</comments>
		<pubDate>Fri, 26 Feb 2016 01:47:58 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Slim]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[slim]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=271</guid>
		<description><![CDATA[A web API (Application Programming Interface) is a set of programming instructions to access software applications and consume and share data between them. Developing your own API you can benefit both you and your users and for that we will write about how to build a simple REST API with PHP. To build our API [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>A <strong>web</strong> <em>API</em> (Application Programming Interface) is a set of programming instructions to access software applications and consume and share data between them. Developing your own API you can benefit both you and your users and for that we will write about how to build a simple <a href="https://es.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">REST</a> <a href="https://en.wikipedia.org/wiki/Application_programming_interface" target="_blank">API</a> with <a href="http://php.net" target="_blank">PHP</a>.</p>
<p>To build our <em>API</em> we will use <a href="http://www.slimframework.com/" target="_blank">Slim</a> micro framework. As they mention in their site </p>
<blockquote><p>Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.</p></blockquote>
<p>As we said above an <em>API</em> is an Application Programming Interface for what it should be friendly, simple and easy to use. To help other developers to use our API we should consider writing a good documentation about it, or at least, a very explanatory <strong>README</strong> file. Such documentation need to be a summary of the service&#8217;s scope and the list of methods and access points.</p>
<p>Following the key principles of <em>REST</em>, each resource is represented by a URL, where the action is the HTTP verb used to access it.</p>
<p>A full list of access points is listed below:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">URL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HTTP Verb &nbsp; &nbsp;Operation<br />
/api/users &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GET &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Returns an array of users<br />
/api/users/:id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GET &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Returns the user with id of :id<br />
/api/users &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POST &nbsp; &nbsp; &nbsp; &nbsp; Adds a new user and return it with its id<br />
/api/users/:id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PATCH &nbsp; &nbsp; &nbsp; &nbsp;Partially updates the user with id of :id<br />
/api/users/:id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Updates the complete user with id of :id<br />
/api/users/:id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DELETE &nbsp; &nbsp; &nbsp; Deletes the user with id of :id<br />
<br />
/api/users/:id/phonenumbers &nbsp; &nbsp; &nbsp; GET &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Returns the phonenumbers for the user with id of :id<br />
/api/users/:id/phonenumbers/:pid &nbsp;GET &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Returns the phonenumber with id of :pid for the user with id of :id<br />
/api/users/:id/phonenumbers &nbsp; &nbsp; &nbsp; POST &nbsp; &nbsp; &nbsp; &nbsp; Adds a new phonenumber for the user with id of :id<br />
/api/users/:id/phonenumbers/:pid &nbsp;PATCH &nbsp; &nbsp; &nbsp; &nbsp;Partially updates the phonenumber with id of :pid for the user with id of :id<br />
/api/users/:id/phonenumbers/:pid &nbsp;PUT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Updates the phonenumber with id of :pid for the user with id of :id<br />
/api/users/:id/phonenumbers/:pid &nbsp;DELETE &nbsp; &nbsp; &nbsp; Deletes the phonenumber with id of :pid for the user with id of :id</div></td></tr></tbody></table></div>
<p>The above <em>README</em> is a good start point, but a more complete documentation would be better. You can consider some libraries to generate documentation like <a href="https://developers.google.com/discovery/" target="_blank">Google APIs Discovery Service</a>, <a href="http://apidocjs.com/" target="_blank">apiDoc</a> or <a href="http://swagger.io/" target="_blank">Swagger</a>.</p>
<h2>Setup</h2>
<p>We define our <em>composer.json</em> with our project information and dependencies:</p>
<div class="codecolorer-container yaml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="yaml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span class="br0">&#123;</span><span style="color: green;"><br />
&nbsp; &nbsp; &quot;name&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;yourname/users-management&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &quot;description&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;Simple RESTful API for users management&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &quot;license&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;MIT&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &quot;authors&quot;</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;Your Name&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;email&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;you@yourdomain.com&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#93;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &quot;require&quot;</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#123;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;slim/slim&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;*&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;slim/extras&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;*&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;slim/middleware&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;*&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;monolog/monolog&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;*&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;j4mie/paris&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;*&quot;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;flynsarmy/slim-monolog&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;*&quot;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &quot;archive&quot;</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#123;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;exclude&quot;</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#91;</span><span style="color: #CF00CF;">&quot;vendor&quot;</span>, <span style="color: #CF00CF;">&quot;.DS_Store&quot;</span>, <span style="color: #CF00CF;">&quot;*.log&quot;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span>,<span style="color: green;"><br />
&nbsp; &nbsp; &quot;autoload&quot;</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#123;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;psr-0&quot;</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#123;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;API&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;lib/&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>As we can see in our <em>composer.json</em>, along with <em><strong>Slim</strong></em> we&#8217;re using <em><strong><a href="https://github.com/j4mie/paris" target="_blank">Paris</a></strong></em> (lightweight Active Record implementation on top of <em><strong><a href="https://github.com/j4mie/idiorm" target="_blank">Idiorm</a></strong></em>) to access the database layer and <em><strong>Monolog</strong></em> for logging.</p>
<h2>Bootstrap our API</h2>
<p>The <strong><em>bootstrap.php</em></strong> file is in charge to autoload classes and load configurations.</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// Init application mode</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_ENV</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SLIM_MODE'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$_ENV</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SLIM_MODE'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/getenv"><span style="color: #990000;">getenv</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SLIM_MODE'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>? <a href="http://www.php.net/getenv"><span style="color: #990000;">getenv</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SLIM_MODE'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'dev'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Init and load configuration</span><br />
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$configFile</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/config/config.'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$_ENV</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SLIM_MODE'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_readable"><span style="color: #990000;">is_readable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$configFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">require_once</span> <span style="color: #000088;">$configFile</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">require_once</span> <a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/config/config.default.php'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Create App</span><br />
<span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> API\Application<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'app'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Get log writer</span><br />
<span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Init database</span><br />
try <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; \ORM<span style="color: #339933;">::</span><span style="color: #004000;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dsn'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \ORM<span style="color: #339933;">::</span><span style="color: #004000;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \ORM<span style="color: #339933;">::</span><span style="color: #004000;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>\PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$log</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#40;</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: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Add some Middlewares here (we discuss about this later)</span></div></td></tr></tbody></table></div>
<p>First, we get the environment we are using (could be dev, staging, prod or whatever you want and define) and then we load the environment related configuration, it there is not, we load default configuration. Then we create the application which as you can see it&#8217;s an <em>Application</em> class which extends the basic <strong><em>Slim class</em></strong>; and at the end we try to connect to the database.</p>
<p>Later we will add some <em>Middleware</em> to our app, you can read about what a <em>Middleware</em> is <a href="http://www.slimframework.com/docs/concepts/middleware.html" target="_blank">here</a>.</p>
<h2>Front Controller</h2>
<p>Our front controller <em><strong>index.php</strong></em> is the unique entry point to our API and it&#8217;s where the magic happens. Here we&#8217;ll define the HTTP verbs to interact with our API and be able to list/create/edit/delete our &#8220;<em>Users</em>&#8221; and &#8220;<em>Phonenumbers</em>&#8220;.</p>
<p>Here is our front controller where we&#8217;ll start writing our methods (HTTP verbs):</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #b1b100;">require_once</span> <a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/bootstrap.php'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// API group</span><br />
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">group</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/api'</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;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Version group</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">group</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/v1'</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;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// GET route for all users</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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;">'/users'</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;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// GET route</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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;">'/users/: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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// POST route, for creating</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users'</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;">$app</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// PUT route, for updating</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/: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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// DELETE route</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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;">'/users/: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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// GET route for all users's phonenumbers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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;">'/users/:id/phonenumbers'</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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// GET route for specific user's phonenumber</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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;">'/users/:id/phonenumbers/:pid'</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: #339933;">,</span> <span style="color: #000088;">$phoneId</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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// POST route, for creating a new user's phonenumber</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/:id/phonenumbers'</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: #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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// PUT route, for updating a user's phonenumber</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/:id/phonenumbers/:pid'</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: #339933;">,</span> <span style="color: #000088;">$phoneId</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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// DELETE route, for delete a user's phonenumber</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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;">'/users/:id/phonenumbers/:pid'</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: #339933;">,</span> <span style="color: #000088;">$phoneId</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;">$log</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Some code here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We&#8217;ve created two nested groups, <em>/api</em> and <em>/v1</em>, so we can easily adhere to the &#8220;versioning in the URL&#8221; best practice to versioning APIs.</p>
<p>So far we&#8217;ve installed needed libraries, bootstrapped the app and defined the routes for all the end points our API will have. In the next parts of this post series we will add the logic for every method we&#8217;ve defined and we&#8217;ll add and write some <em>Middlewares</em> to make a fully functional API.</p>
<p>Keep in touch! <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/build-simple-rest-api-with-php-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My favorite stack</title>
		<link>http://www.hasheado.com/my-favorite-stack/</link>
		<comments>http://www.hasheado.com/my-favorite-stack/#respond</comments>
		<pubDate>Tue, 28 Apr 2015 02:32:54 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[stack]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=251</guid>
		<description><![CDATA[As I posted in an earlier post Javascript is conquering the world and in this post I want to share what is my favorite stack for building web applications. For the latest apps I&#8217;m working I really like the following stack: Symfony for the backend, basically the administrator interface. AngularJS for the frontend and UI. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>As I posted in an <a href="http://www.hasheado.com/the-future-of-web-development/" target="_blank">earlier post</a> Javascript is conquering the world and in this post I want to share what is my favorite stack for building web applications.</p>
<p>For the latest apps I&#8217;m working I really like the following stack:</p>
<p><a href="http://symfony.com/" target="_blank">Symfony</a> for the backend, basically the administrator interface.<br />
<a href="https://angularjs.org/" target="_blank">AngularJS</a> for the frontend and UI.<br />
A <a href="http://en.wikipedia.org/wiki/Representational_state_transfer" target="_blank">RESTful</a> API to be consumed by the frontend.</p>
<p>As I&#8217;m working with <strong>Symfony</strong> for a long time I decided to build my backend and administrator based on this PHP framework. For that I use a bundle I&#8217;ve written nammed <a href="https://github.com/emiliano-viada-developer/HasheadoAdminBundle" target="_blank">HasheadoAdminBundle</a>, but also I&#8217;m playing and experimenting with this <a href="https://github.com/javiereguiluz/EasyAdminBundle" target="_blank">EasyAdminBundle</a> written by <strong>Javier Eguiluz</strong>.</p>
<p>For the frontend I&#8217;m using <strong>AngularJS</strong>, a really cool framework to extend HTML and build rich web applications. There is really good documentation to learn Angular in its own website and it also has a great community.</p>
<p>To build the <strong>RESTful API</strong> I&#8217;m using great Symfony bundles like <a href="https://github.com/FriendsOfSymfony/FOSRestBundle" target="_blank">FOSRestBundle</a> and the <a href="https://github.com/schmittjoh/JMSSerializerBundle" target="_blank">JMSSerializerBundle</a>. <a href="http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/" target="_blank">Here</a> there is a really cool post to see how to build a cool API.</p>
<p>Well, I hope this post help you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/my-favorite-stack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Migrate ElasticSearch data from one server to another one</title>
		<link>http://www.hasheado.com/migrate-elasticsearch-data-from-one-server-to-another-one/</link>
		<comments>http://www.hasheado.com/migrate-elasticsearch-data-from-one-server-to-another-one/#comments</comments>
		<pubDate>Wed, 01 Apr 2015 20:20:01 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[elasticsearch]]></category>
		<category><![CDATA[graylog]]></category>
		<category><![CDATA[graylog2]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[logs]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=239</guid>
		<description><![CDATA[As part of the logging system in a big project I&#8217;m working for a long time ago we are using Graylog to storage the logs coming from the application as well as from devices and services. As you may know Graylog uses ElasticSearch to store the data (logs) and MongoDB to store some metadata and [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>As part of the logging system in a big project I&#8217;m working for a long time ago we are using <a href="https://www.graylog.org/" title="Graylog website" target="_blank">Graylog</a> to storage the logs coming from the application as well as from devices and services.</p>
<p>As you may know <strong><em>Graylog</em></strong> uses <a href="https://www.elastic.co/products/elasticsearch" title="ElasticSeach website" target="_blank">ElasticSearch</a> to store the data (logs) and <a href="https://www.mongodb.org/" title="MongoDB website" target="_blank">MongoDB</a> to store some metadata and configuration.</p>
<p>For these days we are facing the need to grow our stack and make our logging system more scalable, and also, with the release of <a href="https://www.graylog.org/graylog-v1-0-1-has-been-released/" title="Graylog website" target="_blank">Graylog 1.0.1</a> we decided to build a new and more powerful logging system. Said that, and keeping on mind that we have to maintain the already existing logs, we faced the problem to migrate those existing logs (data) into the new <em><strong>ElasticSeach</strong></em> instance.</p>
<p>After some research on existing tools to do that, we found <a href="https://github.com/mallocator/Elasticsearch-Exporter" title="Github website - Elasticsearch-Exporter" target="_blank">Elasticsearch-Exporter</a>.</p>
<p>To install <em><strong>Elasticsearch-Exporter</strong></em>:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666;">user@unix:~$ </span>npm <span style="color: #c20cb9; font-weight: bold;">install</span> nomnom<br />
<span style="color: #666666;">user@unix:~$ </span>npm <span style="color: #c20cb9; font-weight: bold;">install</span> colors<br />
<span style="color: #666666;">user@unix:~$ </span>npm <span style="color: #c20cb9; font-weight: bold;">install</span> elasticsearch-exporter <span style="color: #660033;">--production</span></div></td></tr></tbody></table></div>
<p>Then to copy all indices from server A to server B:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666;">user@unix:~$ </span>node exporter.js <span style="color: #660033;">-a</span> serverA <span style="color: #660033;">-b</span> serverB</div></td></tr></tbody></table></div>
<p>After letting it run for a while, we start seeing messages like:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Processed <span style="color: #000000;">4144396</span> of <span style="color: #000000;">10626656</span> entries <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">39</span><span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></td></tr></tbody></table></div>
<p>And that&#8217;s. After the process finished we have the existing data in the new Server B. Enjoy it! <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/migrate-elasticsearch-data-from-one-server-to-another-one/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hasheado Admin Bundle</title>
		<link>http://www.hasheado.com/hasheado-admin-bundle/</link>
		<comments>http://www.hasheado.com/hasheado-admin-bundle/#respond</comments>
		<pubDate>Tue, 24 Feb 2015 02:23:01 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[admin-generator]]></category>
		<category><![CDATA[AdminBundle]]></category>
		<category><![CDATA[Bundle]]></category>
		<category><![CDATA[generate-admin]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=226</guid>
		<description><![CDATA[This is the bundle I use for admin generator in Symfony application. Basically, it&#8217;s an extension of SonataAdminBundle, but with different UI and FOSUserBundle already integrated for user and authentication management. You can find it in: &#8211; Packagist &#8211; Github You can follow this documentation to install it and use it: Step 1: Download the [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>This is the bundle I use for admin generator in Symfony application. Basically, it&#8217;s an extension of <strong><em>SonataAdminBundle</em></strong>, but with different UI and <strong><em>FOSUserBundle</em></strong> already integrated for user and authentication management.</p>
<p>You can find it in:<br />
&#8211; <a title="Packagist website" href="https://packagist.org/packages/hasheado/admin-bundle" target="_blank">Packagist</a><br />
&#8211; <a title="Github website" href="https://github.com/emiliano-viada-developer/HasheadoAdminBundle" target="_blank">Github</a></p>
<p>You can follow this documentation to install it and use it:</p>
<h4>Step 1: Download the Bundle</h4>
<p>NOTE: This bundle depends in the <a title="Sonata project website" href="http://sonata-project.org/bundles/admin/2-3/doc/index.html" target="_blank">SonataAdminBundle</a>, <a title="Github website" href="https://github.com/FriendsOfSymfony/FOSUserBundle" target="_blank">FOSUserBundle</a> and in the <a title="Sonata project website" href="http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/installation.html" target="_blank">SonataDoctrineORMAdminBundle</a>.</p>
<p>In an already working Symfony installation edit your <em>composer.json</em> file and add this line:</p>
<div class="codecolorer-container yaml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="yaml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: green;">require</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#123;</span><span style="color: green;"><br />
&nbsp; &nbsp; &quot;hasheado/admin-bundle&quot;</span><span style="font-weight: bold; color: brown;">: </span><span style="color: #CF00CF;">&quot;dev-master&quot;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>and then, open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666;">user@unix:~$ </span>composer update</div></td></tr></tbody></table></div>
<p>This command requires you to have Composer installed globally, as explained in the <a title="Composer website" href="https://getcomposer.org/doc/00-intro.md" target="_blank">installation chapter</a> of the Composer documentation.</p>
<h4>Step 2: Enable the bundle</h4>
<p>To enable the bundle we need to add the following line in the <em>app/AppKernel.php</em> file of your project:</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #666666; font-style: italic;">// app/AppKernel.php</span><br />
<br />
<span style="color: #666666; font-style: italic;">// ...</span><br />
<span style="color: #000000; font-weight: bold;">class</span> AppKernel <span style="color: #000000; font-weight: bold;">extends</span> Kernel<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerBundles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$bundles</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add dependencies</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Sonata\CoreBundle\SonataCoreBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Sonata\BlockBundle\SonataBlockBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Knp\Bundle\MenuBundle\KnpMenuBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> FOS\UserBundle\FOSUserBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Sonata\UserBundle\SonataUserBundle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FOSUserBundle'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Sonata\EasyExtendsBundle\SonataEasyExtendsBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Then add SonataAdminBundle</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Sonata\AdminBundle\SonataAdminBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// And HasheadoAdminBundle</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Hasheado\AdminBundle\HasheadoAdminBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<h4>Step 3: Configuration</h4>
<p>If you followed the installation instructions, <em>HasheadoAdminBundle</em> should be installed but inaccessible. You first need to configure it for your models before you can start using it.</p>
<p>Import the <em>HasheadoAdminBundle’s</em> config files:</p>
<div class="codecolorer-container yaml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="yaml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: blue;"># app/config/config.yml</span><span style="color: #007F45;"><br />
imports</span>:<span style="color: green;"><br />
&nbsp; &nbsp; - { resource</span><span style="font-weight: bold; color: brown;">: </span>@HasheadoAdminBundle/Resources/config/config.yml <span class="br0">&#125;</span><br />
<span style="color: #007F45;"><br />
sonata_user</span>:<span style="color: #007F45;"><br />
&nbsp; &nbsp; class</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; user</span><span style="font-weight: bold; color: brown;">: </span>pathToYourBundle\Entity\User<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; group</span><span style="font-weight: bold; color: brown;">: </span>pathToYourBundle\Entity\Group<br />
<span style="color: #007F45;"><br />
&nbsp; &nbsp; fos_user</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; user_class</span><span style="font-weight: bold; color: brown;">: </span>pathToYourBundle\Entity\User<span style="color: #007F45;"><br />
&nbsp; &nbsp; group</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; group_class</span><span style="font-weight: bold; color: brown;">: </span>pathToYourBundle\Entity\Group</div></td></tr></tbody></table></div>
<p>Of course, replace the path to your User and/or Group entities, and do not forget to make your classes extend the <em>Sonata\UserBundle\Entity\BaseUser</em> and the <em>Sonata\UserBundle\Entity\BaseGroup</em> classes.</p>
<p>Then you need to add the below security configuration:</p>
<div class="codecolorer-container yaml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br /></div></td><td><div class="yaml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: blue;"># app/config/security.yml</span><span style="color: #007F45;"><br />
security</span>:<span style="color: #007F45;"><br />
&nbsp; &nbsp; role_hierarchy</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; ROLE_ADMIN</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#91;</span>ROLE_USER, ROLE_SONATA_ADMIN<span class="br0">&#93;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; ROLE_SUPER_ADMIN</span><span style="font-weight: bold; color: brown;">: </span><span class="br0">&#91;</span>ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH<span class="br0">&#93;</span><span style="color: #007F45;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; SONATA</span><span style="font-weight: bold; color: brown;">:<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT <span style="color: blue;"># if you are using acl then this line must be commented</span><br />
<span style="color: #007F45;"><br />
&nbsp; &nbsp; providers</span>:<span style="color: #007F45;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; fos_userbundle</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;id</span><span style="font-weight: bold; color: brown;">: </span>fos_user.user_manager<br />
<span style="color: #007F45;"><br />
&nbsp; &nbsp; encoders</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; FOS\UserBundle\Model\UserInterface</span><span style="font-weight: bold; color: brown;">: </span>sha512<br />
<span style="color: #007F45;"><br />
&nbsp; &nbsp; firewalls</span><span style="font-weight: bold; color: brown;">:<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: blue;"># Disabling the security for the web debug toolbar, the profiler and Assetic.</span><span style="color: #007F45;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; dev</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern</span><span style="font-weight: bold; color: brown;">: </span>^/<span class="br0">&#40;</span>_<span class="br0">&#40;</span>profiler|wdt<span class="br0">&#41;</span>|css|images|js<span class="br0">&#41;</span>/<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; security</span><span style="font-weight: bold; color: brown;">: </span>false<br />
<br />
&nbsp; &nbsp; <span style="color: blue;"># -&gt;; custom firewall for the admin area of the URL</span><span style="color: #007F45;"><br />
&nbsp; &nbsp; admin</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; pattern</span><span style="font-weight: bold; color: brown;">: </span>/admin<span class="br0">&#40;</span>.*<span class="br0">&#41;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; context</span><span style="font-weight: bold; color: brown;">: </span>user<span style="color: #007F45;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; form_login</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; provider</span><span style="font-weight: bold; color: brown;">: </span>fos_userbundle<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; login_path</span><span style="font-weight: bold; color: brown;">: </span>/admin/login<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; use_forward</span><span style="font-weight: bold; color: brown;">: </span>false<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; check_path</span><span style="font-weight: bold; color: brown;">: </span>/admin/login_check<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failure_path</span><span style="font-weight: bold; color: brown;">: </span>null<span style="color: #007F45;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; logout</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path</span><span style="font-weight: bold; color: brown;">: </span>/admin/logout<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target</span><span style="font-weight: bold; color: brown;">: </span>/admin<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anonymous</span><span style="font-weight: bold; color: brown;">: </span>true<br />
<br />
&nbsp; &nbsp; <span style="color: blue;"># -&gt; end custom configuration</span><br />
<span style="color: #007F45;"><br />
&nbsp; &nbsp; acl</span>:<span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; connection</span><span style="font-weight: bold; color: brown;">: </span>default<br />
<span style="color: #007F45;"><br />
&nbsp; &nbsp; access_control</span><span style="font-weight: bold; color: brown;">:<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: blue;"># Admin login page needs to be access without credential</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; - { path</span><span style="font-weight: bold; color: brown;">: </span>^/admin/login$, role<span style="font-weight: bold; color: brown;">: </span>IS_AUTHENTICATED_ANONYMOUSLY <span class="br0">&#125;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; - { path</span><span style="font-weight: bold; color: brown;">: </span>^/admin/logout$, role<span style="font-weight: bold; color: brown;">: </span>IS_AUTHENTICATED_ANONYMOUSLY <span class="br0">&#125;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; - { path</span><span style="font-weight: bold; color: brown;">: </span>^/admin/login_check$, role<span style="font-weight: bold; color: brown;">: </span>IS_AUTHENTICATED_ANONYMOUSLY <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;"># Secured part of the site</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;"># This config requires being logged for the whole site and having the admin role for the admin part.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;"># Change these rules to adapt them to your needs</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; - { path</span><span style="font-weight: bold; color: brown;">: </span>^/admin/, role<span style="font-weight: bold; color: brown;">: </span><span class="br0">&#91;</span>ROLE_ADMIN, ROLE_SONATA_ADMIN<span class="br0">&#93;</span> <span class="br0">&#125;</span><span style="color: green;"><br />
&nbsp; &nbsp; &nbsp; &nbsp; - { path</span><span style="font-weight: bold; color: brown;">: </span>^/.*, role<span style="font-weight: bold; color: brown;">: </span>IS_AUTHENTICATED_ANONYMOUSLY <span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>At this point, the bundle is functional, but not quite ready yet.</p>
<p>You need to update the schema:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666;">user@unix:~$ </span>php app<span style="color: #000000; font-weight: bold;">/</span>console doctrine:schema:update <span style="color: #660033;">--force</span></div></td></tr></tbody></table></div>
<p>and create a new root user:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666;">user@unix:~$ </span>php app<span style="color: #000000; font-weight: bold;">/</span>console fos:user:create <span style="color: #660033;">--super-admin</span></div></td></tr></tbody></table></div>
<p>To be able to access HasheadoAdminBundle’s pages, you need to add its routes to your application’s routing file:</p>
<div class="codecolorer-container yaml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="yaml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: blue;"># app/config/routing.yml</span><span style="color: #007F45;"><br />
admin</span>:<span style="color: green;"><br />
&nbsp; &nbsp; resource</span><span style="font-weight: bold; color: brown;">: </span>'@HasheadoAdminBundle/Resources/config/routing.yml'</div></td></tr></tbody></table></div>
<p>Finally, publish the assets:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666;">user@unix:~$ </span>php app<span style="color: #000000; font-weight: bold;">/</span>console assets:install</div></td></tr></tbody></table></div>
<p>At this point you can already access the (empty) admin dashboard by visiting the url: <em>http://yoursite.local/admin/dashboard</em>. And, start adding your admins following <a title="Sonata project website" href="http://sonata-project.org/bundles/admin/2-3/doc/index.html" target="_blank">SonataAdminBundle</a> documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/hasheado-admin-bundle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP7 is almost here</title>
		<link>http://www.hasheado.com/php7-is-almost-here/</link>
		<comments>http://www.hasheado.com/php7-is-almost-here/#respond</comments>
		<pubDate>Tue, 10 Feb 2015 00:35:39 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP7]]></category>
		<category><![CDATA[sunshinePHP]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=219</guid>
		<description><![CDATA[The Last weekend in Miami, Florida, it took place the SunshinePHP conference where a lot of great members of the PHP community have gave great talks. I did not have the chance to be there, but I followed the talks through the social networks :D! Rasmus Lerdorf (the PHP creator) was talking about PHP7, and [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>The Last weekend in Miami, Florida, it took place the <a href="http://2015.sunshinephp.com/" title="Sunshine PHP website" target="_blank">SunshinePHP conference</a> where a lot of great members of the PHP community have gave great talks. I did not have the chance to be there, but I followed the talks through the social networks :D!</p>
<p><a href="https://twitter.com/@rasmus" title="Twitter account" target="_blank">Rasmus Lerdorf</a> (the PHP creator) was talking about PHP7, and here you can find his slides:</p>
<p><a href="http://talks.php.net/sunshinephp15#" title="PHP talks" target="_blank">http://talks.php.net/sunshinephp15#</a></p>
<p>Some of the most important notes:</p>
<ul>
<li>Engine improvements:
<ul>
<li>100%+ performance gain on most real-world applications.</li>
<li>Lower memory usage.</li>
<li>Native thread local storage.</li>
</ul>
</li>
<li>Removal of many deprecated features (Your PHP4 code will break!).</li>
<li>First Release Candidate scheduled for June 2015.</li>
</ul>
<p>Also, in the slides there are a couple of chart that show you how much the engine is improved, sometimes more than HHVM. And, Rasmus encourages us to test this new PHP7 version and contribute.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/php7-is-almost-here/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resizer: Responsive design testing tool</title>
		<link>http://www.hasheado.com/resizer-responsive-design-testing-tool/</link>
		<comments>http://www.hasheado.com/resizer-responsive-design-testing-tool/#respond</comments>
		<pubDate>Tue, 27 Jan 2015 16:09:39 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[resizer]]></category>
		<category><![CDATA[responsive]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=206</guid>
		<description><![CDATA[Resizer is a great browser-based tool to test responsiveness in our sites created by @MalteWassermann. We just have to bookmark the tool. To use it, in our site we should click on our created bookmarklet and check all kinds of screen resolutions of the page. And it&#8217;s free :D. I hope it&#8217;s helpful for you.]]></description>
				<content:encoded><![CDATA[<p><a title="Resizer website" href="http://lab.maltewassermann.com/viewport-resizer/" target="_blank">Resizer</a> is a great browser-based tool to test responsiveness in our sites created by <a title="Twitter profile" href="https://twitter.com/MalteWassermann" target="_blank">@MalteWassermann</a>. We just have to bookmark the tool. To use it, in our site we should click on our created bookmarklet and check all kinds of screen resolutions of the page.</p>
<p><a href="http://www.hasheado.com/wp-content/uploads/2015/01/resizer-screenshot.png"><img class=" size-large wp-image-214 aligncenter" src="http://www.hasheado.com/wp-content/uploads/2015/01/resizer-screenshot-1024x496.png" alt="resizer-screenshot" width="620" height="300" srcset="http://www.hasheado.com/wp-content/uploads/2015/01/resizer-screenshot-1024x496.png 1024w, http://www.hasheado.com/wp-content/uploads/2015/01/resizer-screenshot-300x145.png 300w, http://www.hasheado.com/wp-content/uploads/2015/01/resizer-screenshot.png 1362w" sizes="(max-width: 620px) 100vw, 620px" /></a></p>
<p>And it&#8217;s free :D. I hope it&#8217;s helpful for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/resizer-responsive-design-testing-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Symfony best practices</title>
		<link>http://www.hasheado.com/symfony-best-practices/</link>
		<comments>http://www.hasheado.com/symfony-best-practices/#respond</comments>
		<pubDate>Wed, 14 Jan 2015 01:54:04 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=154</guid>
		<description><![CDATA[A couple months ago the Symfony creator Fabien Potencier, alongside Ryan Weaver and Javier Eguiluz have created a handbook called &#8220;Symfony Best Practices&#8220;. The reason of this handbook was due to the community has created an unofficial set of recommendations for developing Symfony applications. Unfortunately, a lot of these recommendations are unneeded for web applications. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>A couple months ago the <strong>Symfony</strong> creator <a href="https://connect.sensiolabs.com/profile/fabpot" title="SensioLabs website" target="_blank">Fabien Potencier</a>, alongside <a href="https://connect.sensiolabs.com/profile/weaverryan" title="SensioLabs website" target="_blank">Ryan Weaver</a> and <a href="https://connect.sensiolabs.com/profile/javier.eguiluz" title="SensioLabs website" target="_blank">Javier Eguiluz</a> have created a handbook called &#8220;<a href="http://symfony.com/doc/current/best_practices/index.html" title="Symfony docs website" target="_blank">Symfony Best Practices</a>&#8220;.</p>
<p>The reason of this handbook was due to the community has created an unofficial set of recommendations for developing <strong>Symfony</strong> applications. Unfortunately, a lot of these recommendations are unneeded for web applications.</p>
<p>Here, a summary of these best practices:</p>
<h3>Projects creation</h3>
<blockquote class="small"><p>Use the Symfony installer.</p></blockquote>
<p>You can read about it in an <a href="http://www.hasheado.com/the-symfony-installer/" title="The Symfony Installer" target="_blank">earlier post</a>.</p>
<p>About bundles:</p>
<blockquote class="small"><p>Create only one bundle called <em>AppBundle</em> for your application logic</p></blockquote>
<h3>Configuration</h3>
<blockquote class="small"><p>Define the infrastructure-related configuration options in the <em>app/config/parameters.yml</em> file.</p></blockquote>
<blockquote class="small"><p>Define all your application&#8217;s parameters in the <em>app/config/parameters.yml.dist</em> file.</p></blockquote>
<blockquote class="small"><p>Define the application behavior related configuration options in the <em>app/config/config.yml</em> file.</p></blockquote>
<h3>Organizing our Business Logic</h3>
<p>Services: Name and Format</p>
<blockquote class="small"><p>The name of your application&#8217;s services should be as short as possible, but unique enough that you can search your project for the service if you ever need to.</p></blockquote>
<blockquote class="small"><p>Use the YAML format to define your own services.</p></blockquote>
<blockquote class="small"><p>Don&#8217;t define parameters for the classes of your services.</p></blockquote>
<p>Using a Persistence Layer:</p>
<blockquote class="small"><p>Use annotations to define the mapping information of the Doctrine entities.</p></blockquote>
<h3>Controllers</h3>
<blockquote class="small"><p>Make your controller extend the <em>FrameworkBundle</em> base Controller and use annotations to configure routing, caching and security whenever possible.</p></blockquote>
<p>Routing configuration:</p>
<blockquote class="small"><p>Don&#8217;t use the <em><strong>@Template()</strong></em> annotation to configure the template used by the controller.</p></blockquote>
<blockquote class="small"><p>Use the <em>ParamConverter</em> trick to automatically query for Doctrine entities when it&#8217;s simple and convenient.</p></blockquote>
<h3>Templates</h3>
<blockquote class="small"><p>Use <em>Twig</em> templating format for your templates.</p></blockquote>
<blockquote class="small"><p>Store all your application&#8217;s templates in <em>app/Resources/views/</em> directory.</p></blockquote>
<p>Twig extensions:</p>
<blockquote class="small"><p>Define your Twig extensions in the <em>AppBundle/Twig/</em> directory and configure them using the <em>app/config/services.yml</em> file.</p></blockquote>
<h3>Forms</h3>
<blockquote class="small"><p>Define your forms as PHP classes.</p></blockquote>
<blockquote class="small"><p>Add buttons in the templates, not in the form classes or the controllers.</p></blockquote>
<h3>Internationalization</h3>
<blockquote class="small"><p>Use the XLIFF format for your translation files.</p></blockquote>
<blockquote class="small"><p>Store the translation files in the <em>app/Resources/translations/</em> directory.</p></blockquote>
<blockquote class="small"><p>Always use keys for translations instead of content strings.</p></blockquote>
<h3>Security</h3>
<blockquote class="small"><p>Unless you have two legitimately different authentication systems and users (e.g. form login for the main site and a token system for your API only), we recommend having only one firewall entry with the anonymous key enabled.</p></blockquote>
<blockquote class="small"><p>Use the bcrypt encoder for encoding your users&#8217; passwords.</p></blockquote>
<h3>Web Assets</h3>
<blockquote class="small"><p>Store your assets in the <em>web/</em> directory.</p></blockquote>
<blockquote class="small"><p>Use <em>Assetic</em> to compile, combine and minimize web assets, unless you&#8217;re comfortable with frontend tools like <em>GruntJS</em>.</p></blockquote>
<h3>Tests</h3>
<blockquote class="small"><p>Define a functional test that at least checks if your application pages are successfully loading.</p></blockquote>
<blockquote class="small"><p>Hardcode the URLs used in the functional tests instead of using the URL generator.</p></blockquote>
<p>Well, a great handbook to follow up, but of course, this are &#8220;best practices&#8221; and they are not a must.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/symfony-best-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP and Behavior Driven Desing</title>
		<link>http://www.hasheado.com/php-and-behavior-driven-desing/</link>
		<comments>http://www.hasheado.com/php-and-behavior-driven-desing/#respond</comments>
		<pubDate>Tue, 16 Dec 2014 01:47:35 +0000</pubDate>
		<dc:creator><![CDATA[emiviada]]></dc:creator>
				<category><![CDATA[Design patterns]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[Behat]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PhpSpec]]></category>

		<guid isPermaLink="false">http://www.hasheado.com/?p=134</guid>
		<description><![CDATA[During this 2014 I was hearing and reading a lot about Behavior Driven Desing (BDD), which is a software development process based on Test Driven Design (TDD) combining ideas from Domain Driven Design (DDD) and Object Oriented Programming (OOP). For a full explanation about BDD click here. In the tutsplus.com website we can find an [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>During this 2014 I was hearing and reading a lot about <em><strong>Behavior Driven Desing (BDD)</strong></em>, which is a software development process based on Test Driven Design (<a href="http://en.wikipedia.org/wiki/Test-driven_development" title="Wikipedia" target="_blank">TDD</a>) combining ideas from Domain Driven Design (DDD) and Object Oriented Programming (OOP). For a full explanation about BDD <a href="http://en.wikipedia.org/wiki/Behavior-driven_development" title="Wikipedia" target="_blank">click here</a>.</p>
<p>In the <a href="http://tutsplus.com" title="Tutsplus website" target="_blank">tutsplus.com</a> website we can find an in progress tutorial which explains how to use <strong>BDD</strong> in <a href="http://php.net" title="PHP website" target="_blank">PHP</a> using the <a href="http://laravel.com/" title="Laravel website" target="_blank">Laravel</a> PHP framework, <a href="http://docs.behat.org/en/v2.5/" title="Behat website" target="_blank">Behat</a> and <a href="http://www.phpspec.net/" title="Phpspec website" target="_blank">PhpSpec</a>. The tutotial is called &#8220;<em>Laravel, BDD and You</em>&#8221; and in this post I will be linking to every post in this serie.</p>
<ul>
<li><a href="http://code.tutsplus.com/tutorials/laravel-bdd-and-you-lets-get-started--cms-22155" title="Tutsplus website" target="_blank">Laravel, BDD and You: Let&#8217;s get started</a></li>
<li><a href="http://code.tutsplus.com/tutorials/laravel-bdd-and-you-the-first-feature--cms-22486" title="Tutsplus website" target="_blank">Laravel, BDD and You: The first feature</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.hasheado.com/php-and-behavior-driven-desing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>