Category Archives: PHP

React vs next JS

React.js

  • A library for building user interfaces.
  • Great for SPAs (Single Page Applications) where client-side
  • rendering (CSR) is enough.
  • Requires additional setup for routing, state management, and API
  • handling (e.g., using React Router, Redux, etc.).
  • More flexibility but requires manual configuration for things like SSR, SEO, and performance optimizations.

Next.js

  • A framework built on top of React.
  • Provides SSR (Server-Side Rendering), SSG (Static Site
  • Generation), and ISR (Incremental Static Regeneration) for better SEO and performance.
  • Comes with built-in routing (file-based), API routes, and full-stack capabilities.
  • Ideal for fast-loading, SEO-friendly websites, such as e-commerce platforms, blogs, and dashboards.

Difference Between composer.json and composer.lock Files

The composer.json and composer.lock files are both essential in managing dependencies in PHP projects using Composer, but they serve different purposes:

1. composer.json
Purpose: This is the main configuration file where you define your project’s dependencies, scripts, and other metadata.

Content: It includes the list of required packages, version constraints, and other settings like autoload configurations, scripts, and repositories.

Usage: When you run composer install, Composer reads this file to determine which packages and versions to install.

2. composer.lock
Purpose: This file is automatically generated by Composer and records the exact versions of the dependencies installed in your project.

Content: It contains detailed information about each package, including the exact version, source, and dependencies.

Usage: When you run composer install, Composer uses this file to install the exact versions of the dependencies specified, ensuring consistency across installations.

Editable: This file should not be manually edited. It is updated automatically when you run composer update or when you install new dependencies.

Key Differences:
Control: composer.json is manually controlled by the developer, while composer.lock is automatically managed by Composer.

Versioning: composer.json specifies version constraints (e.g., ^2.0), whereas composer.lock records the exact versions installed.

Consistency: composer.lock ensures that all installations of the project use the same versions of dependencies, providing consistency across different environments.

Best Practices:
Development: Use composer.json to define your dependencies and version constraints.

Deployment: Always commit composer.lock to your version control system (e.g., Git) to ensure that all team members and deployment environments use the same dependency versions.

Updates: Run composer update to update dependencies and generate a new composer.lock file when you want to upgrade to newer versions within the constraints defined in composer.json.

By understanding and using both files correctly, you can effectively manage your PHP project’s dependencies and ensure a consistent development and deployment environment.

 

YAML

YAML stands for “Yet Another Markup Language”. It’s a strict superset of JSON, which means that YAML can do everything JSON can and more.

  • Configuration files: YAML is a popular choice for creating configuration files because it’s human-readable and works with any programming language. YAML files are often used in Kubernetes to configure pods, services, and deployments.
  • Data serialization: YAML is a data serialization language that can be used to convert data to and from other formats, such as JSON. YAML is a good choice when human readability is important.
  • Documentation: YAML’s human-readable format makes it a good choice for documentation.
  • Log files: YAML can be used to create clean, intuitive log files.
  • API and web services: YAML can be used to write API definitions for popular API specifications, such as OpenAPI and AsyncAPI.
  • Metadata: YAML can be used to store and organize metadata and content structure.
  • Machine learning: YAML can be used to define and orchestrate the steps involved in the machine learning lifecycle, such as data ingestion, model training, and evaluation.

Install Elasticsearch

Import the Elasticsearch GPG key:
# wget -qO – https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg –dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

Add the Elasticsearch repository:
# echo “deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main” | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

Update the apt package manager and install Elasticsearch:
# apt update && apt install elasticsearch

Start and enable the Elasticsearch service:
# systemctl start elasticsearch
# systemctl enable elasticsearch

Open the elasticsearch.yml file for editing:

sudo nano /etc/elasticsearch/elasticsearch.yml

Replace the following setting with ‘false’ to disable Magento security features:
# Enable security features
xpack.security.enabled: false
Save the changes to the elasticsearch.yml file.

Restart the Elasticsearch service to apply the configuration:

# systemctl restart elasticsearch.service

Verify that Elasticsearch runs correctly using the curl command:

# curl -X GET “localhost:9200/”
If Elasticsearch is working correctly, you’ll receive an output like this:
{
“name” : “ubuntu”,
“cluster_name” : “elasticsearch”,
“cluster_uuid” : “KPbFKCVLT9uu-RFxzxH_Bw”,
“version” : {
“number” : “8.6.2”,
“build_flavor” : “default”,
“build_type” : “deb”,
“build_hash” : “2d58d0f136141f03239816a4e360a8d17b6d8f29”,
“build_date” : “2023-02-13T09:35:20.314882762Z”,
“build_snapshot” : false,
“lucene_version” : “9.4.2”,
“minimum_wire_compatibility_version” : “7.17.0”,
“minimum_index_compatibility_version” : “7.0.0”
},
“tagline” : “You Know, for Search”
}

Data fixture

A data fixture is a PHP script that sets data you want to reuse in your test. The script can be defined in a separate file or as a local test case method.

Use data fixtures to prepare a database for tests. The Integration Testing Framework (ITF) reverts the database to its initial state automatically. To set up a date fixture, use the @magentoDataFixture annotation.

Constructor Property Promotion

Constructor Property Promotion is a simple shorthand syntax to declare and assign class properties from the constructor. It basically tells the way data has to be treated in code. Constructor Property Promotion is a new syntax provided in the newer version of PHP 8 that allows class property declaration and constructor assignment, variable assignment right from the constructor without getting in the condition of boilerplate code. This avoids having to type the class property name and property type from many to just once. It also avoids using repeated types. Promoted properties can only be used in constructors. Properties will be assigned before the rest of the constructor code is executed.