Improve Highlight colors.

This commit is contained in:
KJ 2024-05-13 02:03:16 -04:00
parent b996cf838d
commit 32d05121f0
1 changed files with 279 additions and 279 deletions

View File

@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2024-05-13 lun 01:52 -->
<!-- 2024-05-13 lun 02:02 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Duckbrain - Manual de inicio</title>
@ -219,57 +219,57 @@ $(function() { $('.note').before("<p class='admonition-title note'>Note</p>")
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org10b0c1d">1. Introducción</a>
<li><a href="#org0c680b9">1. Introducción</a>
<ul>
<li><a href="#org11d9b88">1.1. Estructura de archivos de Duckbrain</a>
<li><a href="#orga543768">1.1. Estructura de archivos de Duckbrain</a>
<ul>
<li><a href="#org96139e8">1.1.1. Carpeta src</a></li>
<li><a href="#org86b1cce">1.1.2. Carpetas: Controllers, Libs, Models, Middlewares, Views y Routers</a></li>
<li><a href="#org0e81a8c">1.1.3. Los archivos en la raíz</a></li>
<li><a href="#org26985c7">1.1.4. Los archivos en la carpeta Libs</a></li>
<li><a href="#org515f3c2">1.1.1. Carpeta src</a></li>
<li><a href="#org6d41d6c">1.1.2. Carpetas: Controllers, Libs, Models, Middlewares, Views y Routers</a></li>
<li><a href="#orgbaf73d6">1.1.3. Los archivos en la raíz</a></li>
<li><a href="#org0579e36">1.1.4. Los archivos en la carpeta Libs</a></li>
</ul>
</li>
<li><a href="#org44f80cd">1.2. Comprendiendo el arranque del sistema</a></li>
<li><a href="#org2fdb6ee">1.3. Extra: Namespaces</a></li>
<li><a href="#orgc3629e2">1.2. Comprendiendo el arranque del sistema</a></li>
<li><a href="#orgb6ac824">1.3. Extra: Namespaces</a></li>
</ul>
</li>
<li><a href="#org8313545">2. Primeros pasos</a>
<li><a href="#orge8f976b">2. Primeros pasos</a>
<ul>
<li><a href="#org5280919">2.1. Usando un Webserver para el desarrollo</a></li>
<li><a href="#org7a43ad7">2.2. Hola mundo</a>
<li><a href="#org5030d65">2.1. Usando un Webserver para el desarrollo</a></li>
<li><a href="#orgaf9e60a">2.2. Hola mundo</a>
<ul>
<li><a href="#orgd562ca3">2.2.1. Explicación del código del hola mundo</a></li>
<li><a href="#org1b78df9">2.2.1. Explicación del código del hola mundo</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgf27c450">3. Mi primera aplicación</a>
<li><a href="#org3d0bbaa">3. Mi primera aplicación</a>
<ul>
<li><a href="#orgc8d4648">3.1. Definición del proyecto</a></li>
<li><a href="#org06ff737">3.2. Base de datos</a>
<li><a href="#org4632ad9">3.1. Definición del proyecto</a></li>
<li><a href="#org6399f35">3.2. Base de datos</a>
<ul>
<li><a href="#orgb1a7788">3.2.1. Configuración de Duckbrain para la base de datos</a></li>
<li><a href="#org53faac3">3.2.1. Configuración de Duckbrain para la base de datos</a></li>
</ul>
</li>
<li><a href="#orgb0fda57">3.3. Modelos</a></li>
<li><a href="#orgbf8f939">3.4. Creando vistas</a></li>
<li><a href="#org9aba6d4">3.5. Mostrando el formulario</a>
<li><a href="#org6650743">3.3. Modelos</a></li>
<li><a href="#org091dca6">3.4. Creando vistas</a></li>
<li><a href="#org45e7d2c">3.5. Mostrando el formulario</a>
<ul>
<li><a href="#org470ae75">3.5.1. Extra: Explicación del tipo callable</a></li>
<li><a href="#org03756c5">3.5.1. Extra: Explicación del tipo callable</a></li>
</ul>
</li>
<li><a href="#org546c136">3.6. Creando la nota encriptada</a></li>
<li><a href="#orgb99cb8f">3.7. Recta final: Desencriptando y mostrando las notas</a></li>
<li><a href="#orgaddaa69">3.8. Repositorio</a></li>
<li><a href="#org1704573">3.6. Creando la nota encriptada</a></li>
<li><a href="#orgc2600c6">3.7. Recta final: Desencriptando y mostrando las notas</a></li>
<li><a href="#orgb6a2dc7">3.8. Repositorio</a></li>
</ul>
</li>
<li><a href="#org21d2ee2">4. Contacto</a></li>
<li><a href="#org792cd68">4. Contacto</a></li>
</ul>
</div>
</div>
<div id="outline-container-org10b0c1d" class="outline-2">
<h2 id="org10b0c1d"><span class="section-number-2">1.</span> Introducción</h2>
<div id="outline-container-org0c680b9" class="outline-2">
<h2 id="org0c680b9"><span class="section-number-2">1.</span> Introducción</h2>
<div class="outline-text-2" id="text-1">
<p>
<a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a> es un conjunto de librerías creadas especialmente pensando en hacer algo simple, que cualquiera pueda deshacer y rearmar fácilmente. Gracias a esa versatilidad, puedes usarlo cualquier proyecto, peo por ello debes tener en cuenta que mientras más grande sea el proyecto, mayor será el nivel de ingeniería que se requerirá de tu parte para acondicionar esta herramienta a los requerimientos.
@ -284,8 +284,8 @@ Ducho eso, vamos a comenzar&#x2026;
</p>
</div>
<div id="outline-container-org11d9b88" class="outline-3">
<h3 id="org11d9b88"><span class="section-number-3">1.1.</span> Estructura de archivos de Duckbrain</h3>
<div id="outline-container-orga543768" class="outline-3">
<h3 id="orga543768"><span class="section-number-3">1.1.</span> Estructura de archivos de Duckbrain</h3>
<div class="outline-text-3" id="text-1-1">
<p>
Al entrar a la carpeta de Duckbrain encontraras esta estructura de archivos (ignoramos el readme.org porque no es necesario).
@ -318,8 +318,8 @@ Primero veamos las carpetas y luego los archivos:
</p>
</div>
<div id="outline-container-org96139e8" class="outline-4">
<h4 id="org96139e8"><span class="section-number-4">1.1.1.</span> Carpeta src</h4>
<div id="outline-container-org515f3c2" class="outline-4">
<h4 id="org515f3c2"><span class="section-number-4">1.1.1.</span> Carpeta src</h4>
<div class="outline-text-4" id="text-1-1-1">
<p>
En esta carpeta se encuentra todo el código PHP.
@ -331,8 +331,8 @@ Como todo en <a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a>, esta ruta
</div>
</div>
<div id="outline-container-org86b1cce" class="outline-4">
<h4 id="org86b1cce"><span class="section-number-4">1.1.2.</span> Carpetas: Controllers, Libs, Models, Middlewares, Views y Routers</h4>
<div id="outline-container-org6d41d6c" class="outline-4">
<h4 id="org6d41d6c"><span class="section-number-4">1.1.2.</span> Carpetas: Controllers, Libs, Models, Middlewares, Views y Routers</h4>
<div class="outline-text-4" id="text-1-1-2">
<p>
Si sabes de estructuras de diseño, el nombre de estas carpetas ya te debe quedar claro qué va en cada una de ellas, puesto que en el nombre de la carpeta dice lo que va a ir dentro.
@ -343,7 +343,7 @@ Si no sabes que es un Model, un Middleware, Controller, etc. entonces requieres
</p>
<p>
Al igual que con la carpeta <code>src</code>, todo lo puedes cambiar y colocar otra estructura de carpetas y las únicas que requerirían algún tipo de edición de archivos serían las carpetas <code>Views</code> y <code>Router</code> que se editarían en el archivo <code>src/Libs/View.php</code> y el archivo <code>index.php</code> respectivamente. Las demás carpetas las puedes borrar cuando quieras y usar otra estructura, pero por lo pronto no lo hagas al menos hasta que hayas pasado la sección <a href="#orgf27c450">3</a> que es cuando ya habrías leído y, con algo de suerte y pericia, asimilado los conocimientos necesarios para hacer lo que te de la gana con este ser de librería al punto de que ya le podrás colocar las tuyas propias, le borrarás alguna de las que viene por defecto porque no la necesites o incluso crees una versión extendida de las que viene por defecto para poder añadirle alguna mejora que necesites.
Al igual que con la carpeta <code>src</code>, todo lo puedes cambiar y colocar otra estructura de carpetas y las únicas que requerirían algún tipo de edición de archivos serían las carpetas <code>Views</code> y <code>Router</code> que se editarían en el archivo <code>src/Libs/View.php</code> y el archivo <code>index.php</code> respectivamente. Las demás carpetas las puedes borrar cuando quieras y usar otra estructura, pero por lo pronto no lo hagas al menos hasta que hayas pasado la sección <a href="#org3d0bbaa">3</a> que es cuando ya habrías leído y, con algo de suerte y pericia, asimilado los conocimientos necesarios para hacer lo que te de la gana con este ser de librería al punto de que ya le podrás colocar las tuyas propias, le borrarás alguna de las que viene por defecto porque no la necesites o incluso crees una versión extendida de las que viene por defecto para poder añadirle alguna mejora que necesites.
</p>
<p>
@ -356,8 +356,8 @@ Para el resto de mortales y gente con menos arrogancia, pueden seguir un ratito
</div>
</div>
<div id="outline-container-org0e81a8c" class="outline-4">
<h4 id="org0e81a8c"><span class="section-number-4">1.1.3.</span> Los archivos en la raíz</h4>
<div id="outline-container-orgbaf73d6" class="outline-4">
<h4 id="orgbaf73d6"><span class="section-number-4">1.1.3.</span> Los archivos en la raíz</h4>
<div class="outline-text-4" id="text-1-1-3">
<p>
El <code>.htaccess</code> es un archivo que te será útil si usas apache o algún otro servidor web que use estos archivos y lo que lleva es una configuración especial para que funcionen las <b>rutas virtuales</b>, o sea, para que podamos crear una ruta tipo <code>mitsitio.com/mi-ruta-virtual/</code> y podamos decir que allí muestre un mensaje de <code>Hola mundo</code>, a pesar de que la carpeta <code>mi-ruta-virtual</code> no exista, ni haya dentro ningún archivo index que tenga el texto <code>Hola mundo</code>. Si esta aún no lo entendiste, no exasperes que ya lo vas a entender en un momento y ahí podrás volver a repasar esta parte.
@ -368,8 +368,8 @@ En caso de que uses nginx, debes configurar el webserver con la regla equivalent
</p>
<div class="org-src-container">
<pre class="src src-nginx"><span style="color: #deb07a; font-weight: bold;">location</span> <span style="color: #8fcfd0;">/</span> {
<span style="color: #deb07a; font-weight: bold;">try_files</span> <span style="color: #ffaacf;">$uri</span> <span style="color: #ffaacf;">$uri</span>/ ./index.php<span style="color: #ffaacf;">$args</span>;
<pre class="src src-nginx"><span style="color: #a020f0;">location</span> <span style="color: #0000ff;">/</span> {
<span style="color: #a020f0;">try_files</span> <span style="color: #a0522d;">$uri</span> <span style="color: #a0522d;">$uri</span>/ ./index.php<span style="color: #a0522d;">$args</span>;
}
</pre>
</div>
@ -384,8 +384,8 @@ El archivo <code>config.php</code>, como su nombre indica, es el archivo para co
</div>
</div>
<div id="outline-container-org26985c7" class="outline-4">
<h4 id="org26985c7"><span class="section-number-4">1.1.4.</span> Los archivos en la carpeta Libs</h4>
<div id="outline-container-org0579e36" class="outline-4">
<h4 id="org0579e36"><span class="section-number-4">1.1.4.</span> Los archivos en la carpeta Libs</h4>
<div class="outline-text-4" id="text-1-1-4">
<p>
Son básicamente librerías, cada una tiene como nombre lo que hace, no tiene mucho misterio y al igual que antes con las carpetas, si sabes algo de diseño, con leer el nombre de las mismas te debería bastar para saber lo que hacen. Si no lo entiendes, sigue leyendo el manual que hablaremos de casi todas poco a poco.
@ -394,8 +394,8 @@ Son básicamente librerías, cada una tiene como nombre lo que hace, no tiene mu
</div>
</div>
<div id="outline-container-org44f80cd" class="outline-3">
<h3 id="org44f80cd"><span class="section-number-3">1.2.</span> Comprendiendo el arranque del sistema</h3>
<div id="outline-container-orgc3629e2" class="outline-3">
<h3 id="orgc3629e2"><span class="section-number-3">1.2.</span> Comprendiendo el arranque del sistema</h3>
<div class="outline-text-3" id="text-1-2">
<p>
Como la intención de este manual es que comprendas <a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a> y no solo seas un robot que copia y pega de la documentación, vamos a calentar comprendiendo el código que lo arranca.
@ -410,7 +410,7 @@ Supongamos que entramos en la ruta <code>/hola</code> (o sea, <code>misitio.com/
</p>
<p>
Primero intenta comprobar si el fichero "hola" existe en la ruta a la que se desea acceder, si no es el caso prueba si existe una carpeta "hola" y si tampoco sucede eso, reenvía todo a <code>index.php</code> y le pide que él se encargue de ahí en adelante. Si llega hasta la tercera opción decimos que es una <b>ruta virtual</b> (lo que mencionamos en <a href="#org0e81a8c">Los archivos en la raiz</a>), porque devolveremos algo desde el PHP simulando un archivo o una carpeta, a pesar de que esa ruta realmente no existe ni como fichero ni como carpeta.
Primero intenta comprobar si el fichero "hola" existe en la ruta a la que se desea acceder, si no es el caso prueba si existe una carpeta "hola" y si tampoco sucede eso, reenvía todo a <code>index.php</code> y le pide que él se encargue de ahí en adelante. Si llega hasta la tercera opción decimos que es una <b>ruta virtual</b> (lo que mencionamos en <a href="#orgbaf73d6">Los archivos en la raiz</a>), porque devolveremos algo desde el PHP simulando un archivo o una carpeta, a pesar de que esa ruta realmente no existe ni como fichero ni como carpeta.
</p>
<p>
@ -418,7 +418,7 @@ Ahora veamos que hace <code>index.php</code>, en la primera línea nos dice esto
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">require_once</span>(<span style="color: #f3a0a0;">'config.php'</span>);
<pre class="src src-php"><span style="color: #a020f0;">require_once</span>(<span style="color: #8b2252;">'config.php'</span>);
</pre>
</div>
@ -427,7 +427,7 @@ Aquí carga el archivo <code>config.php</code>, o sea, carga la configuración.
</p>
<div class="org-src-container">
<pre class="src src-php">define(<span style="color: #f3a0a0;">'ROOT_DIR'</span>, <span style="color: #e3b0c0; font-weight: bold;">__DIR__</span>);
<pre class="src src-php">define(<span style="color: #8b2252;">'ROOT_DIR'</span>, <span style="color: #483d8b;">__DIR__</span>);
</pre>
</div>
@ -440,7 +440,7 @@ La siguiente línea también es la definición de una constante:
</p>
<div class="org-src-container">
<pre class="src src-php">define(<span style="color: #f3a0a0;">'ROOT_CORE'</span>, <span style="color: #80aadf;">ROOT_DIR</span>.<span style="color: #f3a0a0;">'/src'</span>);
<pre class="src src-php">define(<span style="color: #8b2252;">'ROOT_CORE'</span>, <span style="color: #008b8b;">ROOT_DIR</span>.<span style="color: #8b2252;">'/src'</span>);
</pre>
</div>
@ -449,14 +449,14 @@ Esta constante <code>ROOT_CORE</code> lo que hace es tener la ruta de src. Hasta
</p>
<div class="org-src-container">
<pre class="src src-php">spl_autoload_register(<span style="color: #deb07a; font-weight: bold;">function</span> ($<span style="color: #ffaacf;">className</span>) {
$<span style="color: #ffaacf;">fp</span> = str_replace(<span style="color: #f3a0a0;">'</span><span style="color: #65c5a8;">\</span><span style="color: #e47980;">\</span><span style="color: #f3a0a0;">'</span>,<span style="color: #f3a0a0;">'/'</span>,$<span style="color: #ffaacf;">className</span>);
$<span style="color: #ffaacf;">name</span> = basename($<span style="color: #ffaacf;">fp</span>);
$<span style="color: #ffaacf;">dir</span> = dirname($<span style="color: #ffaacf;">fp</span>);
$<span style="color: #ffaacf;">file</span> = <span style="color: #80aadf;">ROOT_CORE</span>.<span style="color: #f3a0a0;">'/'</span>.$<span style="color: #ffaacf;">dir</span>.<span style="color: #f3a0a0;">'/'</span>.$<span style="color: #ffaacf;">name</span>.<span style="color: #f3a0a0;">'.php'</span>;
<span style="color: #deb07a; font-weight: bold;">if</span> (file_exists($<span style="color: #ffaacf;">file</span>)) {
<span style="color: #deb07a; font-weight: bold;">require_once</span> $<span style="color: #ffaacf;">file</span>;
<span style="color: #deb07a; font-weight: bold;">return</span>;
<pre class="src src-php">spl_autoload_register(<span style="color: #a020f0;">function</span> ($<span style="color: #a0522d;">className</span>) {
$<span style="color: #a0522d;">fp</span> = str_replace(<span style="color: #8b2252;">'</span><span style="color: #8b2252; font-weight: bold;">\</span><span style="color: #8b2252; font-weight: bold;">\</span><span style="color: #8b2252;">'</span>,<span style="color: #8b2252;">'/'</span>,$<span style="color: #a0522d;">className</span>);
$<span style="color: #a0522d;">name</span> = basename($<span style="color: #a0522d;">fp</span>);
$<span style="color: #a0522d;">dir</span> = dirname($<span style="color: #a0522d;">fp</span>);
$<span style="color: #a0522d;">file</span> = <span style="color: #008b8b;">ROOT_CORE</span>.<span style="color: #8b2252;">'/'</span>.$<span style="color: #a0522d;">dir</span>.<span style="color: #8b2252;">'/'</span>.$<span style="color: #a0522d;">name</span>.<span style="color: #8b2252;">'.php'</span>;
<span style="color: #a020f0;">if</span> (file_exists($<span style="color: #a0522d;">file</span>)) {
<span style="color: #a020f0;">require_once</span> $<span style="color: #a0522d;">file</span>;
<span style="color: #a020f0;">return</span>;
}
});
</pre>
@ -475,13 +475,13 @@ Si aún no lo entendiste, no pierdas tiempo releyendo el párrafo anterior, en s
</p>
<div class="org-src-container">
<pre class="src src-php">$<span style="color: #ffaacf;">routers</span> = glob(<span style="color: #80aadf;">ROOT_CORE</span>.<span style="color: #f3a0a0;">'/Routers/*.php'</span>);
<pre class="src src-php">$<span style="color: #a0522d;">routers</span> = glob(<span style="color: #008b8b;">ROOT_CORE</span>.<span style="color: #8b2252;">'/Routers/*.php'</span>);
<span style="color: #deb07a; font-weight: bold;">foreach</span>($<span style="color: #ffaacf;">routers</span> <span style="color: #deb07a; font-weight: bold;">as</span> $<span style="color: #ffaacf;">file</span>){
<span style="color: #deb07a; font-weight: bold;">require_once</span>($<span style="color: #ffaacf;">file</span>);
<span style="color: #a020f0;">foreach</span>($<span style="color: #a0522d;">routers</span> <span style="color: #a020f0;">as</span> $<span style="color: #a0522d;">file</span>){
<span style="color: #a020f0;">require_once</span>($<span style="color: #a0522d;">file</span>);
}
<span style="color: #80aadf;">\Libs\Router</span>::apply();
<span style="color: #008b8b;">\Libs\Router</span>::apply();
</pre>
</div>
@ -499,8 +499,8 @@ Nótese que lo que ha hecho el autoloader es convertir los backslashes o barras
</div>
</div>
<div id="outline-container-org2fdb6ee" class="outline-3">
<h3 id="org2fdb6ee"><span class="section-number-3">1.3.</span> Extra: Namespaces</h3>
<div id="outline-container-orgb6ac824" class="outline-3">
<h3 id="orgb6ac824"><span class="section-number-3">1.3.</span> Extra: Namespaces</h3>
<div class="outline-text-3" id="text-1-3">
<p>
Si eres nuevo en esto puede que no sepas lo que es un namespace, pero si también te dio curiosidad el que la una clase tenga <code>\</code> (backslash) en su nombre, entonces tienes futuro en este campo y como no quiero que te vayas de este manual, voy a explicarte rápidamente de donde viene ese backslash.
@ -511,7 +511,7 @@ Si abres el archivo <code>src/Libs/Router.php</code> verás que en su primera l
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">namespace</span> <span style="color: #a9c99f;">Libs</span>;
<pre class="src src-php"><span style="color: #a020f0;">namespace</span> <span style="color: #228b22;">Libs</span>;
</pre>
</div>
@ -530,16 +530,16 @@ Puede que ahora mismo no notes la utilidad de esto, pero es algo muy útil realm
</div>
</div>
<div id="outline-container-org8313545" class="outline-2">
<h2 id="org8313545"><span class="section-number-2">2.</span> Primeros pasos</h2>
<div id="outline-container-orge8f976b" class="outline-2">
<h2 id="orge8f976b"><span class="section-number-2">2.</span> Primeros pasos</h2>
<div class="outline-text-2" id="text-2">
<p>
Antes de ponernos a programar vamos a asegurarnos de estar en la misma pagina preparando nuestro entorno de trabajo y haciendo el usual Hola mundo.
</p>
</div>
<div id="outline-container-org5280919" class="outline-3">
<h3 id="org5280919"><span class="section-number-3">2.1.</span> Usando un Webserver para el desarrollo</h3>
<div id="outline-container-org5030d65" class="outline-3">
<h3 id="org5030d65"><span class="section-number-3">2.1.</span> Usando un Webserver para el desarrollo</h3>
<div class="outline-text-3" id="text-2-1">
<p>
Desde luego esta la carpeta con <a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a> supongo que ya la tienes con un webserver nginx o apache en localhost. Si no es el caso, al menos deberás instalarte PHP primero y una ves instalado, abres una terminal en la carpeta donde tienes Duckbrain y ejecutas:
@ -564,8 +564,8 @@ He conocido gente que usa directamente un hosting y edita los archivos allí. Es
</div>
</div>
<div id="outline-container-org7a43ad7" class="outline-3">
<h3 id="org7a43ad7"><span class="section-number-3">2.2.</span> Hola mundo</h3>
<div id="outline-container-orgaf9e60a" class="outline-3">
<h3 id="orgaf9e60a"><span class="section-number-3">2.2.</span> Hola mundo</h3>
<div class="outline-text-3" id="text-2-2">
<p>
Agarren su teclado y su editor de código que vamos a comenzar a hacer la magia. Como es usual, el primer paso para hacer nuetra aplicación, será imprimir un <code>hello world</code>.
@ -576,12 +576,12 @@ Para ello vamos a la carpeta <code>src/Routers</code> y crearemos un archivo con
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Request</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Router</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Request</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Router</span>;
<span style="color: #80aadf;">Router</span>::get(<span style="color: #f3a0a0;">'/'</span>, <span style="color: #deb07a; font-weight: bold;">function</span>(<span style="color: #a9c99f;">Request</span> $<span style="color: #ffaacf;">requets</span>) {
<span style="color: #deb07a; font-weight: bold;">echo</span> <span style="color: #f3a0a0;">"Hola mundo"</span>;
<span style="color: #008b8b;">Router</span>::get(<span style="color: #8b2252;">'/'</span>, <span style="color: #a020f0;">function</span>(<span style="color: #228b22;">Request</span> $<span style="color: #a0522d;">requets</span>) {
<span style="color: #a020f0;">echo</span> <span style="color: #8b2252;">"Hola mundo"</span>;
});
</pre>
</div>
@ -592,16 +592,16 @@ Ahora, siguiendo con el código, abrimos la URL donde estamos corriendo el sitio
</p>
</div>
<div id="outline-container-orgd562ca3" class="outline-4">
<h4 id="orgd562ca3"><span class="section-number-4">2.2.1.</span> Explicación del código del hola mundo</h4>
<div id="outline-container-org1b78df9" class="outline-4">
<h4 id="org1b78df9"><span class="section-number-4">2.2.1.</span> Explicación del código del hola mundo</h4>
<div class="outline-text-4" id="text-2-2-1">
<p>
Si entendiste el código con solo leerlo, perfecto, pero si no lo hiciste, vamos a explicarlo. Primero vamos a explicar esta parte:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #80aadf;">Router</span>::get(<span style="color: #f3a0a0;">'/'</span>, <span style="color: #deb07a; font-weight: bold;">function</span>(<span style="color: #a9c99f;">Request</span> $<span style="color: #ffaacf;">requets</span>) {
<span style="color: #deb07a; font-weight: bold;">echo</span> <span style="color: #f3a0a0;">"Hola mundo"</span>;
<pre class="src src-php"><span style="color: #008b8b;">Router</span>::get(<span style="color: #8b2252;">'/'</span>, <span style="color: #a020f0;">function</span>(<span style="color: #228b22;">Request</span> $<span style="color: #a0522d;">requets</span>) {
<span style="color: #a020f0;">echo</span> <span style="color: #8b2252;">"Hola mundo"</span>;
});
</pre>
</div>
@ -623,7 +623,7 @@ Lo que estámos haciendo aquí, es llamar al método <code>get</code> de la clas
</p>
<p>
Al igual que en <a href="#org2fdb6ee">Extra: Namespaces</a> la explicación de las primeras 2 líneas de este código, realmente no es algo de <a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a>, sino del lenguaje mismo, pero de todos modos lo explicaré.
Al igual que en <a href="#orgb6ac824">Extra: Namespaces</a> la explicación de las primeras 2 líneas de este código, realmente no es algo de <a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a>, sino del lenguaje mismo, pero de todos modos lo explicaré.
</p>
<p>
@ -631,9 +631,9 @@ Primero que nada, si no quisieramos o no tuviéramos la posibilidad de usar <cod
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #80aadf;">Libs\Router</span>::get(<span style="color: #f3a0a0;">'/'</span>, <span style="color: #deb07a; font-weight: bold;">function</span>(<span style="color: #a9c99f;">Libs\Request</span> $<span style="color: #ffaacf;">requets</span>) {
<span style="color: #deb07a; font-weight: bold;">echo</span> <span style="color: #f3a0a0;">"Hola mundo"</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #008b8b;">Libs\Router</span>::get(<span style="color: #8b2252;">'/'</span>, <span style="color: #a020f0;">function</span>(<span style="color: #228b22;">Libs\Request</span> $<span style="color: #a0522d;">requets</span>) {
<span style="color: #a020f0;">echo</span> <span style="color: #8b2252;">"Hola mundo"</span>;
});
</pre>
</div>
@ -647,7 +647,7 @@ Si un <code>namespace</code> es como dar un apellido, <code>use</code> es como d
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Router</span> <span style="color: #deb07a; font-weight: bold;">as</span> <span style="color: #a9c99f;">Rutercito</span>;
<pre class="src src-php"><span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Router</span> <span style="color: #a020f0;">as</span> <span style="color: #228b22;">Rutercito</span>;
</pre>
</div>
@ -663,16 +663,16 @@ Prueba ponerle apodos ambas clases y usar sus distintos nombres en el código. E
</div>
</div>
<div id="outline-container-orgf27c450" class="outline-2">
<h2 id="orgf27c450"><span class="section-number-2">3.</span> Mi primera aplicación</h2>
<div id="outline-container-org3d0bbaa" class="outline-2">
<h2 id="org3d0bbaa"><span class="section-number-2">3.</span> Mi primera aplicación</h2>
<div class="outline-text-2" id="text-3">
<p>
Ya hicimos un hola mundo, ahora vamos a hacer algo completo de verdad. Una buena práctica a la hora de crear proyectos es comenzar a definir lo que vamos a hacer, por lo tanto:
</p>
</div>
<div id="outline-container-orgc8d4648" class="outline-3">
<h3 id="orgc8d4648"><span class="section-number-3">3.1.</span> Definición del proyecto</h3>
<div id="outline-container-org4632ad9" class="outline-3">
<h3 id="org4632ad9"><span class="section-number-3">3.1.</span> Definición del proyecto</h3>
<div class="outline-text-3" id="text-3-1">
<p>
Vamos a hacer un sistema que nos permita compartir textos de manera secreta. Una persona cualquiera podrá entrar a nuestra web, escribir un texto o nota secreta y se le entregará un enlace con el que puede compartir esa nota secreta.
@ -692,8 +692,8 @@ El nombre de la aplicación será <b>Ignota</b>. No soy bueno con los nombres y
</div>
</div>
<div id="outline-container-org06ff737" class="outline-3">
<h3 id="org06ff737"><span class="section-number-3">3.2.</span> Base de datos</h3>
<div id="outline-container-org6399f35" class="outline-3">
<h3 id="org6399f35"><span class="section-number-3">3.2.</span> Base de datos</h3>
<div class="outline-text-3" id="text-3-2">
<p>
Este sistema no es algo complicado en lo que al diseño de la base de datos se refiere, por lo que sólo vendría a tener una tabla:
@ -716,11 +716,11 @@ El código para crear las tablas en SQLite sería este:
</p>
<div class="org-src-container">
<pre class="src src-sqlite"><span style="color: #deb07a; font-weight: bold;">CREATE</span> <span style="color: #deb07a; font-weight: bold;">TABLE</span> <span style="color: #8fcfd0;">notes</span> (
id <span style="color: #a9c99f;">INTEGER</span> <span style="color: #deb07a; font-weight: bold;">PRIMARY</span> <span style="color: #deb07a; font-weight: bold;">KEY</span>,
<pre class="src src-sqlite"><span style="color: #a020f0;">CREATE</span> <span style="color: #a020f0;">TABLE</span> <span style="color: #0000ff;">notes</span> (
id <span style="color: #228b22;">INTEGER</span> <span style="color: #a020f0;">PRIMARY</span> <span style="color: #a020f0;">KEY</span>,
content TEXT,
expire_at <span style="color: #a9c99f;">INTEGER</span>,
max_views <span style="color: #a9c99f;">INTEGER</span>
expire_at <span style="color: #228b22;">INTEGER</span>,
max_views <span style="color: #228b22;">INTEGER</span>
);
</pre>
</div>
@ -730,11 +730,11 @@ El código para crear las tablas en SQLite sería este:
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #deb07a; font-weight: bold;">CREATE</span> <span style="color: #deb07a; font-weight: bold;">TABLE</span> <span style="color: #8fcfd0;">notes</span> (
id BIGINT <span style="color: #deb07a; font-weight: bold;">PRIMARY</span> <span style="color: #deb07a; font-weight: bold;">KEY</span> AUTO_INCREMENT,
<pre class="src src-sql"><span style="color: #a020f0;">CREATE</span> <span style="color: #a020f0;">TABLE</span> <span style="color: #0000ff;">notes</span> (
id BIGINT <span style="color: #a020f0;">PRIMARY</span> <span style="color: #a020f0;">KEY</span> AUTO_INCREMENT,
content TEXT,
expire_at <span style="color: #a9c99f;">INT</span> <span style="color: #deb07a; font-weight: bold;">default</span> <span style="color: #deb07a; font-weight: bold;">null</span>,
max_views <span style="color: #a9c99f;">INT</span> <span style="color: #deb07a; font-weight: bold;">default</span> 0
expire_at <span style="color: #228b22;">INT</span> <span style="color: #a020f0;">default</span> <span style="color: #a020f0;">null</span>,
max_views <span style="color: #228b22;">INT</span> <span style="color: #a020f0;">default</span> 0
);
</pre>
</div>
@ -744,25 +744,25 @@ Voy a obviar la parte de cómo crear las bases de datos ya que ambas cosas se pu
</p>
</div>
<div id="outline-container-orgb1a7788" class="outline-4">
<h4 id="orgb1a7788"><span class="section-number-4">3.2.1.</span> Configuración de Duckbrain para la base de datos</h4>
<div id="outline-container-org53faac3" class="outline-4">
<h4 id="org53faac3"><span class="section-number-4">3.2.1.</span> Configuración de Duckbrain para la base de datos</h4>
<div class="outline-text-4" id="text-3-2-1">
<p>
Desde luego, <a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a> va necesitar tener la información de la base de datos, por lo que abriremos el archivo y veremos algo como esto:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
define(<span style="color: #f3a0a0;">'DB_TYPE'</span>, <span style="color: #f3a0a0;">'mysql'</span>);
define(<span style="color: #f3a0a0;">'DB_HOST'</span>, <span style="color: #f3a0a0;">'localhost'</span>);
define(<span style="color: #f3a0a0;">'DB_NAME'</span>, <span style="color: #f3a0a0;">''</span>);
define(<span style="color: #f3a0a0;">'DB_USER'</span>, <span style="color: #f3a0a0;">''</span>);
define(<span style="color: #f3a0a0;">'DB_PASS'</span>, <span style="color: #f3a0a0;">''</span>);
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
define(<span style="color: #8b2252;">'DB_TYPE'</span>, <span style="color: #8b2252;">'mysql'</span>);
define(<span style="color: #8b2252;">'DB_HOST'</span>, <span style="color: #8b2252;">'localhost'</span>);
define(<span style="color: #8b2252;">'DB_NAME'</span>, <span style="color: #8b2252;">''</span>);
define(<span style="color: #8b2252;">'DB_USER'</span>, <span style="color: #8b2252;">''</span>);
define(<span style="color: #8b2252;">'DB_PASS'</span>, <span style="color: #8b2252;">''</span>);
<span style="color: #a0a0cf; font-style: italic;">//</span><span style="color: #a0a0cf; font-style: italic;">define('SITE_URL', '');</span>
<span style="color: #b22222;">//</span><span style="color: #b22222;">define('SITE_URL', '');</span>
define(<span style="color: #f3a0a0;">'ROOT_DIR'</span>, <span style="color: #e3b0c0; font-weight: bold;">__DIR__</span>);
<span style="color: #65c5a8;">?&gt;</span>
define(<span style="color: #8b2252;">'ROOT_DIR'</span>, <span style="color: #483d8b;">__DIR__</span>);
<span style="color: #483d8b;">?&gt;</span>
</pre>
</div>
@ -775,17 +775,17 @@ El código terminaría así:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
define(<span style="color: #f3a0a0;">'DB_TYPE'</span>, <span style="color: #f3a0a0;">'sqlite'</span>);
define(<span style="color: #f3a0a0;">'DB_HOST'</span>, <span style="color: #f3a0a0;">'localhost'</span>);
define(<span style="color: #f3a0a0;">'DB_NAME'</span>, <span style="color: #f3a0a0;">'mi-bd.sqlite'</span>);
define(<span style="color: #f3a0a0;">'DB_USER'</span>, <span style="color: #f3a0a0;">''</span>);
define(<span style="color: #f3a0a0;">'DB_PASS'</span>, <span style="color: #f3a0a0;">''</span>);
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
define(<span style="color: #8b2252;">'DB_TYPE'</span>, <span style="color: #8b2252;">'sqlite'</span>);
define(<span style="color: #8b2252;">'DB_HOST'</span>, <span style="color: #8b2252;">'localhost'</span>);
define(<span style="color: #8b2252;">'DB_NAME'</span>, <span style="color: #8b2252;">'mi-bd.sqlite'</span>);
define(<span style="color: #8b2252;">'DB_USER'</span>, <span style="color: #8b2252;">''</span>);
define(<span style="color: #8b2252;">'DB_PASS'</span>, <span style="color: #8b2252;">''</span>);
define(<span style="color: #f3a0a0;">'SITE_URL'</span>, <span style="color: #f3a0a0;">'http://localhost/'</span>);
define(<span style="color: #8b2252;">'SITE_URL'</span>, <span style="color: #8b2252;">'http://localhost/'</span>);
define(<span style="color: #f3a0a0;">'ROOT_DIR'</span>, <span style="color: #e3b0c0; font-weight: bold;">__DIR__</span>);
<span style="color: #65c5a8;">?&gt;</span>
define(<span style="color: #8b2252;">'ROOT_DIR'</span>, <span style="color: #483d8b;">__DIR__</span>);
<span style="color: #483d8b;">?&gt;</span>
</pre>
</div>
@ -796,8 +796,8 @@ Como nuestra base de dato es solo un archivo sqlite, no requerimos de <code>DB_H
</div>
</div>
<div id="outline-container-orgb0fda57" class="outline-3">
<h3 id="orgb0fda57"><span class="section-number-3">3.3.</span> Modelos</h3>
<div id="outline-container-org6650743" class="outline-3">
<h3 id="org6650743"><span class="section-number-3">3.3.</span> Modelos</h3>
<div class="outline-text-3" id="text-3-3">
<p>
Debo hacer incapié en que nada está escrito en piedra y podemos hacer las cosas de muchas manera con <a href="https://git.kj2.me/kj/duckbrain">Duckbrain</a>, pero en este caso, siguiendo la estructura MVC, vamos a crear modelos, vistas y controladores.
@ -808,15 +808,15 @@ Comenzando con los modelos, que en realidad para este proyecto sería en singula
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">namespace</span> <span style="color: #a9c99f;">Models</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">namespace</span> <span style="color: #228b22;">Models</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Model</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Model</span>;
<span style="color: #deb07a; font-weight: bold;">class</span> <span style="color: #a9c99f;">Note</span> <span style="color: #deb07a; font-weight: bold;">extends</span> <span style="color: #a9c99f;">Model</span> {
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #a9c99f;">string</span> $<span style="color: #ffaacf;">content</span>;
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #a9c99f;">?int</span> $<span style="color: #ffaacf;">expire_at</span>;
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #a9c99f;">int</span> $<span style="color: #ffaacf;">max_views</span> = 0;
<span style="color: #a020f0;">class</span> <span style="color: #228b22;">Note</span> <span style="color: #a020f0;">extends</span> <span style="color: #228b22;">Model</span> {
<span style="color: #a020f0;">public</span> <span style="color: #228b22;">string</span> $<span style="color: #a0522d;">content</span>;
<span style="color: #a020f0;">public</span> <span style="color: #228b22;">?int</span> $<span style="color: #a0522d;">expire_at</span>;
<span style="color: #a020f0;">public</span> <span style="color: #228b22;">int</span> $<span style="color: #a0522d;">max_views</span> = 0;
}
</pre>
</div>
@ -834,27 +834,27 @@ Nada de lo mencionado en el anterior párrafo lo necesitaremos, pero de todos mo
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">namespace</span> <span style="color: #a9c99f;">Models</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">namespace</span> <span style="color: #228b22;">Models</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Model</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Model</span>;
<span style="color: #deb07a; font-weight: bold;">class</span> <span style="color: #a9c99f;">Note</span> <span style="color: #deb07a; font-weight: bold;">extends</span> <span style="color: #a9c99f;">Model</span> {
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #a9c99f;">string</span> $<span style="color: #ffaacf;">content</span>;
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #a9c99f;">?int</span> $<span style="color: #ffaacf;">expire_at</span>;
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #a9c99f;">int</span> $<span style="color: #ffaacf;">max_views</span> = 0;
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #a9c99f;">string</span> $<span style="color: #ffaacf;">tmp_cache</span>;
<span style="color: #deb07a; font-weight: bold;">private</span> <span style="color: #a9c99f;">string</span> $<span style="color: #ffaacf;">password</span>;
<span style="color: #deb07a; font-weight: bold;">protected</span> <span style="color: #a9c99f;">string</span> $<span style="color: #ffaacf;">salt</span>;
<span style="color: #a020f0;">class</span> <span style="color: #228b22;">Note</span> <span style="color: #a020f0;">extends</span> <span style="color: #228b22;">Model</span> {
<span style="color: #a020f0;">public</span> <span style="color: #228b22;">string</span> $<span style="color: #a0522d;">content</span>;
<span style="color: #a020f0;">public</span> <span style="color: #228b22;">?int</span> $<span style="color: #a0522d;">expire_at</span>;
<span style="color: #a020f0;">public</span> <span style="color: #228b22;">int</span> $<span style="color: #a0522d;">max_views</span> = 0;
<span style="color: #a020f0;">public</span> <span style="color: #228b22;">string</span> $<span style="color: #a0522d;">tmp_cache</span>;
<span style="color: #a020f0;">private</span> <span style="color: #228b22;">string</span> $<span style="color: #a0522d;">password</span>;
<span style="color: #a020f0;">protected</span> <span style="color: #228b22;">string</span> $<span style="color: #a0522d;">salt</span>;
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Cambiamos el nombre de la tabla a la que se relacionar&#225; nuestro modelo.</span>
<span style="color: #deb07a; font-weight: bold;">static</span> <span style="color: #deb07a; font-weight: bold;">protected</span> <span style="color: #a9c99f;">string</span> $<span style="color: #ffaacf;">table</span> = <span style="color: #f3a0a0;">'se_llama_como_YO_quiero_AAAhh'</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Cambiamos el nombre de la tabla a la que se relacionar&#225; nuestro modelo.</span>
<span style="color: #a020f0;">static</span> <span style="color: #a020f0;">protected</span> <span style="color: #228b22;">string</span> $<span style="color: #a0522d;">table</span> = <span style="color: #8b2252;">'se_llama_como_YO_quiero_AAAhh'</span>;
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Forzamos a que se guarden las propiedades salt y password a pesar de que no son p&#250;blicas.</span>
<span style="color: #deb07a; font-weight: bold;">static</span> <span style="color: #deb07a; font-weight: bold;">protected</span> <span style="color: #a9c99f;">array</span> $<span style="color: #ffaacf;">forceSave</span> = [<span style="color: #f3a0a0;">'password'</span>, <span style="color: #f3a0a0;">'salt'</span>];
<span style="color: #b22222;">// </span><span style="color: #b22222;">Forzamos a que se guarden las propiedades salt y password a pesar de que no son p&#250;blicas.</span>
<span style="color: #a020f0;">static</span> <span style="color: #a020f0;">protected</span> <span style="color: #228b22;">array</span> $<span style="color: #a0522d;">forceSave</span> = [<span style="color: #8b2252;">'password'</span>, <span style="color: #8b2252;">'salt'</span>];
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Frozamos a no guuardar la propiedad p&#250;blica tmp_cache</span>
<span style="color: #deb07a; font-weight: bold;">static</span> <span style="color: #deb07a; font-weight: bold;">protected</span> <span style="color: #a9c99f;">array</span> $<span style="color: #ffaacf;">ignoreSave</span> = [<span style="color: #f3a0a0;">'tmp_cache'</span>];
<span style="color: #b22222;">// </span><span style="color: #b22222;">Frozamos a no guuardar la propiedad p&#250;blica tmp_cache</span>
<span style="color: #a020f0;">static</span> <span style="color: #a020f0;">protected</span> <span style="color: #228b22;">array</span> $<span style="color: #a0522d;">ignoreSave</span> = [<span style="color: #8b2252;">'tmp_cache'</span>];
}
</pre>
</div>
@ -865,8 +865,8 @@ Nada de lo mencionado en el anterior párrafo lo necesitaremos, pero de todos mo
</div>
</div>
<div id="outline-container-orgbf8f939" class="outline-3">
<h3 id="orgbf8f939"><span class="section-number-3">3.4.</span> Creando vistas</h3>
<div id="outline-container-org091dca6" class="outline-3">
<h3 id="org091dca6"><span class="section-number-3">3.4.</span> Creando vistas</h3>
<div class="outline-text-3" id="text-3-4">
<p>
En este caso solo necesitaremos una sola vista, por ello no crearemos archivos aparte para el código CSS, sino que irá todo en un solo archivo.
@ -877,13 +877,13 @@ Vamos a crear un archivo llamado <code>CreateNote.php</code> en <code>src/Views/
</p>
<div class="org-src-container">
<pre class="src src-html">&lt;<span style="color: #deb07a; font-weight: bold;">!DOCTYPE</span> html&gt;
&lt;<span style="color: #8fcfd0;">html</span>&gt;
&lt;<span style="color: #8fcfd0;">head</span>&gt;
&lt;<span style="color: #8fcfd0;">meta</span> <span style="color: #ffaacf;">charset</span>=<span style="color: #f3a0a0;">"UTF-8"</span>/&gt;
&lt;<span style="color: #8fcfd0;">meta</span> <span style="color: #ffaacf;">name</span>=<span style="color: #f3a0a0;">"viewport"</span> <span style="color: #ffaacf;">content</span>=<span style="color: #f3a0a0;">"width=device-width, initial-scale=1.0"</span>&gt;
&lt;<span style="color: #8fcfd0;">title</span>&gt;<span style="font-weight: bold; text-decoration: underline;">Ignota - Notas encriptadas punto a punto</span>&lt;/<span style="color: #8fcfd0;">title</span>&gt;
&lt;<span style="color: #8fcfd0;">style</span>&gt;
<pre class="src src-html">&lt;<span style="color: #a020f0;">!DOCTYPE</span> html&gt;
&lt;<span style="color: #0000ff;">html</span>&gt;
&lt;<span style="color: #0000ff;">head</span>&gt;
&lt;<span style="color: #0000ff;">meta</span> <span style="color: #a0522d;">charset</span>=<span style="color: #8b2252;">"UTF-8"</span>/&gt;
&lt;<span style="color: #0000ff;">meta</span> <span style="color: #a0522d;">name</span>=<span style="color: #8b2252;">"viewport"</span> <span style="color: #a0522d;">content</span>=<span style="color: #8b2252;">"width=device-width, initial-scale=1.0"</span>&gt;
&lt;<span style="color: #0000ff;">title</span>&gt;<span style="font-weight: bold; text-decoration: underline;">Ignota - Notas encriptadas punto a punto</span>&lt;/<span style="color: #0000ff;">title</span>&gt;
&lt;<span style="color: #0000ff;">style</span>&gt;
form {
display: grid;
place-content: center;
@ -918,22 +918,22 @@ Vamos a crear un archivo llamado <code>CreateNote.php</code> en <code>src/Views/
font-weight: bold;
display: ;
}
&lt;/<span style="color: #8fcfd0;">style</span>&gt;
&lt;/<span style="color: #8fcfd0;">head</span>&gt;
&lt;<span style="color: #8fcfd0;">body</span>&gt;
&lt;<span style="color: #8fcfd0;">form</span> <span style="color: #ffaacf;">method</span>=<span style="color: #f3a0a0;">"POST"</span>&gt;
&lt;<span style="color: #8fcfd0;">textarea</span> <span style="color: #ffaacf;">name</span>=<span style="color: #f3a0a0;">"note"</span> <span style="color: #ffaacf;">placeholder</span>=<span style="color: #f3a0a0;">"Escribe tu texto a encriptar"</span>&gt;&lt;/<span style="color: #8fcfd0;">textarea</span>&gt;
&lt;<span style="color: #8fcfd0;">fieldset</span> <span style="color: #ffaacf;">class</span>=<span style="color: #f3a0a0;">"config"</span>&gt;
&lt;<span style="color: #8fcfd0;">legend</span>&gt;Configuraci&#243;n de expiraci&#243;n&lt;/<span style="color: #8fcfd0;">legend</span>&gt;
&lt;<span style="color: #8fcfd0;">label</span> <span style="color: #ffaacf;">for</span>=<span style="color: #f3a0a0;">"max_views"</span>&gt;Visualizaciones m&#225;ximas (0 = infinitas):&lt;/<span style="color: #8fcfd0;">label</span>&gt;
&lt;<span style="color: #8fcfd0;">input</span> <span style="color: #ffaacf;">name</span>=<span style="color: #f3a0a0;">"max_views"</span> <span style="color: #ffaacf;">type</span>=<span style="color: #f3a0a0;">"int"</span> <span style="color: #ffaacf;">value</span>=<span style="color: #f3a0a0;">"0"</span>&gt;
&lt;<span style="color: #8fcfd0;">label</span> <span style="color: #ffaacf;">for</span>=<span style="color: #f3a0a0;">"expiration"</span>&gt;Tiempo de expiraci&#243;n (en minutos):&lt;/<span style="color: #8fcfd0;">label</span>&gt;
&lt;<span style="color: #8fcfd0;">input</span> <span style="color: #ffaacf;">name</span>=<span style="color: #f3a0a0;">"expiration"</span> <span style="color: #ffaacf;">type</span>=<span style="color: #f3a0a0;">"int"</span> <span style="color: #ffaacf;">value</span>=<span style="color: #f3a0a0;">"15"</span>&gt;
&lt;/<span style="color: #8fcfd0;">fieldset</span>&gt;
&lt;<span style="color: #8fcfd0;">button</span> <span style="color: #ffaacf;">type</span>=<span style="color: #f3a0a0;">"submit"</span>&gt;Guardar&lt;/<span style="color: #8fcfd0;">button</span>&gt;
&lt;/<span style="color: #8fcfd0;">form</span>&gt;
&lt;/<span style="color: #8fcfd0;">body</span>&gt;
&lt;/<span style="color: #8fcfd0;">html</span>&gt;
&lt;/<span style="color: #0000ff;">style</span>&gt;
&lt;/<span style="color: #0000ff;">head</span>&gt;
&lt;<span style="color: #0000ff;">body</span>&gt;
&lt;<span style="color: #0000ff;">form</span> <span style="color: #a0522d;">method</span>=<span style="color: #8b2252;">"POST"</span>&gt;
&lt;<span style="color: #0000ff;">textarea</span> <span style="color: #a0522d;">name</span>=<span style="color: #8b2252;">"note"</span> <span style="color: #a0522d;">placeholder</span>=<span style="color: #8b2252;">"Escribe tu texto a encriptar"</span>&gt;&lt;/<span style="color: #0000ff;">textarea</span>&gt;
&lt;<span style="color: #0000ff;">fieldset</span> <span style="color: #a0522d;">class</span>=<span style="color: #8b2252;">"config"</span>&gt;
&lt;<span style="color: #0000ff;">legend</span>&gt;Configuraci&#243;n de expiraci&#243;n&lt;/<span style="color: #0000ff;">legend</span>&gt;
&lt;<span style="color: #0000ff;">label</span> <span style="color: #a0522d;">for</span>=<span style="color: #8b2252;">"max_views"</span>&gt;Visualizaciones m&#225;ximas (0 = infinitas):&lt;/<span style="color: #0000ff;">label</span>&gt;
&lt;<span style="color: #0000ff;">input</span> <span style="color: #a0522d;">name</span>=<span style="color: #8b2252;">"max_views"</span> <span style="color: #a0522d;">type</span>=<span style="color: #8b2252;">"int"</span> <span style="color: #a0522d;">value</span>=<span style="color: #8b2252;">"0"</span>&gt;
&lt;<span style="color: #0000ff;">label</span> <span style="color: #a0522d;">for</span>=<span style="color: #8b2252;">"expiration"</span>&gt;Tiempo de expiraci&#243;n (en minutos):&lt;/<span style="color: #0000ff;">label</span>&gt;
&lt;<span style="color: #0000ff;">input</span> <span style="color: #a0522d;">name</span>=<span style="color: #8b2252;">"expiration"</span> <span style="color: #a0522d;">type</span>=<span style="color: #8b2252;">"int"</span> <span style="color: #a0522d;">value</span>=<span style="color: #8b2252;">"15"</span>&gt;
&lt;/<span style="color: #0000ff;">fieldset</span>&gt;
&lt;<span style="color: #0000ff;">button</span> <span style="color: #a0522d;">type</span>=<span style="color: #8b2252;">"submit"</span>&gt;Guardar&lt;/<span style="color: #0000ff;">button</span>&gt;
&lt;/<span style="color: #0000ff;">form</span>&gt;
&lt;/<span style="color: #0000ff;">body</span>&gt;
&lt;/<span style="color: #0000ff;">html</span>&gt;
</pre>
</div>
@ -943,8 +943,8 @@ No creo que sea necesario que expliquemos un código html, si no lo entiendes, n
</div>
</div>
<div id="outline-container-org9aba6d4" class="outline-3">
<h3 id="org9aba6d4"><span class="section-number-3">3.5.</span> Mostrando el formulario</h3>
<div id="outline-container-org45e7d2c" class="outline-3">
<h3 id="org45e7d2c"><span class="section-number-3">3.5.</span> Mostrando el formulario</h3>
<div class="outline-text-3" id="text-3-5">
<p>
Ahora no vamos a hacer un archivos completos, los haremos a pedacitos de código a medida que avancemos.
@ -955,16 +955,16 @@ Comenzando con el controlador, vamos a crear un archivo llamado <code>NoteContro
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">namespace</span> <span style="color: #a9c99f;">Controllers</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">namespace</span> <span style="color: #228b22;">Controllers</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Request</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\View</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Request</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\View</span>;
<span style="color: #deb07a; font-weight: bold;">class</span> <span style="color: #a9c99f;">NoteController</span> {
<span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #deb07a; font-weight: bold;">static</span> <span style="color: #deb07a; font-weight: bold;">function</span> <span style="color: #8fcfd0;">home</span>(Request $<span style="color: #ffaacf;">request</span>): <span style="color: #a9c99f;">void</span>
<span style="color: #a020f0;">class</span> <span style="color: #228b22;">NoteController</span> {
<span style="color: #a020f0;">public</span> <span style="color: #a020f0;">static</span> <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">home</span>(Request $<span style="color: #a0522d;">request</span>): <span style="color: #228b22;">void</span>
{
<span style="color: #80aadf;">View</span>::render(<span style="color: #f3a0a0;">'CreateNote'</span>);
<span style="color: #008b8b;">View</span>::render(<span style="color: #8b2252;">'CreateNote'</span>);
}
}
</pre>
@ -991,10 +991,10 @@ Aquí me doy el lujo de hacer otro paréntesis para hablar del tipo <code>callab
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Router</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Router</span>;
<span style="color: #80aadf;">Router</span>::get(<span style="color: #f3a0a0;">'/'</span>, <span style="color: #f3a0a0;">'Controllers\NoteController::home'</span>);
<span style="color: #008b8b;">Router</span>::get(<span style="color: #8b2252;">'/'</span>, <span style="color: #8b2252;">'Controllers\NoteController::home'</span>);
</pre>
</div>
@ -1003,11 +1003,11 @@ O de esta otra manera:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Controllers\NoteController</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Router</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Controllers\NoteController</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Router</span>;
<span style="color: #80aadf;">Router</span>::get(<span style="color: #f3a0a0;">'/'</span>, [<span style="color: #80aadf;">NoteController</span>::<span style="color: #e3b0c0; font-weight: bold;">class</span>, <span style="color: #f3a0a0;">'home'</span>]);
<span style="color: #008b8b;">Router</span>::get(<span style="color: #8b2252;">'/'</span>, [<span style="color: #008b8b;">NoteController</span>::<span style="color: #483d8b;">class</span>, <span style="color: #8b2252;">'home'</span>]);
</pre>
</div>
@ -1016,8 +1016,8 @@ Ambas funcionarán y al entrar en <code>http://localhost</code> ya
</p>
</div>
<div id="outline-container-org470ae75" class="outline-4">
<h4 id="org470ae75"><span class="section-number-4">3.5.1.</span> Extra: Explicación del tipo callable</h4>
<div id="outline-container-org03756c5" class="outline-4">
<h4 id="org03756c5"><span class="section-number-4">3.5.1.</span> Extra: Explicación del tipo callable</h4>
<div class="outline-text-4" id="text-3-5-1">
<p>
Enlos anteriores 2 bloques de código lo que hemos visto han sido 2 maneras de escribir un callable:
@ -1028,13 +1028,13 @@ En el primer código he escrito el nombre completo hasta la función en un strin
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Controllers\NoteController</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Request</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Router</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Controllers\NoteController</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Request</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Router</span>;
<span style="color: #80aadf;">Router</span>::get(<span style="color: #f3a0a0;">'/'</span>, <span style="color: #deb07a; font-weight: bold;">function</span>(){});
[<span style="color: #80aadf;">NoteController</span>::<span style="color: #e3b0c0; font-weight: bold;">class</span>, <span style="color: #f3a0a0;">'home'</span>](<span style="color: #deb07a; font-weight: bold;">new</span> <span style="color: #a9c99f;">Request</span>);
<span style="color: #008b8b;">Router</span>::get(<span style="color: #8b2252;">'/'</span>, <span style="color: #a020f0;">function</span>(){});
[<span style="color: #008b8b;">NoteController</span>::<span style="color: #483d8b;">class</span>, <span style="color: #8b2252;">'home'</span>](<span style="color: #a020f0;">new</span> <span style="color: #228b22;">Request</span>);
</pre>
</div>
@ -1043,12 +1043,12 @@ O también:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #65c5a8;">&lt;?php</span>
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Request</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Router</span>;
<pre class="src src-php"><span style="color: #483d8b;">&lt;?php</span>
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Request</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Router</span>;
<span style="color: #80aadf;">Router</span>::get(<span style="color: #f3a0a0;">'/'</span>, <span style="color: #deb07a; font-weight: bold;">function</span>(){});
<span style="color: #f3a0a0;">'Controllers\NoteController::home'</span>(<span style="color: #deb07a; font-weight: bold;">new</span> <span style="color: #a9c99f;">Request</span>);
<span style="color: #008b8b;">Router</span>::get(<span style="color: #8b2252;">'/'</span>, <span style="color: #a020f0;">function</span>(){});
<span style="color: #8b2252;">'Controllers\NoteController::home'</span>(<span style="color: #a020f0;">new</span> <span style="color: #228b22;">Request</span>);
</pre>
</div>
@ -1059,34 +1059,34 @@ Sé que esto es algo raro y que realmente tiene que ver con el conocimiendo de P
</div>
</div>
<div id="outline-container-org546c136" class="outline-3">
<h3 id="org546c136"><span class="section-number-3">3.6.</span> Creando la nota encriptada</h3>
<div id="outline-container-org1704573" class="outline-3">
<h3 id="org1704573"><span class="section-number-3">3.6.</span> Creando la nota encriptada</h3>
<div class="outline-text-3" id="text-3-6">
<p>
Ahora que ya tenemos el formulario, ya solo nos queda encriptar y guardar nuestra nota. Para ello comenzaremos agregando la siguiente función a nuestro modelo:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #deb07a; font-weight: bold;">function</span> <span style="color: #8fcfd0;">encrypt</span>(): <span style="color: #a9c99f;">string</span>
<pre class="src src-php"><span style="color: #a020f0;">public</span> <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">encrypt</span>(): <span style="color: #228b22;">string</span>
{
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Generamos una llave aleatoria de entre 12 a 32 caracteres</span>
$<span style="color: #ffaacf;">secretKey</span> = substr(
str_shuffle(<span style="color: #f3a0a0;">'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'</span>),
<span style="color: #b22222;">// </span><span style="color: #b22222;">Generamos una llave aleatoria de entre 12 a 32 caracteres</span>
$<span style="color: #a0522d;">secretKey</span> = substr(
str_shuffle(<span style="color: #8b2252;">'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'</span>),
0,
mt_rand(16, 32)
);
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Encriptamos la nota en AES256 usando la llave antes generada</span>
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span> = openssl_encrypt(
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span>,
<span style="color: #f3a0a0;">'AES-256-CBC'</span>,
$<span style="color: #ffaacf;">secretKey</span>,
<span style="color: #80aadf;">OPENSSL_RAW_DATA</span>,
substr($<span style="color: #ffaacf;">secretKey</span>, 0, 16)
<span style="color: #b22222;">// </span><span style="color: #b22222;">Encriptamos la nota en AES256 usando la llave antes generada</span>
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span> = openssl_encrypt(
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span>,
<span style="color: #8b2252;">'AES-256-CBC'</span>,
$<span style="color: #a0522d;">secretKey</span>,
<span style="color: #008b8b;">OPENSSL_RAW_DATA</span>,
substr($<span style="color: #a0522d;">secretKey</span>, 0, 16)
);
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Devolvemos la llave</span>
<span style="color: #deb07a; font-weight: bold;">return</span> $<span style="color: #ffaacf;">secretKey</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Devolvemos la llave</span>
<span style="color: #a020f0;">return</span> $<span style="color: #a0522d;">secretKey</span>;
}
</pre>
</div>
@ -1100,20 +1100,20 @@ Ahora vamos a hacer un controlador que recibirá el formulario, encriptará la n
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #deb07a; font-weight: bold;">static</span> <span style="color: #deb07a; font-weight: bold;">function</span> <span style="color: #8fcfd0;">create</span>(Request $<span style="color: #ffaacf;">request</span>): <span style="color: #a9c99f;">void</span>
<pre class="src src-php"><span style="color: #a020f0;">public</span> <span style="color: #a020f0;">static</span> <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">create</span>(Request $<span style="color: #a0522d;">request</span>): <span style="color: #228b22;">void</span>
{
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Creamos una instancia de Note y le pasamos los datos del formulario</span>
$<span style="color: #ffaacf;">note</span> = <span style="color: #deb07a; font-weight: bold;">new</span> <span style="color: #a9c99f;">Note</span>;
$<span style="color: #ffaacf;">note</span>-&gt;<span style="color: #ffaacf;">content</span> = $<span style="color: #ffaacf;">request</span>-&gt;<span style="color: #ffaacf;">post</span>-&gt;<span style="color: #ffaacf;">note</span>;
$<span style="color: #ffaacf;">note</span>-&gt;<span style="color: #ffaacf;">max_views</span> = $<span style="color: #ffaacf;">request</span>-&gt;<span style="color: #ffaacf;">post</span>-&gt;<span style="color: #ffaacf;">max_views</span>;
$<span style="color: #ffaacf;">note</span>-&gt;<span style="color: #ffaacf;">expire_at</span> = strtotime(<span style="color: #f3a0a0;">"+</span><span style="color: #ffaacf;">{$request-&gt;post-&gt;expiration}</span><span style="color: #f3a0a0;"> minutes"</span>);
<span style="color: #b22222;">// </span><span style="color: #b22222;">Creamos una instancia de Note y le pasamos los datos del formulario</span>
$<span style="color: #a0522d;">note</span> = <span style="color: #a020f0;">new</span> <span style="color: #228b22;">Note</span>;
$<span style="color: #a0522d;">note</span>-&gt;<span style="color: #a0522d;">content</span> = $<span style="color: #a0522d;">request</span>-&gt;<span style="color: #a0522d;">post</span>-&gt;<span style="color: #a0522d;">note</span>;
$<span style="color: #a0522d;">note</span>-&gt;<span style="color: #a0522d;">max_views</span> = $<span style="color: #a0522d;">request</span>-&gt;<span style="color: #a0522d;">post</span>-&gt;<span style="color: #a0522d;">max_views</span>;
$<span style="color: #a0522d;">note</span>-&gt;<span style="color: #a0522d;">expire_at</span> = strtotime(<span style="color: #8b2252;">"+</span><span style="color: #a0522d;">{$request-&gt;post-&gt;expiration}</span><span style="color: #8b2252;"> minutes"</span>);
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Encriptamos los datos y luego los guardamos</span>
$<span style="color: #ffaacf;">secretKey</span> = $<span style="color: #ffaacf;">note</span>-&gt;encrypt();
$<span style="color: #ffaacf;">note</span>-&gt;save();
<span style="color: #b22222;">// </span><span style="color: #b22222;">Encriptamos los datos y luego los guardamos</span>
$<span style="color: #a0522d;">secretKey</span> = $<span style="color: #a0522d;">note</span>-&gt;encrypt();
$<span style="color: #a0522d;">note</span>-&gt;save();
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Redirigimos a la url final donde se ver&#225; la nota</span>
<span style="color: #80aadf;">Router</span>::redirect(<span style="color: #f3a0a0;">"/</span><span style="color: #ffaacf;">{$note-&gt;id}</span><span style="color: #f3a0a0;">/</span><span style="color: #ffaacf;">{$secretKey}</span><span style="color: #f3a0a0;">"</span>);
<span style="color: #b22222;">// </span><span style="color: #b22222;">Redirigimos a la url final donde se ver&#225; la nota</span>
<span style="color: #008b8b;">Router</span>::redirect(<span style="color: #8b2252;">"/</span><span style="color: #a0522d;">{$note-&gt;id}</span><span style="color: #8b2252;">/</span><span style="color: #a0522d;">{$secretKey}</span><span style="color: #8b2252;">"</span>);
}
</pre>
</div>
@ -1123,8 +1123,8 @@ Desde luego, también tenemos que añadir los uses de <code>Libs\Router</code> y
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Libs\Router</span>;
<span style="color: #deb07a; font-weight: bold;">use</span> <span style="color: #a9c99f;">Models\Note</span>;
<pre class="src src-php"><span style="color: #a020f0;">use</span> <span style="color: #228b22;">Libs\Router</span>;
<span style="color: #a020f0;">use</span> <span style="color: #228b22;">Models\Note</span>;
</pre>
</div>
@ -1137,7 +1137,7 @@ Para finalizar vamos configurar la ruta. Así que editamos el archivo <code>note
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #80aadf;">Router</span>::post(<span style="color: #f3a0a0;">'/'</span>, [<span style="color: #80aadf;">NoteController</span>::<span style="color: #e3b0c0; font-weight: bold;">class</span>, <span style="color: #f3a0a0;">'create'</span>]);
<pre class="src src-php"><span style="color: #008b8b;">Router</span>::post(<span style="color: #8b2252;">'/'</span>, [<span style="color: #008b8b;">NoteController</span>::<span style="color: #483d8b;">class</span>, <span style="color: #8b2252;">'create'</span>]);
</pre>
</div>
@ -1151,57 +1151,57 @@ Ahora ya podemos comenzar a crear nuestras notas, auque aún no las podremos ver
</div>
</div>
<div id="outline-container-orgb99cb8f" class="outline-3">
<h3 id="orgb99cb8f"><span class="section-number-3">3.7.</span> Recta final: Desencriptando y mostrando las notas</h3>
<div id="outline-container-orgc2600c6" class="outline-3">
<h3 id="orgc2600c6"><span class="section-number-3">3.7.</span> Recta final: Desencriptando y mostrando las notas</h3>
<div class="outline-text-3" id="text-3-7">
<p>
Vamos directo al grano, editaremos el modelo (<code>src/Models/Note.php</code>) y le añadiremos esta otra función para desencriptar:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #deb07a; font-weight: bold;">function</span> <span style="color: #8fcfd0;">decrypt</span>(<span style="color: #a9c99f;">string</span> $<span style="color: #ffaacf;">secretKey</span>) : <span style="color: #a9c99f;">void</span>
<pre class="src src-php"><span style="color: #a020f0;">public</span> <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">decrypt</span>(<span style="color: #228b22;">string</span> $<span style="color: #a0522d;">secretKey</span>) : <span style="color: #228b22;">void</span>
{
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Verificamos si la nota ha expirado</span>
<span style="color: #deb07a; font-weight: bold;">if</span> (<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">expire_at</span> &lt; time()) {
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span> = <span style="color: #f3a0a0;">'La nota se ha destruido por expiraci&#243;n.'</span>;
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;delete();
<span style="color: #deb07a; font-weight: bold;">return</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Verificamos si la nota ha expirado</span>
<span style="color: #a020f0;">if</span> (<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">expire_at</span> &lt; time()) {
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span> = <span style="color: #8b2252;">'La nota se ha destruido por expiraci&#243;n.'</span>;
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;delete();
<span style="color: #a020f0;">return</span>;
}
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Intentamos desencriptar.</span>
$<span style="color: #ffaacf;">content</span> = openssl_decrypt(
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span>,
<span style="color: #f3a0a0;">'AES-256-CBC'</span>,
$<span style="color: #ffaacf;">secretKey</span>,
<span style="color: #80aadf;">OPENSSL_RAW_DATA</span>,
substr($<span style="color: #ffaacf;">secretKey</span>, 0, 16)
<span style="color: #b22222;">// </span><span style="color: #b22222;">Intentamos desencriptar.</span>
$<span style="color: #a0522d;">content</span> = openssl_decrypt(
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span>,
<span style="color: #8b2252;">'AES-256-CBC'</span>,
$<span style="color: #a0522d;">secretKey</span>,
<span style="color: #008b8b;">OPENSSL_RAW_DATA</span>,
substr($<span style="color: #a0522d;">secretKey</span>, 0, 16)
);
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Verificamos si desencript&#243; correctamente</span>
<span style="color: #deb07a; font-weight: bold;">if</span> (is_string($<span style="color: #ffaacf;">content</span>)) {
<span style="color: #b22222;">// </span><span style="color: #b22222;">Verificamos si desencript&#243; correctamente</span>
<span style="color: #a020f0;">if</span> (is_string($<span style="color: #a0522d;">content</span>)) {
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Verificamos si est&#225; configurado para infinitas vistas.</span>
<span style="color: #deb07a; font-weight: bold;">if</span> (<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">max_views</span> == 0) {
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span> = $<span style="color: #ffaacf;">content</span>;
<span style="color: #deb07a; font-weight: bold;">return</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Verificamos si est&#225; configurado para infinitas vistas.</span>
<span style="color: #a020f0;">if</span> (<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">max_views</span> == 0) {
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span> = $<span style="color: #a0522d;">content</span>;
<span style="color: #a020f0;">return</span>;
}
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Reducimos el contador de vistas</span>
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">max_views</span> = <span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">max_views</span> - 1;
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;save();
<span style="color: #b22222;">// </span><span style="color: #b22222;">Reducimos el contador de vistas</span>
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">max_views</span> = <span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">max_views</span> - 1;
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;save();
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Verificamos si el contador de vistas ha llegado a 0.</span>
<span style="color: #deb07a; font-weight: bold;">if</span> (<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">max_views</span> &lt;= 0) {
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span> = <span style="color: #f3a0a0;">'La nota se ha destruido seg&#250;n su l&#237;mite de vistas.'</span>;
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;delete();
<span style="color: #deb07a; font-weight: bold;">return</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Verificamos si el contador de vistas ha llegado a 0.</span>
<span style="color: #a020f0;">if</span> (<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">max_views</span> &lt;= 0) {
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span> = <span style="color: #8b2252;">'La nota se ha destruido seg&#250;n su l&#237;mite de vistas.'</span>;
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;delete();
<span style="color: #a020f0;">return</span>;
}
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Si no ha expirado, solo devolvemos el contenido</span>
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span> = $<span style="color: #ffaacf;">content</span>;
} <span style="color: #deb07a; font-weight: bold;">else</span> {
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Si no se pudo desencriptar colocamos un error</span>
<span style="color: #80aadf;">$</span><span style="color: #80aadf;">this</span>-&gt;<span style="color: #ffaacf;">content</span> = <span style="color: #f3a0a0;">'Llave incorrecta.'</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Si no ha expirado, solo devolvemos el contenido</span>
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span> = $<span style="color: #a0522d;">content</span>;
} <span style="color: #a020f0;">else</span> {
<span style="color: #b22222;">// </span><span style="color: #b22222;">Si no se pudo desencriptar colocamos un error</span>
<span style="color: #008b8b;">$</span><span style="color: #008b8b;">this</span>-&gt;<span style="color: #a0522d;">content</span> = <span style="color: #8b2252;">'Llave incorrecta.'</span>;
}
}
@ -1213,29 +1213,29 @@ Ahora en el <code>NoteController</code> añadiremos esta otra función:
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #deb07a; font-weight: bold;">public</span> <span style="color: #deb07a; font-weight: bold;">static</span> <span style="color: #deb07a; font-weight: bold;">function</span> <span style="color: #8fcfd0;">show</span>(Request $<span style="color: #ffaacf;">request</span>): <span style="color: #a9c99f;">void</span>
<pre class="src src-php"><span style="color: #a020f0;">public</span> <span style="color: #a020f0;">static</span> <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">show</span>(Request $<span style="color: #a0522d;">request</span>): <span style="color: #228b22;">void</span>
{
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Verificamos si el ID es un n&#250;mero</span>
<span style="color: #deb07a; font-weight: bold;">if</span> (!is_numeric($<span style="color: #ffaacf;">request</span>-&gt;<span style="color: #ffaacf;">params</span>-&gt;<span style="color: #ffaacf;">id</span>)) {
<span style="color: #80aadf;">Router</span>::defaultNotFound();
<span style="color: #deb07a; font-weight: bold;">return</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Verificamos si el ID es un n&#250;mero</span>
<span style="color: #a020f0;">if</span> (!is_numeric($<span style="color: #a0522d;">request</span>-&gt;<span style="color: #a0522d;">params</span>-&gt;<span style="color: #a0522d;">id</span>)) {
<span style="color: #008b8b;">Router</span>::defaultNotFound();
<span style="color: #a020f0;">return</span>;
}
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Obtenemos la nota de la base de datos</span>
$<span style="color: #ffaacf;">note</span> = <span style="color: #80aadf;">Note</span>::getById($<span style="color: #ffaacf;">request</span>-&gt;<span style="color: #ffaacf;">params</span>-&gt;<span style="color: #ffaacf;">id</span>);
<span style="color: #b22222;">// </span><span style="color: #b22222;">Obtenemos la nota de la base de datos</span>
$<span style="color: #a0522d;">note</span> = <span style="color: #008b8b;">Note</span>::getById($<span style="color: #a0522d;">request</span>-&gt;<span style="color: #a0522d;">params</span>-&gt;<span style="color: #a0522d;">id</span>);
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Verificamos si la nota existe</span>
<span style="color: #deb07a; font-weight: bold;">if</span> (is_null($<span style="color: #ffaacf;">note</span>)) {
<span style="color: #80aadf;">Router</span>::defaultNotFound();
<span style="color: #deb07a; font-weight: bold;">return</span>;
<span style="color: #b22222;">// </span><span style="color: #b22222;">Verificamos si la nota existe</span>
<span style="color: #a020f0;">if</span> (is_null($<span style="color: #a0522d;">note</span>)) {
<span style="color: #008b8b;">Router</span>::defaultNotFound();
<span style="color: #a020f0;">return</span>;
}
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Desencriptamos la nota</span>
$<span style="color: #ffaacf;">note</span>-&gt;decrypt($<span style="color: #ffaacf;">request</span>-&gt;<span style="color: #ffaacf;">params</span>-&gt;<span style="color: #ffaacf;">key</span>);
<span style="color: #b22222;">// </span><span style="color: #b22222;">Desencriptamos la nota</span>
$<span style="color: #a0522d;">note</span>-&gt;decrypt($<span style="color: #a0522d;">request</span>-&gt;<span style="color: #a0522d;">params</span>-&gt;<span style="color: #a0522d;">key</span>);
<span style="color: #a0a0cf; font-style: italic;">// </span><span style="color: #a0a0cf; font-style: italic;">Imprimimos en formato de texto plano el resultado del desencriptado</span>
$<span style="color: #ffaacf;">view</span> = <span style="color: #deb07a; font-weight: bold;">new</span> <span style="color: #a9c99f;">View</span>;
$<span style="color: #ffaacf;">view</span>-&gt;text($<span style="color: #ffaacf;">note</span>-&gt;<span style="color: #ffaacf;">content</span>);
<span style="color: #b22222;">// </span><span style="color: #b22222;">Imprimimos en formato de texto plano el resultado del desencriptado</span>
$<span style="color: #a0522d;">view</span> = <span style="color: #a020f0;">new</span> <span style="color: #228b22;">View</span>;
$<span style="color: #a0522d;">view</span>-&gt;text($<span style="color: #a0522d;">note</span>-&gt;<span style="color: #a0522d;">content</span>);
}
</pre>
</div>
@ -1257,7 +1257,7 @@ Ahora la cerecita del paste: Vamos a configurar el router, así que editamos nue
</p>
<div class="org-src-container">
<pre class="src src-php"><span style="color: #80aadf;">Router</span>::get(<span style="color: #f3a0a0;">'/{id}/{key}'</span>, [<span style="color: #80aadf;">NoteController</span>::<span style="color: #e3b0c0; font-weight: bold;">class</span>, <span style="color: #f3a0a0;">'show'</span>]);
<pre class="src src-php"><span style="color: #008b8b;">Router</span>::get(<span style="color: #8b2252;">'/{id}/{key}'</span>, [<span style="color: #008b8b;">NoteController</span>::<span style="color: #483d8b;">class</span>, <span style="color: #8b2252;">'show'</span>]);
</pre>
</div>
@ -1291,8 +1291,8 @@ Crea una aplicación de gestión tareas (un TODO list), en donde puedas agregar
</div>
</div>
<div id="outline-container-orgaddaa69" class="outline-3">
<h3 id="orgaddaa69"><span class="section-number-3">3.8.</span> Repositorio</h3>
<div id="outline-container-orgb6a2dc7" class="outline-3">
<h3 id="orgb6a2dc7"><span class="section-number-3">3.8.</span> Repositorio</h3>
<div class="outline-text-3" id="text-3-8">
<p>
Por si alguien se perdió o sencillamente quiere ver código, he subido la aplicación ignota en este repositorio:
@ -1309,8 +1309,8 @@ Te animo a hacerle un fork, mejorarlo y enviármelo. Los forks que me parezcan i
</div>
</div>
<div id="outline-container-org21d2ee2" class="outline-2">
<h2 id="org21d2ee2"><span class="section-number-2">4.</span> Contacto</h2>
<div id="outline-container-org792cd68" class="outline-2">
<h2 id="org792cd68"><span class="section-number-2">4.</span> Contacto</h2>
<div class="outline-text-2" id="text-4">
<p>
Como siempre, pueden contactarme mediante el correo <code>webmaster@outcontol.net</code>
@ -1328,7 +1328,7 @@ No es una comunidad de programación solamente, pero ahora mismo no existe una d
</div>
<div id="postamble" class="status">
<p class="author">Author: KJ</p>
<p class="date">Created: 2024-05-13 lun 01:52</p>
<p class="date">Created: 2024-05-13 lun 02:02</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>