Guía práctica para desplegar una aplicación de Laravel en Cloudways utilizando despliegue vía Git, configuración del servidor y manejo de entorno de producción.
Antes de comenzar asegúrate de contar con:
| -- Creación de base de datos | |
| CREATE DATABASE products_api | |
| CHARACTER SET utf8mb4 | |
| COLLATE utf8mb4_unicode_ci; | |
| -- Usar nuestro base de datos products_api | |
| USE products_api; | |
| -- Tabla para productos | |
| CREATE TABLE products ( | |
| id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
| name VARCHAR(150) NOT NULL, |
| <?php | |
| function nextId(array $products): int | |
| { | |
| $maxId = 0; | |
| foreach ($products as $product) { | |
| $maxId = max($maxId, (int)($product["id"] ?? 0)); | |
| } | |
| return $maxId + 1; | |
| } |
| RewriteEngine On | |
| # Si la petición es a un archivo o directorio que existe lo mostramos directamente | |
| RewriteCond %{REQUEST_FILENAME} -f [OR] | |
| RewriteCond %{REQUEST_FILENAME} -d | |
| RewriteRule ^ - [L] | |
| # Realizamos el enrutamiento para todas las peticiones | |
| RewriteRule ^ Index.php [L,QSA] |
| <?php | |
| function storagePath(): string | |
| { | |
| return __DIR__ . "/storage_products.json"; | |
| } | |
| function loadProducts(): array | |
| { | |
| $path = storagePath(); | |
| $seed = [ |
| RewriteEngine On | |
| RewriteCond %{REQUEST_FILENAME} !-f | |
| RewriteCond %{REQUEST_FILENAME} !-d | |
| RewriteRule ^(.*)$ Index.php [QSA,L] |
| <?php | |
| declare(strict_types=1); | |
| class Customer | |
| { | |
| public function __construct( | |
| public string $name, | |
| public string $email | |
| ) {} | |
| } |
| <?php | |
| declare(strict_types=1); | |
| require_once __DIR__ . '/src/ProcessOrder.php'; | |
| $cart = [ | |
| ["name" => "Mouse", "price" => 100, "quantity" => 2], | |
| ["name" => "Teclado", "price" => 150, "quantity" => 1], | |
| ["name" => "Producto roto"], // sin price/quantity -> salta |
| <?php | |
| declare(strict_types=1); | |
| $users = [ | |
| ["name" => "Teddy", "role" => "admin", "age" => 28], | |
| ["name" => "Edgar", "role" => "user", "age" => 16], | |
| ["name" => "Devi", "role" => "editor", "age" => 22], | |
| ["name" => "Maria", "role" => "user", "age" => 35], | |
| ]; |