<?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>CaChi &#187; service</title>
	<atom:link href="http://cachi.temiga.org/tag/service/feed/" rel="self" type="application/rss+xml" />
	<link>http://cachi.temiga.org</link>
	<description>[ TEMIGA ]</description>
	<lastBuildDate>Mon, 19 Jul 2010 14:10:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>KumbiaPHP + Ajax + jQuery</title>
		<link>http://cachi.temiga.org/2010/06/10/kumbiaphp-ajax-jquery/</link>
		<comments>http://cachi.temiga.org/2010/06/10/kumbiaphp-ajax-jquery/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 16:04:07 +0000</pubDate>
		<dc:creator>CaChi</dc:creator>
				<category><![CDATA[KumbiaPHP]]></category>
		<category><![CDATA[Programación]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Buenas Prácticas]]></category>
		<category><![CDATA[comunidad]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[KumbiaPHP Tips]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php framework]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[ShortUrl]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://cachi.temiga.org/?p=641</guid>
		<description><![CDATA[Actualizado 11 de Junio 2010 En este tutorial explcaré el comportamiento de KumbiaPHP cuando hacemos aplicaciones ricas en JavaScript, me basare en utilizar jQuery como framework de JavaScript y no daré una explicación muy amplia de que son peticiones asíncronas ni como funcionan estas esto porque hay suficiente información en la red para quienes deseen]]></description>
			<content:encoded><![CDATA[<blockquote><p>Actualizado 11 de Junio 2010</p></blockquote>
<p>En este tutorial explcaré el comportamiento de <a href="http://www.kumbiaphp.com" title="KumbiaPHP Framework">KumbiaPHP</a> cuando hacemos aplicaciones ricas en JavaScript, me basare en utilizar jQuery como framework de JavaScript  y no daré una explicación muy amplia de que son peticiones asíncronas ni como funcionan estas esto porque hay suficiente información en la red para quienes deseen indagar un poco mas, simplemente nos dedicaremos hacer peticiones asíncronas dentro de <a href="http://www.kumbiaphp.com" title="KumbiaPHP Framework">KumbiaPHP</a>.</p>
<p>Como todo ejemplo lo ideal es basarlo en hechos reales y cotidianos en este sentido haremos un cliente que acorte URL utilizando el <a href="http://www.siguemilink.com/api">API</a> del servicio <a href="http://www.siguemilink.com/" title="SigueMiLink » Acorta tus enlaces!">SigueMiLink</a> el cual fue desarrollado con KumbiaPHP :).</p>
<h3>Requisitos</h3>
<p>Antes de continuar hay que verificar lo que necesitamos para llevar acabo el tutorial.</p>
<ul>
<li>Tener funcionando KumbiaPHP <a href="http://www.kumbiaphp.com/blog/manuales-y-descargas/">Beta1</a> (<a href="http://wiki.kumbiaphp.com/Instalar_Kumbia">Instalar KumbiaPHP</a>)</li>
<li><a href="http://code.jquery.com/jquery-1.4.2.min.js">Descargar jQuery</a> y colocarlo en el directorio <strong>app/public/javascript/jquery/jquery-1.4.2.min.js</strong>.</li>
<li>Por lo menos haber hecho el ejemplo <a href="http://wiki.kumbiaphp.com/Hola_Mundo_KumbiaPHP_Framework">Hola Mundo de KumbiaPHP</a> para entender este tutorial.</li>
<li>Leer un poco el <a href="http://www.siguemilink.com/api">API</a> del servicio <a href="http://www.siguemilink.com/" title="SigueMiLink » Acorta tus enlaces!">SigueMiLink</a></li>
<li>Ver funcionamiento básico de <a href="http://wiki.kumbiaphp.com/AJAX">KumbiaPHP con AJAX</a></li>
</ul>
<h3>Empezamos</h3>
<p>Lo primero es crear el controlador para efecto de este tutorial lo llame <strong>api</strong> mirar las <a href="http://wiki.kumbiaphp.com/Preguntas_Frecuentes#.C2.BFCu.C3.A1l_es_la_convenci.C3.B3n_para_nombrar_los_archivos_y_clases_de_modelos_y_controladores.3F">convenciones de nombres de controladores</a> si se te ocurre otro nombre mejor usalo, pero piensa que a lo largo del tutorial haré mención al <strong>controller api</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Controller para utilizar el API de SigueMiLink
 * @author Deivinson Tejeda &lt;deivinsontejeda@kumbiaphp.com&gt;
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> ApiController <span style="color: #000000; font-weight: bold;">extends</span> ApplicationController
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * URL del API de SigueMiLink
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_urlAPI</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.siguemilink.com/api/%s/?destination=%s'</span><span style="color: #339933;">;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Callback Before que verifica si la petición es Asíncrona...
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> before_filter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_response</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Form que se comunica con el API de SigueMiLink
     * para acortar el enlace
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> acortar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">has_post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//Reemplazo de valores en la URL del API.</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_urlAPI <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_urlAPI<span style="color: #339933;">,</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_urlAPI<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'response'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ahora se crea la vista <strong>app/views/api/acortar.phtml</strong> que tiene un formulario que envia la URL al controller que será enviada al API de SigueMiLink.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div style=&quot;margin: auto&quot;&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> form_tag<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'api/acortar/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'form-remote'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;label&gt;Acorte tu enlace&lt;/label&gt;
        <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> text_field_tag<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> submit_tag<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Enviar'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> end_form_tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;br /&gt;
    &lt;div id=&quot;update&quot; style=&quot;margin: auto;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> javascript_include_tag<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery/jquery-1.4.2.min'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;script type=&quot;text/javascript&quot;&gt;
    &lt;!--//--&gt;&lt;![CDATA[//&gt;&lt;!--
        $(function() {
            $(&quot;.form-remote&quot;).live('submit', function(event){
                event.preventDefault();
			    self = $(this);
			    var button = $('[type=submit]', self);
			    button.attr('disabled', 'disabled');
			    var url = self.attr('action');
			    //var div = self.attr('data-div');
			    $.post(url, self.serialize(), function(data, status){
				    var capa = $('#update');
				    capa.html(data);
				    capa.hide();
				    capa.show('slow');
				    button.attr('disabled', null);
			    });
            });
        });
    //--&gt;&lt;!]]&gt;
&lt;/script&gt;</pre></div></div>

<p>Creamos la vista que tiene la respuesta enviado desde el servicio que acorta la URL <strong>app/views/api/response.phtml</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">code</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;a href=&quot;http://smlk.es/<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$response1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #000000; font-weight: bold;">?&gt;</span>/&quot;&gt;http://smlk.es/<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$response1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #000000; font-weight: bold;">?&gt;</span>/&lt;/a&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;strong&gt;No se pudo acortar el enlace&lt;/strong&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Lo genial de este ejemplo es que funciona con o sin javascript para promover la <a href="http://cachi.temiga.org/2009/10/27/accesibilidad-web/">accesibilidad web</a>. Ahora explico un poco lo que hace el <strong>controller api</strong>, el método <strong>before_filter()</strong> es un filtro que se ejecuta antes de la acción en este caso verificamos si la petición es asíncrona y de serlo damos respuesta asíncrona, útil para centralizar y si la aplicación es muy rica en javascript no se tendra que tener esta validación en cada action del controller ;).</p>
<p>La action <strong>acortar()</strong> muestra el form y envia la petición (cuando se hace el submit) al servicio <a href="http://www.siguemilink.com/" title="SigueMiLink » Acorta tus enlaces!">SigueMiLink</a> para acortar la URL, con el método <a href="http://wiki.kumbiaphp.com/KumbiaPHP_Framework_Versi%C3%B3n_1.0_Spirit#Utilizando_el_m.C3.A9todo_render.28.24view.2C_.5B.24template.5D.29">render()</a> cambiamos de vista a <strong>renpose</strong> y como la petición se ha hecho asíncrona ya saben lo demas :).</p>
<p>En la vista <strong>acortar.phtml</strong> tenemos el formulario y con un JavaScript para enviar la petición asíncrona, en la <a href="http://api.jquery.com/">documentación de jQuery</a> se explican los métodos usados en este ejemplo en los cuales no me enfocare. </p>
<p>Es importante indicar el elemento del <a href="http://en.wikipedia.org/wiki/Document_Object_Model">DOM</a> que se actualizará luego de la petición, en este caso es un div con un identificador (id) <strong>update</strong>.</p>
<p>Es de notar que en la vista <strong>response</strong> no se analizan todos los códigos de error que devuelve el API de SigueMiLink, como tarea pueden hacer esto y dar respuesta al usuario de acuerdo al error retornado del servicio.</p>
<h3>Importante</h3>
<ul>
<li>Si no indicas que la respuesta será asíncrona <a href="http://www.kumbiaphp.com">KumbiaPHP</a> cargará todo el template y por ende habran elementos de la vista que veras repetidos, así que no olvides el <strong>$this->set_response(&#8216;view&#8217;)</strong>.</li>
<li>Saber que elemento del DOM se va actualizar</li>
</ul>
<h3>A probar lo hecho</h3>
<p>Así es, nos queda probar lo que se hizo y mirar que todo ande como lo planeado, es decir un formulario con un campo de texto donde se coloca la URL que deseamos acortar al dar click en el submit si todo estuvo bien nos retorna una URL acortada para compartirla en nuestras redes sociales&#8230;</p>
<p>Asumiendo que tienes una instalación local deberías ver el ejemplo pinchando una URL como esta http://localhost/kumbiaphp/api/acortar/ es importante entender las <a href="http://wiki.kumbiaphp.com/Hola_Mundo_KumbiaPHP_Framework#KumbiaPHP_URLs" title="URL de KumbiaPHP">URL de KumbiaPHP</a></p>
<p>En la <a href="http://wiki.kumbiaphp.com/">Wiki de KumbiaPHP</a> hay un ejemplo mas simple de como <a href="http://wiki.kumbiaphp.com/Integrar_KumbiaPHP_Spirit_con_jQuery">integrar jQuery en KumbiaPHP</a> y puede quedar mucho mejor este tutorial por ejemplo tener un modelo que se consuma todo el web service del API de SigueMiLink, pero ya esto es tarea de lo mas curiosos les dejo un post que escribí sobre los <a href="http://cachi.temiga.org/2009/12/29/que-es-un-modelo-mvc/">modelo dentro del MVC</a></p>
<p>¿dudas? ¿sugerencias? deja tu comentario y ayuda a mejorar este ejemplo&#8230;</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcachi.temiga.org%2F2010%2F06%2F10%2Fkumbiaphp-ajax-jquery%2F&amp;linkname=KumbiaPHP%20%2B%20Ajax%20%2B%20jQuery"><img src="http://cachi.temiga.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://cachi.temiga.org/2010/06/10/kumbiaphp-ajax-jquery/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Servicios libres para URL&#8217;s Cortas (short url)</title>
		<link>http://cachi.temiga.org/2009/08/15/servicios_libres_para_urls_cortas/</link>
		<comments>http://cachi.temiga.org/2009/08/15/servicios_libres_para_urls_cortas/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 01:34:54 +0000</pubDate>
		<dc:creator>CaChi</dc:creator>
				<category><![CDATA[Planeta Linux]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[ShortUrl]]></category>

		<guid isPermaLink="false">http://cachi.temiga.org/?p=334</guid>
		<description><![CDATA[Algo que ha causado algo de algarabía ha sido el tema de lasUrl&#8217;s cortas, en algunos casos es útil, pero en otros como para el tema del SEO puede que no lo sea, para acortar las Url&#8217;s existen una gama bastante grande de servicios para lograr este fin, solo dejo una pequeña lista. tinyurl.com eb.cx]]></description>
			<content:encoded><![CDATA[<p>Algo que ha causado algo de algarabía ha sido el tema de las<a title="Url's cortas" href="http://en.wikipedia.org/wiki/URL_shortening">Url&#8217;s cortas</a>, en algunos casos es útil, pero en otros como para el tema del SEO puede que no lo sea, para acortar las Url&#8217;s existen una gama bastante grande de servicios para lograr este fin, solo dejo una pequeña lista.<a style="padding: 10px" href="http://tinyurl.com/mdg8dr" target="_blank"><img class="alignleft" title="Short Url" src="http://tinyurl.com/mdg8dr" alt="" width="283" height="206" /></a></p>
<ol>
<li><a href="http://tinyurl.com/">tinyurl.com</a></li>
<li><a href="http://eb.cx/">eb.cx</a></li>
<li><a href="http://elfurl.com/">elfurl.com</a></li>
<li><a href="http://www.shorl.com/">shorl.com</a></li>
<li><a href="http://url.fm/">url.fm</a></li>
<li><a href="http://shurl.org/">shurl.org</a></li>
<li><a href="http://qurl.net/">qurl.net</a></li>
<li><a href="http://notlong.com/">notlong.com</a></li>
<li><a href="http://shortlinks.co.uk/">shortlinks.co.uk</a></li>
<li><a href="http://www.url123.com/">url123.com</a></li>
</ol>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcachi.temiga.org%2F2009%2F08%2F15%2Fservicios_libres_para_urls_cortas%2F&amp;linkname=Servicios%20libres%20para%20URL%26%238217%3Bs%20Cortas%20%28short%20url%29"><img src="http://cachi.temiga.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://cachi.temiga.org/2009/08/15/servicios_libres_para_urls_cortas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
