Skip to the content.

Chapter 6: Advanced Topics

This chapter covers some of the more advanced features and helper classes in PrestaSDK that will help you develop more complex modules.

6.1. Asset Management (CSS/JS)

A common challenge in web development is managing browser cache for CSS and JavaScript files. PrestaSDK solves this problem with an automated system.

AssetPublisher and Automatic Versioning

The AssetPublisher class is responsible for copying prestasdk.css and prestasdk.js files from the vendor directory into your module’s views/css and views/js directories. This is done during module installation. More importantly, the setMedia() method in AdminController automatically includes these files in your pages and appends a version number to their URLs (e.g., ?v=0.4.0). This version number is read from the SDK’s own composer.json file. What’s the benefit? Whenever you update the PrestaSDK version via Composer, the version number in the file URLs changes. This forces users’ browsers to download the new files, completely solving the caching issue. If the SDK version has changed, AdminController will automatically re-run AssetPublisher to replace the old files with the new ones.

6.2. Request Lifecycle and Middlewares

PanelCore (used in AdminController) provides a powerful Middleware-like system for managing the request lifecycle. This system allows you to execute code before or after the main logic of a “section” runs. This is extremely useful for tasks like access validation, processing POST data before a form is displayed, or loading data that is shared across multiple sections.

How to Use middlewaresACL

To use this feature, you need to define the $middlewaresACL property in your controller. This is an array that specifies which methods (middlewares) should run and when. Array Structure:

$this->middlewaresACL = [
    'before' => [
        // 'section@Controller' => ['middleware_name1', 'middleware_name2'],
    ],
    'after' => [],
    'ignore' => [], // To skip a middleware under certain conditions
];

This class contains static methods for common tasks:

VersionHelper

This class has a static method getSDKVersion() that reads the current SDK version from its composer.json file. It is used internally by AssetPublisher.