Blog
- Details
- Written by R. Elizondo
- Category: Databases
To install Elasticsearch on Ubuntu 22, you can follow these steps:
Update System Packages
Open a terminal and run the following commands to update the system packages to their latest versions
sudo apt update
sudo apt upgrade
Install Java Development Kit (JDK)
Elasticsearch requires Java to run. Install OpenJDK using the following command
sudo apt install openjdk-11-jdk
Import the Elasticsearch GPG Key
Run the command below to import the Elasticsearch GPG key
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-archive-keyring.gpg
- Details
- Written by R. Elizondo
- Category: Symfony Framework
One of the most powerful features of Symfony Framework is its Event Driven Architecture. It is very easy and simple to implement PHP logic that responds to certain events during the request - response cycle.
Events related to the Symfony Kernel
Kernel Request Event
use Symfony\Component\HttpKernel\Event\RequestEvent;
class MyRequestListener
{
public function onKernelRequest(RequestEvent $event)
{
// Perform logic before handling the request
// Example: Set a custom request attribute
$request = $event->getRequest();
$request->attributes->set('custom_attribute', 'some_value');
}
}
Kernel View Event
use Symfony\Component\HttpKernel\Event\ViewEvent;
class MyViewListener
{
public function onKernelView(ViewEvent $event)
{
// Perform logic after the controller has been executed
// Example: Modify the response
$response = $event->getResponse();
$response->setContent('Modified content');
}
}
Read more: How to implement Actions that respond to Events in PHP Symfony
- Details
- Written by R. Elizondo
- Category: PHP Software Development
In PHP, a DTO (Data Transfer Object) is a design pattern that is used to transfer data between different layers or components of an application. It acts as a container or data structure that holds the data.The purpose of using DTOs is to encapsulate and transport data in a structured manner, independent of the underlying implementation details. They are commonly used in scenarios where you need to transfer data across different layers of an application, such as between the database layer, business logic layer, and presentation layer.
Here are some key characteristics and benefits of using DTOs in PHP
- Data Encapsulation: DTOs encapsulate related data fields into a single object. This helps in organizing the data and making it easier to manage and pass around.
- Abstraction: DTOs provide an abstraction layer that separates the internal implementation details from the external components. They define a well-defined contract for accessing and manipulating the data, hiding the complexities of the underlying data structures or data sources.
- Data Integrity: By using DTOs, you can enforce data integrity rules and validations. For example, you can have validation logic in the DTO's setter methods to ensure that only valid data is assigned to the properties.
- Data Transformation: DTOs can be used to transform and normalize data from different sources into a standardized format. For example, you can convert database records into DTO objects, or map JSON/XML data into DTOs.
- Security: DTOs can be used to filter or sanitize data before it is exposed to external components. This helps in preventing security vulnerabilities, such as SQL injection or cross-site scripting attacks, by ensuring that only the required data is transferred.
- Versioning: DTOs can aid in versioning and backward compatibility. As the application evolves and new versions are released, you can introduce changes to the DTOs while maintaining backward compatibility by including both old and new properties in the DTOs.
Overall, DTOs provide a structured and controlled way to transfer data between different parts of an application. They promote better separation of concerns and help in managing data integrity, security, and versioning. By using DTOs, you can improve the maintainability, flexibility, and reliability of your PHP applications.
Transform a JSON object into a DTO (Data Transfer Object) in PHP
Let's say you have the following JSON object
{
"id": 1,
"name": "John Doe",
"email": "This email address is being protected from spambots. You need JavaScript enabled to view it."
}
- Details
- Written by R. Elizondo
- Category: Symfony Framework
Monolog is a great package available as a Symfony component. It is very powerful yet very easy to setup and use. You can log almost everything to everywhere. Configuring Monolog in Symfony involves setting up log channels, handlers, and formatters.
Basic Configuration
In your config/packages/dev/monolog.yaml
file, you can define a basic configuration for Monolog
monolog:
channels: ['app']
handlers:
app:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
This configuration creates a log channel named app and configures a stream handler that writes logs to a file. The log file is stored in the logs directory with the name corresponding to the current environment.
Customizing Log Formats
You can customize the log format by defining a formatter
for your handler. For example, to include the log level, date, and message in the log output, modify the configuration as follows
monolog:
handlers:
app:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
formatter: 'Monolog\Formatter\LineFormatter'
formatter_options:
format: "%datetime% [%level_name%] %message%\n"
Here, we set the formatter
option to Monolog\Formatter\LineFormatter
and provide the format option to specify the desired log format.
Read more: How to configure Monolog in Symfony for effective logging
Page 31 of 42