<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Herberth Amaral &#187; PHP</title> <atom:link href="http://herberthamaral.com/category/php/feed/" rel="self" type="application/rss+xml" /><link>http://herberthamaral.com</link> <description>Software development adventures</description> <lastBuildDate>Thu, 26 Aug 2010 13:59:42 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>[Dica] Valide e-mail, IP e URLs no PHP sem regex</title><link>http://herberthamaral.com/2010/08/dica-valide-e-mail-ip-e-urls-no-php-sem-regex/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=dica-valide-e-mail-ip-e-urls-no-php-sem-regex</link> <comments>http://herberthamaral.com/2010/08/dica-valide-e-mail-ip-e-urls-no-php-sem-regex/#comments</comments> <pubDate>Fri, 13 Aug 2010 16:21:05 +0000</pubDate> <dc:creator>Herberth Amaral</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[web]]></category><guid isPermaLink="false">http://herberthamaral.com/?p=693</guid> <description><![CDATA[Eu vejo muita gente utilizando regex (expressão regular) para validar vários tipos de dados, incluindo emails, tipos (boolean, string e int), sanitizar dados (remover caracteres inválidos, por exemplo) no PHP. Este tipo de coisa é um saco para quem não saca muito de regex, mas é possível fazer todas estas validações sem o uso de [...]]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fherberthamaral.com%2F2010%2F08%2Fdica-valide-e-mail-ip-e-urls-no-php-sem-regex%2F"> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fherberthamaral.com%2F2010%2F08%2Fdica-valide-e-mail-ip-e-urls-no-php-sem-regex%2F&amp;source=HerberthAmaral&amp;style=normal" height="61" width="50" /> </a></div><p>Eu vejo muita gente utilizando regex (expressão regular) para validar vários tipos de dados, incluindo emails, tipos (boolean, string e int), sanitizar dados (remover caracteres inválidos, por exemplo) no PHP. Este tipo de coisa é um saco para quem não saca muito de regex, mas é possível fazer todas estas validações sem o uso de regex utilizando as funções da família <strong><a href="http://fr.php.net/manual/en/book.filter.php">filter</a></strong>:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bob@example.com'</span><span style="color: #339933;">,</span> FILTER_VALIDATE_EMAIL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yay!'</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span></div></div><p>Há <a href="http://fr.php.net/manual/en/filter.constants.php">vários outros filtros</a> que você pode experimentar com esta função super útil e nunca mais precisar lembrar daquela regex chata de email <img src='http://herberthamaral.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p><p>Happy coding!</p><p><strong>[Update]</strong></p><p>Esqueci de mencionar: Estas funções estão disponíveis a partir do PHP 5.2.0. Valeu, <a href="http://www.almirmendes.net">Almir Mendes</a>!</p><p>Sem posts relacionados.</p>]]></content:encoded> <wfw:commentRss>http://herberthamaral.com/2010/08/dica-valide-e-mail-ip-e-urls-no-php-sem-regex/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Quando uma deficiência pode se tornar uma feature numa linguagem de programação</title><link>http://herberthamaral.com/2010/07/quando-uma-deficiencia-pode-se-tornar-uma-feature-numa-linguagem-de-programacao/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=quando-uma-deficiencia-pode-se-tornar-uma-feature-numa-linguagem-de-programacao</link> <comments>http://herberthamaral.com/2010/07/quando-uma-deficiencia-pode-se-tornar-uma-feature-numa-linguagem-de-programacao/#comments</comments> <pubDate>Thu, 29 Jul 2010 13:34:55 +0000</pubDate> <dc:creator>Herberth Amaral</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[agile]]></category> <category><![CDATA[ruby]]></category><guid isPermaLink="false">http://herberthamaral.com/?p=665</guid> <description><![CDATA[Estava lendo um antigo post do Obie Fernandez onde ele lista várias razões do porquê Java é uma porcaria (em tom de brincadeira, claro =). Numa das razões citadas por ele, há o seguinte trecho: 8 ) Java has good debugging support Java has great and powerful debugging (which is very necessary since most Java [...]]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fherberthamaral.com%2F2010%2F07%2Fquando-uma-deficiencia-pode-se-tornar-uma-feature-numa-linguagem-de-programacao%2F"> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fherberthamaral.com%2F2010%2F07%2Fquando-uma-deficiencia-pode-se-tornar-uma-feature-numa-linguagem-de-programacao%2F&amp;source=HerberthAmaral&amp;style=normal" height="61" width="50" /> </a></div><p>Estava lendo um <a href="http://www.jroller.com/obie/entry/top_10_reasons_why_java">antigo post</a> do <a href="http://obiefernandez.com/">Obie Fernandez </a>onde ele lista várias razões do porquê Java é uma porcaria (em tom de brincadeira, claro =). Numa das razões citadas por ele, há o seguinte trecho:</p><blockquote><p> 8 ) Java has good debugging support</p><p>Java has great and powerful debugging (which is very necessary since most Java code sucks so much ass). Ruby has ridiculously pitiful debugging support, which means our code actually needs to be well-tested and readable.</p></blockquote><p>Traduzindo:</p><blockquote><p> 8 ) Java possui um bom suporte a debugging</p><p>Java possui um excelente e poderoso debugging (o que é necessário, já que boa parte de códigos em Java são muito ruins). Ruby tem um suporte a debugging lamentavelmente ridículo, o que significa que nosso código realmente precisa ser bem testado e legível.</p></blockquote><p>Vendo por este lado, Ruby é uma excelente linguagem para quem sabe fazer direito e uma droga pra quem está acostumado com IDEs com Intellisense e checagem de erros pelo compilador.</p><p>Outras linguagens possuem problemas que são resolvidos facilmente com o uso de testes unitários. Um problema clássico que acontece com o PHP, o <a href="http://en.wikipedia.org/wiki/White_screen_of_death#PHP_web_applications">White Screen of Death</a> (um erro que acontece com o script que nenhuma informação ou mensagem de erro é apresentada), pode ser resolvido com o uso de testes.</p><p>Alguns &#8220;problemas&#8221; advindos com linguagens dinâmicas é com a mudança de tipo de uma determinada variável, por exemplo:</p><div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">class</span> Op:<br /> &nbsp; &nbsp; x = <span style="color: #ff4500;">1</span> &nbsp; &nbsp; <br /> &nbsp; &nbsp; y = <span style="color: #ff4500;">3</span><br /> &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> soma<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">x</span> + <span style="color: #008000;">self</span>.<span style="color: black;">y</span><br /> op = Op<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br /> op.<span style="color: black;">y</span> = <span style="color: #483d8b;">&quot;aha!&quot;</span><br /> op.<span style="color: black;">soma</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#Ooopss... TypeError</span></div></div><p>Este é um tipo de problema que vem com a checagem de tipo em tempo de execução, ao invés de ser feito em tempo de compilação. Muitas pessoas que não possuem a mesma visão do Obie, encaram isto como um problema.</p><h3> Opinionated Software</h3><p>Um trecho muito conhecido do <a href="http://gettingreal.37signals.com/">Getting Real</a> é o trecho que fala sobre <a href="http://gettingreal.37signals.com/ch04_Make_Opinionated_Software.php">opinionated software</a>:</p><blockquote><p> <strong>Your app should take sides</strong></p><p>Some people argue software should be agnostic. They say it&#8217;s arrogant for developers to limit features or ignore feature requests. They say software should always be as flexible as possible.</p><p>We think that&#8217;s bullshit. The best software has a vision. The best software takes sides. When someone uses software, they&#8217;re not just looking for features, they&#8217;re looking for an approach. They&#8217;re looking for a vision. Decide what your vision is and run with it.</p></blockquote><p>Traduzindo:</p><blockquote><p> <strong>Seu aplicativo deve tomar partido</strong></p><p>Algumas pessoas defendem que o software deve ser agnóstico. Dizem que é arrogante da parte dos desenvolvedores limitar a funcionalidade ou ignorar pedidos de novos recursos. Dizem que o software deve ser sempre o mais flexível possível.</p><p>Para nós isso é papo-furado. O melhor software traz consigo uma visão. O melhor software toma partido. Quando alguém usa um software, não está procurando apenas recursos, está procurando uma abordagem. Está procurando uma visão. Decida qual é sua visão e atenha-se a ela.</p></blockquote><p>Esta mesma idéia permeia linguagens de programação. Você não achará um linguagem que sirva bem para todos os tipos de casos. Python, por exemplo, não é uma boa linguagem se você quiser usar threads pesadamente. Erlang, apesar de ser uma excelente escolha para programação distribuída e paralela, enfrenta um certo preconceito por ser funcional e não ter suporte à orientação a objetos. C#.NET é uma boa linguagem para criação de aplicativos comerciais, mas não é totalmente portável para todas as plataformas (o Mono mal suporta o .NET 3.5 e o framework já está na versão 4).</p><p><strong>Conclusão</strong>: antes de reclamar de uma linguagem de programação, saiba que toda linguagem tem um <em>approach</em>, um foco e veja se esta &#8220;falha&#8221; pode ser algo útil&#8230; muitas vezes o é <img src='http://herberthamaral.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p><p>Sem posts relacionados.</p>]]></content:encoded> <wfw:commentRss>http://herberthamaral.com/2010/07/quando-uma-deficiencia-pode-se-tornar-uma-feature-numa-linguagem-de-programacao/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Usando o SabreAMF com o CakePHP</title><link>http://herberthamaral.com/2009/12/usando-o-sabreamf-com-o-cakephp/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=usando-o-sabreamf-com-o-cakephp</link> <comments>http://herberthamaral.com/2009/12/usando-o-sabreamf-com-o-cakephp/#comments</comments> <pubDate>Tue, 15 Dec 2009 21:25:33 +0000</pubDate> <dc:creator>Herberth Amaral</dc:creator> <category><![CDATA[Flex & Actionscript]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[cakephp]]></category> <category><![CDATA[flex]]></category> <category><![CDATA[hello world]]></category> <category><![CDATA[sabreamf]]></category><guid isPermaLink="false">http://herberthamaral.com/?p=66</guid> <description><![CDATA[O AMF é um formato aberto de comunicação do Flash. É o formato preferido para tais aplicações por possuir diversas vantagens, sendo que a principal delas é a velocidade, já que o AMF não precisa ser deserializado como o XML e o JSON. Por ser um formato aberto, ele pode ser implementado em qualquer linguagem. [...]]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F12%2Fusando-o-sabreamf-com-o-cakephp%2F"> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F12%2Fusando-o-sabreamf-com-o-cakephp%2F&amp;source=HerberthAmaral&amp;style=normal" height="61" width="50" /> </a></div><p>O <a href="http://en.wikipedia.org/wiki/Action_Message_Format" target="_blank">AMF</a> é um formato aberto de comunicação do Flash. É o formato preferido para tais aplicações por possuir diversas vantagens, sendo que a principal delas é a velocidade, já que o AMF não precisa ser deserializado como o XML e o JSON.</p><p>Por ser um formato aberto, ele pode ser implementado em qualquer linguagem. Há várias implementações para o PHP, sendo que as mais notáveis são o AMFPHP, Zend_AMF e o SabreAMF.</p><p>Neste tutorial, eu mostrarei como desenvolver aplicações em PHP com o framework CakePHP que se comunicam com o Flash (usando a plataforma Flex 3) através da biblioteca SabreAMF.</p><h4>1- Baixando e instalando os componentes</h4><p>1 &#8211; Baixe os CakePHP 1.2.5 no <a href="http://www.cakephp.org" target="_blank">site do CakePHP</a> e o SabreAMF no <a href="http://code.google.com/p/sabreamf/downloads/list" target="_blank">Google Code</a>.</p><p>2- <a href="http://book.cakephp.org/pt/view/308/Installing-CakePHP" target="_blank">Instale o CakePHP</a> de forma que ele fique disponível em http://localhost/cakeflex.</p><p>3 &#8211; Descompacte o SabreAmf em app/vendors/SabreAMF</p><h4>2 &#8211; Colocando os dois para trabalhar</h4><p>Para fazer com que o CakePHP trabalhe de forma transparente com o SabreAMF, é necessário<a href="http://book.cakephp.org/view/64/Creating-Components"> criar um componente para o CakePHP</a>.</p><p>Para tal, vamos criar o nosso componente: (salve-o em app/controllers/components/sabre_amf.php)</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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 /> <br /> <a href="http://www.php.net/set_include_path"><span style="color: #990000;">set_include_path</span></a><span style="color: #009900;">&#40;</span>APP<span style="color: #339933;">.</span><span style="color: #0000ff;">'vendors'</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <a href="http://www.php.net/get_include_path"><span style="color: #990000;">get_include_path</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 /> App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Vendor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'SabreAMF_CallbackServer'</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;">'file'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'SabreAMF/CallbackServer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> <span style="color: #000000; font-weight: bold;">class</span> SabreAmfComponent <span style="color: #000000; font-weight: bold;">extends</span> Object <span style="color: #009900;">&#123;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$controller</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'gateway'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$_cakeController</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$_cakeController</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">autoRender</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$server</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SabreAMF_CallbackServer<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;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">onInvokeService</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: #000088;">$this</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'amfCallBack'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</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; <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; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <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;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amfExclude</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">!</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;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">,</span><span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amfExclude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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: #009900;">&#40;</span><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: #000000; font-weight: bold;">function</span> amfCallBack<span style="color: #009900;">&#40;</span><span style="color: #000088;">$service</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$_cakeController</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</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;">$_cakeController</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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/strpos"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">0</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/method_exists"><span style="color: #990000;">method_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_cakeController</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> &nbsp;and<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">!</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;">$_cakeController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">,</span><span style="color: #000088;">$_cakeController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amfExclude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/call_user_func_array"><span style="color: #990000;">call_user_func_array</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> <span style="color: #000088;">$_cakeController</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</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; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Action nao encontrada.&quot;</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><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Nome de metodo invalido.&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Controller nao encontrado.&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> <span style="color: #009900;">&#125;</span><br /> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div><p>O que nós fizemos acima foi redirecionar todas as chamadas à action &#8216;gateway&#8217; do controller para nosso gateway do SabreAMF.</p><p>Agora, um exemplo de como utilizar isso em um controller:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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/controllers/teste_amf_controller.php</span><br /> <span style="color: #000000; font-weight: bold;">class</span> TesteAmfController <span style="color: #000000; font-weight: bold;">extends</span> AppController<br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</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;">'SabreAmf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nome</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Olá, &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$nome</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;! agora são: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; horas&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> &nbsp;<span style="color: #000088;">$ret</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> <span style="color: #009900;">&#125;</span><br /> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div><p>Nosso server side não estaria completo sem o nosso model. No meu caso, como é só uma demonstração, eu não quero criar uma tabela no banco de dados. Portanto, eu crio o meu model dessa forma:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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/models/teste_amf.php</span><br /> <span style="color: #000000; font-weight: bold;">class</span> TesteAmf <span style="color: #000000; font-weight: bold;">extends</span> AppModel<br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useTable</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span><br /> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div><p>Para demonstrar nosso código funcionado, nada melhor que uma aplicação Flex. Segue abaixo, um exemplo de como a configuração criada acima pode ser usada no Flex:</p><pre>
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"&gt;

	&lt;mx:RemoteObject
		endpoint="http://localhost/cakeflex/teste_amf/gateway"
		id="rmoTeste"
		source=""
		destination="minhaApp"
		result="rmoTesteResult(event)"
		fault="rmoTesteFail(event)"
		showBusyCursor="true"
		/&gt;

        &lt;mx:Button label="Hello!" click="rmoTeste.hello('Herberth')" /&gt;
        &lt;mx:Script&gt;
		&lt; ![CDATA[
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.controls.Alert;

			public function rmoTesteResult(e:ResultEvent):void
			{
				Alert.show(e.result.toString());
			}

			public function rmoTesteFail(e:FaultEvent):void
			{
				Alert.show("Fail!");
			}
		]]&gt;
	&lt;/mx:Script&gt;
&lt;/mx:Application&gt;
</pre><h4>4 &#8211; Conclusão</h4><p>O resultado final deve ficar parecido com isso:</p><p><img class="alignnone size-full wp-image-81" title="hello_flex" src="http://herberthamaral.com/wp-content/uploads/2009/12/hello_flex.png" alt="hello_flex" width="525" height="272" /></p><p>É possível retornar tipos mais complexos para serem usados em tipos mais complexos (tipo AdvancedDataGrid), mas irei abordar tal aplicação mais futuramente.</p><h4>5 &#8211; Referências</h4><ul><li><a href="http://blog.non-style.com/2008/11/cakephp-airsabreamfcakephp.html">http://blog.non-style.com/2008/11/cakephp-airsabreamfcakephp.html</a></li></ul><p>É isso aí pessoal. Até a próxima!</p><p>Sem posts relacionados.</p>]]></content:encoded> <wfw:commentRss>http://herberthamaral.com/2009/12/usando-o-sabreamf-com-o-cakephp/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>