Introducci贸n a desarrollo web con PHP y Yii
驴Qu茅 es Yii2?
驴Por qu茅 usar PHP y Yii2?
Arquitectura modelo鈥搗ista鈥揷ontrolador
Instalaci贸n de Yii2
Estructura de una aplicaci贸n con Yii2
Configuraci贸n de variables de entorno
Quiz: Introducci贸n a desarrollo web con PHP y Yii
Esquema de la aplicaci贸n
Proyecto: The Book Club
Creaci贸n del schema de la base de datos
Creaci贸n de tablas clubs y club_members
Quiz: Esquema de la aplicaci贸n
Modelos, vistas y controladores
Commands
Model
ActiveRecord
ActiveRecord de la clase author
Relational data: hasOne()
Relational data: hasMany()
Web Controllers
Controller de la clase author
Enrutamiento y creaci贸n de URLS con urlManager
Views
Configuraci贸n de vistas con Smarty
Quiz: Modelos, vistas y controladores
Manejo de usuarios con Yii2
Clase user
Autenticaci贸n de usuarios
Autenticaci贸n de usuarios: LoginForm
User input
Formulario de login con ActiveForm
Validaci贸n de modelo con core validators
Core validators: unique, filter, email
Quiz: Manejo de usuarios con Yii2
Enlaces, navegaci贸n y tablas intermedias
Dar de alta un libro
Asignaci贸n de libros a usuario
Asignaci贸n de libros a usuario: vista
Calificar un libro
Promedio de calificaciones de libro
Relaciones entre tablas intermedias con viaTable()
Enlaces y navegaci贸n
Quiz: Enlaces, navegaci贸n y tablas intermedias
Despliegue y cierre del proyecto
Despliegue de aplicaci贸n de Yii2 a Heroku
You don't have access to this class
Keep learning! Join and start boosting your career
A model is fundamental in programming, especially when dealing with web applications and databases. It is described as a package of data transport, business rules, business logic and validation. It is considered the heart of an application, as it centralizes all logic and handles interaction with other system components.
Models allow us to encapsulate data in objects, each object being a direct representation of a structure that we manage within our business logic. This facilitates the handling and processing of data in a systematic and organized way.
The integration of models with controllers is crucial to the flow of a web application. In a programming environment such as PHP, models are commonly stored in a special folder, indicated for their specific handling. By creating a model, such as a Book.php
book, a base structure is established for specific data handling.
A simple example using PHP is shown below:
<?php
namespace app\models;
use yiibaseModel;
class Book extends Model{ public $title; public $author;}
This code defines a book model with two attributes: title
and author
. However, such a model does not directly connect or store data in databases or other resources; its role is purely organizational to manage data in the application.
When integrating models into the logic of an application, it is important to understand how they are instantiated and used:
Model instantiation: creating an instance of a model allows you to handle specific data about that object, e.g.:
$book = new Book();$book->title = '1984';$book->author = 'George Orwell';
Integration with controllers: controllers, by handling the application's action logic, such as data manipulation and display, interact closely with models.
Example controller:
use appmodels;
$book = new Book();$book->title = $data[0];$book->author = $data[2];
echo $book->toString();
In the above code, toString()
prints a representation of the Book
object, clearly separating the data logic from the presentation, promoting a clean and efficient design.
When implementing models, it is essential to follow certain practices that improve code maintenance and understanding:
Concise Names: naming models in the singular, such as "Book" instead of "Books", helps instantiated objects individually represent a specific entity.
Namespaces: Using namespaces is crucial to avoid conflicts and better organize classes in an application. This helps to modularize and maintain the code.
Validation and Logic: Models not only carry data, but can also include validation logic, managing both input and output information.
Private Functions: Allows encapsulation of specific auxiliary logic at the model level without exposing it out of context. For example, manipulating titles before displaying them:
private function quick(Book $book){ $book->title = sprintf('%s %s', $book->title, 'Extra Info'); return $book;}
Models are a key piece in structuring a robust and efficient application. Their correct implementation not only organizes the code, but also significantly improves maintainability and data flow through the different layers of our application. With time, understanding and continued practice, these techniques are refined, enabling the development of advanced and efficient applications. Continue to explore and hone your programming skills!
Contributions 3
Questions 0
Want to see more contributions, questions and answers from the community?