dataDesk BLOG

Welcome to the blog of Mr.G


Blog 51: php_project_structure | (Open) | (Share)

▲ Previous | Next ▼

/Dev/HowTo

Last modified: December 21 2024 01:37:55

php project structure

When you're working on a PHP-focused project, the structure can differ from a typical front-end-focused project (like one with JavaScript, SCSS, and HTML). PHP-based projects are often server-side applications, and their folder structure depends on the frameworks, deployment strategy, and how the application is built.

Here's a breakdown of how a PHP-based project differs and a recommended structure:


Key Differences in PHP Projects

  1. PHP is Server-Side:

    • Your code runs on the server, and the client receives the output (usually HTML, CSS, and JavaScript).
    • There's often no need to distinguish between src and dist, as PHP files are not "built" or "compiled"; they are interpreted directly by the server.
  2. Focus on Backend Logic:

    • The project revolves around PHP scripts, templates, and logic for dynamic rendering.
    • Static assets like CSS, JavaScript, and images are still present but serve the front-end.
  3. Deployment is Different:

    • PHP projects are typically deployed by copying the entire folder to a server or uploading files via FTP or version control.
    • There’s usually no "build" process (unless you're also using modern tools for static assets like JavaScript or SCSS).

Recommended Folder Structure for PHP Projects

Here’s a standard folder structure for a PHP project:

project
├── public                 // Publicly accessible files
│   ├── index.php          // Entry point (e.g., front controller)
│   ├── css
│   │   └── styles.css     // CSS files
│   ├── js
│   │   └── script.js      // JavaScript files
│   ├── images
│   │   └── logo.png       // Images accessible by the browser
│   └── uploads            // User-uploaded files (if applicable)
├── src                    // Application source code (non-public)
│   ├── controllers        // PHP controllers
│   │   └── HomeController.php
│   ├── models             // Database models
│   │   └── UserModel.php
│   ├── views              // HTML templates or views
│   │   └── home.php
│   └── helpers            // Utility/helper PHP functions
│       └── Utils.php
├── config                 // Configuration files
│   ├── database.php       // Database connection settings
│   └── app.php            // Application settings
├── vendor                 // Composer dependencies (if using Composer)
├── storage                // Temporary files (e.g., logs, cache)
│   ├── cache
│   ├── logs
│   └── sessions
├── tests                  // Test scripts (optional)
│   ├── Unit
│   └── Integration
├── .env                   // Environment variables (e.g., database credentials)
├── composer.json          // Composer dependency configuration
├── README.md              // Documentation
└── .htaccess              // Apache configuration (if applicable)

Explanation of Each Folder

1. public/

  • This is the only folder accessible by the browser.
  • It contains:
    • index.php: The entry point for the application. This is often referred to as the "front controller" in frameworks like Laravel.
    • Static files: CSS, JS, images, and fonts.
    • Uploads: A folder for user-uploaded files (e.g., images, documents). Ensure this folder is secure.

2. src/

  • Contains all the core PHP logic for your application.
  • This is not publicly accessible for security reasons.
  • Subfolders typically include:
    • controllers/: Handles incoming requests and prepares data for views.
    • models/: Manages the data and database interactions.
    • views/: Contains HTML templates mixed with PHP logic (e.g., home.php).
    • helpers/: Stores reusable utility functions (e.g., date formatters, string helpers).

3. config/

  • Stores configuration files (e.g., database connection details, app settings).
  • Sensitive information like database credentials should be in a .env file or similar.


Blog 52: Git commands.md | (Open) | (Share)

▲ Previous | Next ▼

/Dev/HowTo/Git

Last modified: November 12 2024 18:24:39

Basic Git commands

This guide outlines essential Git commands for setup, project initiation, staging, committing, branching, merging, and more.

Initial configuration involves setting a global username and email. Project setup can be achieved by creating a repository or cloning an existing one. Staging files and committing changes are crucial steps, with options to amend commits or stage all changes.

Branch management includes creating, switching, and deleting branches, while merging and rebasing ensure smooth integration of changes. Command sets for viewing status, inspecting history, and undoing changes are also provided.

Advanced commands cover sensitive data removal, squashing commits, modifying messages, managing merge conflicts, and recovering lost commits. Additionally, the guide addresses handling submodules, multiple remotes, and repository cleanup, promoting safe practices while using force-push and stash workflows.

1. Setup and Configuration

# Set a global username
git config --global user.name "Your Name"

# Set a global email
git config --global user.email "you@example.com"

# Display all Git configuration settings
git config --list

2. Starting a Project

# Initialize a new Git repository
git init

# Clone an existing repository from a URL
git clone <url>

3. Staging and Committing Changes

# Stage a specific file for commit
git add <file>

# Stage all changes in the current directory
git add .

# Commit changes with a message
git commit -m "message"

# Modify the last commit message or add more changes to it
git commit --amend

4. Branching

# List all branches
git branch

# Create a new branch
git branch <branch_name>

# Switch to a branch
git checkout <branch_name>

# Create and switch to a new branch
git checkout -b <branch_name>

# Delete a branch locally
git branch -d <branch_name>

5. Merging and Rebasing

# Merge a branch into the current branch
git merge <branch_name>

# Reapply commits on top of another branch
git rebase <branch_name>

# Abort a merge in progress
git merge --abort

# Abort a rebase in progress
git rebase --abort

6. Viewing and Inspecting

# Show the working directory and staging area status
git status

# View commit history
git log

# View a concise, graphical commit history
git log --oneline --graph

# Show details of a specific commit
git show <commit_hash>

# Show changes in the working directory (not staged)
git diff

# Show changes that are staged
git diff --staged

7. Undoing Changes

# Unstage a file
git reset <file>

# Discard all working directory changes
git reset --hard

# Move HEAD to a previous commit, keeping staged changes
git reset --soft <commit>

# Discard changes to a file
git checkout -- <file>

8. Remote Repositories

# Add a new remote repository
git remote add <name> <url>

# List remote repositories
git remote -v

# Push changes to a remote repository
git push <remote> <branch>

# Fetch and merge changes from a remote repository
git pull <remote> <branch>

# Fetch updates from a remote without merging
git fetch <remote>

9. Tagging

# Create a tag
git tag <tag_name>

# Create an annotated tag with a message
git tag -a <tag_name> -m "message"

# Push a tag to a remote
git push <remote> <tag_name>

10. Stashing

# Save changes to a stash
git stash

# Reapply the most recent stash and remove it from the stash list
git stash pop

# Reapply a stash without removing it
git stash apply

# List all stashes
git stash list

# Delete a specific stash
git stash drop

11. Advanced Commands

# Apply a specific commit from another branch
git cherry-pick <commit_hash>

# Binary search to find the commit that introduced a bug
git bisect

# Show a log of all references (HEAD changes)
git reflog

# Show who changed each line in a file
git blame <file>

12. Cleaning Up

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd


Blog 53: Gc2VBdwXMAAudpx | (Open) | (Share)

▲ Previous | Next ▼

/Quotes

(Last modified: November 22 2024 03:45:25)
Gc2VBdwXMAAudpx
Image
Shared by © dataDesk



Blog 54: sysbench | (Open) | (Share)

▲ Previous | Next ▼

/HOWTOS

(Last modified: March 28 2024 02:54:23)

sudo apt-get install sysbench


A good tool to test Ubuntu server performance is called "Sysbench." Sysbench is a versatile benchmarking tool that allows you to assess various aspects of system performance, including CPU, memory, disk I/O, and database performance.

Here's how you can install and use Sysbench on your Ubuntu server:

Installation:

You can install Sysbench using the following command in the terminal:


sudo apt-get install sysbench
Usage:
Once installed, you can use Sysbench to perform various types of benchmarks. Here are some common examples:

CPU Benchmark:
sysbench cpu --threads=2 run

Memory Benchmark:
sysbench memory --memory-block-size=1K --memory-total-size=10G --threads=1 run

File I/O Benchmark:
sysbench fileio --file-total-size=10G --file-test-mode=rndrw prepare
sysbench fileio --file-total-size=10G --file-test-mode=rndrw --threads=16 --time=300 --max-requests=0 run
sysbench fileio --file-total-size=10G --file-test-mode=rndrw cleanup

Database Benchmark (MySQL):
sysbench oltp_read_write --mysql-host=localhost --mysql-user=root --mysql-password=pass



Blog 55: groupBy _vs_reduce | (Open) | (Share)

▲ Previous

/Dev

(Last modified: December 17 2024 02:42:02)

Object.groupBy Map.groupBy vs reduce


<script>

/* 2. Grouping Orders */
/* Orders Array */
const orders = [
    { countryCode: 'US', name: 'John', product: 'stickers' },
    { countryCode: 'CA', name: 'Sarah', product: 'shirt' },
    { countryCode: 'MX', name: 'Carlos', product: 'skateboard' },
    { countryCode: 'FR', name: 'Marie', product: 'hoodie' },
    { countryCode: 'JP', name: 'Yuki', product: 'basketball' },
    { countryCode: 'US', name: 'Emily', product: 'stickers' },
    { countryCode: 'US', name: 'Scott', product: 'shirt' },
    { countryCode: 'CA', name: 'David', product: 'shirt' },
    { countryCode: 'MX', name: 'Juan', product: 'skateboard' }
];

/* Using reduce to Group Orders */
const ordersByCountryR = orders.reduce((acc, order) => {
    const { countryCode } = order;
    if (!acc[countryCode]) {
        acc[countryCode] = [];
    }
    acc[countryCode].push(order);
    return acc;
}, {});

console.log(ordersByCountryR);

/* Group Orders by Product */
const { shirt, stickers } = Object.groupBy(orders, (order) => order.product);

console.log("shirt\n", shirt);

/* Ouput */

// [
//     {
//         "countryCode": "CA",
//         "name": "Sarah",
//         "product": "shirt"
//     },
//     {
//         "countryCode": "US",
//         "name": "Scott",
//         "product": "shirt"
//     },
//     {
//         "countryCode": "CA",
//         "name": "David",
//         "product": "shirt"
//     }
// ]

console.log("stickers\n", stickers);

/* Group Orders by Country Code */
const ordersByCountry = Object.groupBy(orders, (order) => order.countryCode);

console.log("ordersByCountry\n", ordersByCountry);

/* Grouped Orders by Country Code: */

/* {
"US": [
{ countryCode: "US", name: "John", product: "stickers" },
{ countryCode: "US", name: "Emily", product: "stickers" },
{ countryCode: "US", name: "Scott", product: "shirt" }
],
"CA": [
{ countryCode: "CA", name: "Sarah", product: "shirt" },
{ countryCode: "CA", name: "David", product: "shirt" }
],
"MX": [
{ countryCode: "MX", name: "Carlos", product: "skateboard" },
{ countryCode: "MX", name: "Juan", product: "skateboard" }
],
"FR": [{ countryCode: "FR", name: "Marie", product: "hoodie" }],
"JP": [{ countryCode: "JP", name: "Yuki", product: "basketball" }]
}
 */

/* 1. Grouping Topics and Utterances
Topics Array */
const topics = [
    { topic: 'Topic 1', start: 0 },
    { topic: 'Topic 2', start: 12 },
    { topic: 'Topic 3', start: 24 },
];
/* Utterances Array */
const utterances = [
    { text: 'Hello Welcome to the show', start: 0, end: 3 },
    { text: 'JavaScript popular for web dev', start: 4, end: 7 },
    { text: 'Variables declared with var, let', start: 8, end: 11 },
    { text: 'Prototype inheritance in JavaScript', start: 12, end: 15 },
    { text: 'DOM API manipulates HTML', start: 16, end: 19 },
    { text: 'Callbacks used for async code', start: 20, end: 23 },
    { text: 'Runs client-side and server-side', start: 24, end: 27 },
    { text: 'React and Vue popular frameworks', start: 28, end: 31 },
    { text: 'Node.js runs JavaScript outside browser', start: 32, end: 35 }
 ];

 /*
Group Utterances into Topics */
const groupedTopics = Map.groupBy(utterances, (utterance) =>
    topics.find((topic, i) => {
        const { start } = topic;
        const end = topics.at(i + 1)?.start ?? Infinity;
        return utterance.start >= start && utterance.end <= end;
    })
);

console.log(groupedTopics);

 /* Output */

/* Map(3) {
"Topic 1" => [
{ text: "Hello Welcome to the show", start: 0, end: 3 },
{ text: "JavaScript popular for web dev", start: 4, end: 7 },
{ text: "Variables declared with var, let", start: 8, end: 11 }
],
"Topic 2" => [
{ text: "Prototype inheritance in JavaScript", start: 12, end: 15 },
{ text: "DOM API manipulates HTML", start: 16, end: 19 },
{ text: "Callbacks used for async code", start: 20, end: 23 }
],
"Topic 3" => [
{ text: "Runs client-side and server-side", start: 24, end: 27 },
{ text: "React and Vue popular frameworks", start: 28, end: 31 },
{ text: "Node.js runs JavaScript outside browser", start: 32, end: 35 }
]
} */

</script>


Who I am

"Just me, myself, and I, exploring the universe of unknownment, embracing the mystery of what lies beyond, one star at a time."

Share this page on Facebook | X.com | Reddit

Page Content

  • Image php project structure
    Published: 2024-12-21
    Read
  • Image Git commands
    Published: 2024-12-20
    Read
  • Image Gc2VBdwXMAAudpx
    Published: 2024-12-20
    Read
  • Image sysbench
    Published: 2024-12-19
    Read
  • Image groupBy vs reduce
    Published: 2024-12-19
    Read

Tags

WebDev New York London AI NORWAY DIY Ideas Life Family News Clothing Invest Sports Health