Friday, 18 October 2024

Guide for Markdown language

Here’s a hands-on guide showing you how to use Markdown with examples of all the key features:


1. Headings

You can create headings by using # symbols. More # means a smaller heading.

# Heading 1 ## Heading 2 ### Heading 3 #### Heading 4

Result:

Heading 1

Heading 2

Heading 3

Heading 4


2. Bold and Italics

You can format text in bold, italic, or bold and italic.

**Bold Text** *Italic Text* ***Bold and Italic Text***

Result:

  • Bold Text
  • Italic Text
  • Bold and Italic Text

3. Lists

Unordered List:

Use -, *, or + to create bullet points.

- Item 1 * Item 2 + Item 3

Ordered List:

Use numbers followed by a period.

1. First 2. Second 3. Third

Result:

  • Item 1
  • Item 2
  • Item 3
  1. First
  2. Second
  3. Third

4. Links

Create a link using [Link Text](URL).

[Visit GitHub](https://github.com)

Result: Visit GitHub


5. Images

Insert an image using ![Alt Text](Image URL).

![GitHub Logo](https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png)

Result:


6. Blockquotes

Use > to create a blockquote.

> This is a blockquote.

Result:

This is a blockquote.


7. Code Blocks

You can insert inline code using backticks (`) or block code using triple backticks (```).

Inline `code` example.

For code blocks:

```python def hello_world(): print("Hello, World!")
**Result:** Inline `code` example. ```python def hello_world(): print("Hello, World!")

8. Horizontal Line

Use ---, ***, or ___ to create a horizontal rule.

---

Result:



9. Task Lists

You can create task lists with - [ ] for incomplete and - [x] for completed tasks.

- [ ] Task 1 - [x] Task 2

Result:

  • Task 1
  • Task 2

10. Tables

Use pipes (|) and dashes (-) to create tables.

| Column 1 | Column 2 | Column 3 | | -------- | -------- | -------- | | Item 1 | Item 2 | Item 3 |

Result:

Column 1Column 2Column 3
Item 1Item 2Item 3

11. Strikethrough

Strike through text using ~~.

~~This text is struck through~~

Result:

This text is struck through


12. Footnotes

Create footnotes with [^1] for reference and [^1]: text for the actual footnote.

This is a footnote reference[^1]. [^1]: This is the footnote text.

Result: This is a footnote reference1.


13. Escaping Characters

Use a backslash (\) to escape Markdown special characters like #, *, or _.

\# Not a heading \*Not italic\*

Result:

# Not a heading
*Not italic*

Guide use Botble

 

References
-
https://docs.botble.com/cms/filters.html

Thank you.

Wednesday, 16 October 2024

Optimize using memory in php with Generators

In PHP, a generator provides a memory-efficient way to handle large datasets or streams of data by producing values one at a time, rather than loading everything into memory at once. This can significantly reduce memory usage when working with large datasets, as it avoids the need to store the entire dataset in memory.

Key Concepts:

  1. Standard Iteration (Without Generators):

    • When using a regular function that returns a large dataset, PHP loads the entire dataset into memory at once, which can consume a lot of memory, especially for large collections of data.
  2. Generators:

    • A generator allows you to iterate over a sequence of values without having to create and store the entire sequence in memory. It works by "yielding" values one at a time.
    • Instead of returning a full array, the yield keyword returns one value at a time during each iteration.

Example:

Here's how a generator works in PHP compared to regular iteration:

Without a Generator (High Memory Usage):

function largeDataset() { $data = []; foreach (/* large data source */ as $item) { $data[] = $item; } return $data; } foreach (largeDataset() as $data) { process($data); }
  • This function returns the entire dataset as an array.
  • The entire array must be stored in memory, which can be problematic for large datasets, causing high memory usage.

With a Generator (Memory Efficient):

function largeDataset() { foreach (/* large data source */ as $data) { yield $data; // Yield returns one value at a time } } foreach (largeDataset() as $data) { process($data); }
  • The yield keyword in this function produces one value at a time.
  • Instead of returning a complete array, the generator produces values lazily, meaning it only generates the next value when needed.
  • This avoids loading the entire dataset into memory at once.

How Generators Work:

  • When you call a generator function, it doesn't execute immediately. Instead, it returns an object of type Generator.
  • Each time you iterate over the generator (using foreach or similar), PHP resumes execution of the generator from where it left off, until it hits the next yield.
  • The generator pauses and saves its state after each yield, allowing you to continue later without reloading all the data.

Benefits of Using Generators:

  1. Memory Efficiency: Since only one value is stored in memory at a time, this greatly reduces the memory footprint.
  2. Improved Performance: In cases where you don't need to process the entire dataset at once, generators improve performance by handling one piece of data at a time.
  3. Streaming Large Data: Generators are useful for working with large files or database queries where you can process data in chunks, instead of loading everything into memory.

Example of a Real Use Case:

Imagine you are processing a large CSV file:

function readLargeCsv($filename) { $handle = fopen($filename, 'r'); while (($row = fgetcsv($handle)) !== false) { yield $row; // Yield each row from the CSV } fclose($handle); } foreach (readLargeCsv('hugefile.csv') as $row) { process($row); // Process each row one by one without loading the entire file into memory }
  • In this case, only one row of the CSV file is in memory at any given time, even if the file contains millions of rows. This prevents memory exhaustion and allows you to process very large files efficiently.

Conclusion:

Using generators in PHP is an excellent technique to optimize memory usage and improve performance, particularly when dealing with large datasets. The key advantage is that you only work with one piece of data at a time, which allows you to process large data sources without overwhelming system resources.

Thank you

Tuesday, 15 October 2024

FileTrackingServiceProvider for Laravel

 FileTrackingServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Laravel\Telescope\Telescope;

class FileTrackingServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
       // Listen to each request
        $this->app['router']->matched(function () {
            // Get all included files
            $includedFiles = get_included_files();

            // Filter out files in the vendor directory
            $phpBladeFiles = array_filter($includedFiles, function ($file) {
                return preg_match('/\.(php|blade\.php)$/', $file) &&
                    strpos($file, 'Shofy\vendor') === false;  // Exclude vendor files
            });

            // Log these files using Laravel Log
            foreach ($phpBladeFiles as $file) {
                Log::info("Non-vendor file included: $file");
            }
        });
    }
}


Fix the 413 Request Entity Too Large error in Laravel running on Nginx

1. Increase client_max_body_size in Nginx

The client_max_body_size directive in Nginx controls the maximum allowed size of the client request body, which includes uploaded files.

Steps:

  • Open your Nginx configuration file. This could be located at /etc/nginx/nginx.conf or in a site-specific configuration file under /etc/nginx/sites-available/.
sudo nano /etc/nginx/nginx.conf
  • Add or update the client_max_body_size directive inside the http block (or inside server or location blocks, if you prefer):
http { client_max_body_size 100M; }
$ sudo service nginx reload
 

2. Increase post_max_size and upload_max_filesize in PHP

Nginx will pass the request to PHP, and PHP has its own limits. You need to increase the upload_max_filesize and post_max_size in your php.ini file.

Steps:

  • Find your php.ini file. It is usually located in /etc/php/7.x/fpm/php.ini (on Linux) or C:\xampp\php\php.ini (on Windows).

  • Open the file and increase the following values:

upload_max_filesize = 100M post_max_size = 100M
 
Thank you

Sunday, 13 October 2024

Compare Aimeos and Bagisto

FeatureAimeosBagisto
PerformanceHigh performance, optimized for large-scaleModerate, suitable for SMBs
Multi-VendorAdvanced multi-vendor supportBasic multi-vendor functionality
Clean CodeComplex but modular, highly customizableLaravel-friendly, clean, easier for smaller projects
FeaturesFeature-rich, subscription, B2B/B2C, multi-storeSimpler, essential features, extendable via plugins
ScalabilityEnterprise-level scalabilityGood for small to mid-sized businesses
CommunitySmaller but mature ecosystemGrowing community, lots of support

Conclusion

  • Aimeos is the best choice for large-scale e-commerce platforms or businesses that require a high degree of customization, performance, and scalability. It’s ideal for complex multi-vendor marketplaces or companies with advanced e-commerce needs.

  • Bagisto is more suitable for small to medium businesses looking for a quick and efficient e-commerce solution with multi-vendor support. Its simplicity and Laravel-friendly codebase make it a great starting point for developers who want to get a store up and running quickly without needing the complexity of Aimeos.

Thank you.

Wednesday, 9 October 2024

Optimize a Laravel application for performance

Optimizing a Laravel application for performance is crucial for ensuring that your application can handle high traffic, respond quickly, and use server resources efficiently. Here are some strategies and techniques to boost Laravel application performance:

1. Caching:

Laravel provides several types of caching that can significantly reduce the load on your server and speed up response times.

  • Route Caching: Speeds up route resolution by caching the routes to avoid re-parsing them on each request.
    php artisan route:cache
  • Config Caching: Loads configuration files faster by caching them.
    php artisan config:cache
  • View Caching: Caches Blade views to avoid recompiling them for every request.
    php artisan view:cache
  • Query Caching: Caches database query results to avoid repeated expensive queries.
    $users = User::remember(60)->get(); // Caches for 60 minutes

2. Optimize Database Queries:

Efficient database interaction can significantly reduce the load time of your application.

  • Use Eager Loading: Instead of loading related models one by one, use eager loading to load all relationships at once, reducing the number of queries.
    // Without eager loading (N+1 problem) $users = User::all(); foreach ($users as $user) { echo $user->profile->bio; } // With eager loading $users = User::with('profile')->get();
  • Database Indexing: Add appropriate indexes to database tables, especially for columns used in WHERE, JOIN, and ORDER BY clauses.
  • Use SELECT with specific columns: Only select the fields you need instead of retrieving all fields.
    $users = User::select('id', 'name')->get();
  • Batch Inserts and Updates: Use batch operations to reduce the number of database hits.
    DB::table('users')->insert([ ['name' => 'John', 'email' => 'john@example.com'], ['name' => 'Jane', 'email' => 'jane@example.com'] ]);

3. Use Queues for Time-Consuming Tasks:

Offload tasks like sending emails, resizing images, and other resource-intensive operations to queues. This frees up your application to handle more requests quickly.

ProcessImageJob::dispatch($image);

4. Optimize Middleware:

  • Reduce Middleware: Only apply middleware where necessary using only or except methods in your controllers or route definitions.
    Route::get('profile', 'ProfileController@show')->middleware('auth');
  • Use Route Grouping: Group routes that use the same middleware to avoid reapplying middleware individually.

5. Leverage HTTP Caching:

Utilize browser caching and response headers to reduce the amount of data transmitted to clients.

  • Set Cache-Control headers: Use Laravel’s Cache facade to set cache headers for static content.
    return response($content) ->header('Cache-Control', 'public, max-age=3600');
  • Use Content Delivery Networks (CDNs): Offload serving static assets (e.g., CSS, JavaScript, images) to CDNs, reducing server load and speeding up delivery.

6. Optimize Assets:

  • Minify CSS/JavaScript: Use tools like Laravel Mix or npm scripts to minify CSS and JavaScript files to reduce their size.
    npm run production
  • Defer JavaScript Loading: Use defer or async attributes on <script> tags to prevent JavaScript from blocking the rendering of the page.
  • Use Gzip Compression: Enable Gzip compression on your web server to compress HTML, CSS, and JS files sent to clients.

7. Use Redis for Session and Cache:

Redis is a fast, in-memory key-value store that can be used to store sessions, cache data, and even queues.

  • Sessions: Configure Laravel to use Redis for session management.
    SESSION_DRIVER=redis
  • Cache: Set Redis as your caching driver in the .env file.
    CACHE_DRIVER=redis

8. Database Optimization:

  • Use Pagination: Don’t load all records in one request; instead, paginate them.
    $users = User::paginate(10);
  • Database Connection Pooling: Use tools like pgbouncer (for PostgreSQL) to reduce overhead in creating new connections.

9. Optimize Blade Views:

  • Avoid Complex Logic in Views: Move complex logic to controllers or view composers instead of keeping it in Blade files.
  • Use View Composers: Share data with multiple views using view composers.
    View::composer('profile', function ($view) { $view->with('user', Auth::user()); });

10. Enable OPcache:

OPcache is a caching engine that caches PHP bytecode in memory, improving performance by eliminating the need for PHP to compile scripts on each request.

  • Enable OPcache in your PHP configuration (php.ini).
    opcache.enable=1

11. Lazy Collection for Large Datasets:

When working with large datasets, you can use Laravel's LazyCollection to handle data without loading everything into memory at once.

$users = User::lazy(); foreach ($users as $user) { // Process user... }

12. Use Optimized Composer Autoloading:

Use optimized autoloading in Composer to improve performance by avoiding unnecessary class loading.

composer install --optimize-autoloader --no-dev

13. Optimize Images:

Compress and optimize images to reduce their size and loading times. Tools like spatie/laravel-image-optimizer can help automate this process.


By combining these techniques, you can significantly boost the performance of your Laravel application.

Thank you.

Publish npm package

  Để publish   pav-kit  lên NPM, bạn hãy làm theo các bước dưới đây. Tôi đã tạo thêm file  index.js  để đảm bảo gói tin hợp lệ. Bước 1: Tạo ...