Curso de Programación Web con PHP y Symfony

Miryam Godoy

CODELAB

 

24/08/2024

 

https://meet.google.com/psd-mrvz-gzb

Para ver mejor las presentaciones y la pantalla de ejercicios

hay un link generado para pode acceder al google meet:

https://meet.google.com/psd-mrvz-gzb

Menú de hoy

  • Model-View-Controller (MVC)
  • ORM (Doctrine)
  • Calabaza calabaza...

Model-View-Controller (MVC)

 Una de las arquitecturas más utilizadas es el Modelo Vista Controlador (MVC), el cual se enfoca en separar las distintas capas de la aplicación para mejorar su mantenimiento y escalabilidad.

M

V

C

Model-View-Controller (MVC)

M

V

C

Es el patron de diseño de sfotware mas utilizado en el desarrollo web 

Model-View-Controller (MVC)

M

Storage and manages data

Model-View-Controller (MVC)

M

V

Storage and manages data

Displays data and handles user interactions

Model-View-Controller (MVC)

M

C

V

Storage and manages data

Displays data and handles user interactions

Connects Model and View

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model-View-Controller (MVC)

Model

Model-View-Controller (MVC)

Model

Controller

Model-View-Controller (MVC)

Model

Controller

View

Model-View-Controller (MVC)

// src/Entity/Product.php
namespace App\Entity;

use App\Repository\ProductRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ProductRepository::class)]
class Product
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

    #[ORM\Column]
    private ?int $price = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    // ... getter and setter methods
}

Model

Model-View-Controller (MVC)

// src/Controller/ProductController.php
namespace App\Controller;

use App\Entity\Product;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ProductController extends AbstractController
{
    #[Route('/product', name: 'create_product')]
    public function createProduct(ManagerRegistry $doctrine): Response
    {
        $entityManager = $doctrine->getManager();

        $product = new Product();
        $product->setName('Keyboard');
        $product->setPrice(1999);
        $product->setDescription('Ergonomic and stylish!');

        // tell Doctrine you want to (eventually) save the Product (no queries yet)
        $entityManager->persist($product);

        // actually executes the queries (i.e. the INSERT query)
        $entityManager->flush();

        return new Response('Saved new product with id '.$product->getId());
    }
}

Controller

Model-View-Controller (MVC)

{# templates/base.html.twig #}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}My Application{% endblock %}</title>
        {% block stylesheets %}
            <link rel="stylesheet" type="text/css" href="/css/base.css"/>
        {% endblock %}
    </head>
    <body>
        {% block body %}
            <div id="sidebar">
                {% block sidebar %}
                    <ul>
                        <li><a href="{{ path('homepage') }}">Home</a></li>
                        <li><a href="{{ path('blog_index') }}">Blog</a></li>
                    </ul>
                {% endblock %}
            </div>

            <div id="content">
                {% block content %}{% endblock %}
            </div>
        {% endblock %}
    </body>
</html>

View

ORM Doctrine

ORM son las siglas de Object-Relational Mapping, es decir, el mapeo relacional de objetos. Esto significa que va a trasladar los datos de una base de datos relacional, como puede ser MySQL o Postgres, a un sistema de clases y de objetos, donde las clases serían las tablas y los registros pasarían a ser lo equivalente a objetos.

Symfony

Veamos un ejemplo

Curso de PHP (Clase 5)

By Pedro Flores

Curso de PHP (Clase 5)

  • 86