Sulu Skeleton

repository·3.0·Indexed 43 days ago

https://github.com/sulu/skeleton

The recommended project template for starting new websites using the Sulu PHP content management system. Built on the Symfony framework, it facilitates multi-lingual and multi-portal development. Requires PHP 8.2+ and is compatible with Symfony versions 5.4 through 7.4.

Tokens
1.6K
Snippets
6
Records
9
Agent score
55%

What's inside Sulu

  1. Install a new Sulu project using the skeleton template

    3.0

    To start a new Sulu project following best practices, use the composer create-project command with the sulu/skeleton package. This template builds upon the official symfony/skeleton and configures the sulu/sulu core framework automatically.

    After installation, refer to the official Sulu documentation to initialize and configure the project for your specific requirements.

    composer create-project sulu/skeleton my-project
  2. Understand Sulu Kernel Contexts

    3.0

    The SuluKernel operates within different contexts, which determines how the application behaves and which parts of the system are initialized. The context is automatically determined based on the request URI:

    • SuluKernel::CONTEXT_ADMIN: Triggered when the request URI starts with /admin or /admin/.
    • SuluKernel::CONTEXT_WEBSITE: The default context for all other requests.

    This context is passed to the Kernel constructor during initialization.

  3. Enable HttpCache for Production

    3.0

    In non-development environments (APP_ENV !== 'dev'), Sulu can wrap the kernel in a SuluHttpCache layer to improve performance. This is automatically applied when the context is SuluKernel::CONTEXT_WEBSITE.

    Note: If you choose to use a Varnish-based caching strategy instead of the built-in SuluHttpCache, you must manually comment out the HttpCache wrapping logic in the front controller.

  4. Use bind-mounting for database data persistence

    3.0

    By default, the database service uses a named Docker volume database_data for persistence. To prevent accidental data loss from removing volumes, you can switch to a bind-mounted host directory.

    To use a bind mount, comment out the named volume and uncomment the host directory mapping in the volumes section of the database service:

    # Instead of:
    # - database_data:/var/lib/mysql
    
    # Use a host directory:
    - ./docker/db/data:/var/lib/mysql:rw
        volumes:
          - ./docker/db/data:/var/lib/mysql:rw
  5. Configure the MySQL database service

    3.0

    The compose.yaml file defines a database service using MySQL. You can configure the database instance using the following environment variables:

    • MYSQL_VERSION: Specifies the MySQL image version. Defaults to 8.4 if not provided.
    • MYSQL_DATABASE: The name of the database to create on startup. Defaults to su_myapp.
    • MYSQL_ROOT_PASSWORD: The root password for the MySQL instance. Warning: You should definitely change this password in production. Defaults to ChangeMe.
    • MYSQL_ROOT_HOST: Sets the host allowed to connect as root. Defaults to % (allowing connections from any host).

    Data persistence is handled via a named volume database_data mapped to /var/lib/mysql.

    services:
      database:
        image: mysql:${MYSQL_VERSION:-8.4}
        environment:
          MYSQL_DATABASE: ${MYSQL_DATABASE:-su_myapp}
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-ChangeMe}
          MYSQL_ROOT_HOST: '%'
        volumes:
          - database_data:/var/lib/mysql
    
    volumes:
      database_data:
  6. Configure Maintenance Mode

    3.0

    Sulu supports a maintenance mode that can be triggered via an environment variable. If SULU_MAINTENANCE is set to true (or any truthy value), the application will attempt to load and execute public/maintenance.php.

    To allow specific users or IPs to bypass maintenance mode, the maintenance.php file must return false. If it returns true, the application will exit and show the maintenance page.

    # Example: Enabling maintenance mode via environment variable
    export SULU_MAINTENANCE=true
  7. Sulu project requirements

    3.0

    When setting up a Sulu project, ensure your environment meets the following minimum requirements:

    • PHP: Version 8.2 or higher.
    • Symfony: Compatible with versions 5.4 through 7.4.

    For a complete and up-to-date list of dependencies, consult the require section in the composer.json of the sulu/sulu core framework.

  8. Configure HTTP Caching in the Application Kernel

    3.0

    The App\Kernel class extends Sulu\Component\HttpKernel\SuluKernel and implements FOS\HttpCache\SymfonyCache\HttpCacheProvider. To enable or customize HTTP caching, you can interact with the getHttpCache() method.

    By default, it returns an instance of Sulu\Bundle\HttpCacheBundle\Cache\SuluHttpCache. If you need user-based caching (where the cache varies by user session), you can add a UserContextListener to the cache instance. Note that when using a custom session name, you should configure the session_name_prefix (e.g., to SULUSESSID).

    // Example of how the Kernel handles HTTP cache retrieval
    public function getHttpCache(): HttpKernelInterface
    {
        if (!$this->httpCache instanceof HttpKernelInterface) {
            $this->httpCache = new SuluHttpCache($this);
            // To enable user-based caching:
            // $this->httpCache->addSubscriber(
            //    new \FOS\HttpCache\SymfonyCache\UserContextListener([
            //        'session_name_prefix' => 'SULUSESSID',
            //    ])
            // );
        }
    
        return $this->httpCache;
    }
  9. Configure Trusted Proxies and Hosts

    3.0

    To ensure correct handling of client IP addresses and host headers when running behind a load balancer or reverse proxy, configure the following environment variables:

    • TRUSTED_PROXIES: A comma-separated list of IP addresses or hostnames. When set, Sulu configures Symfony's Request to trust X-Forwarded-For, X-Forwarded-Port, and X-Forwarded-Proto headers.
    • TRUSTED_HOSTS: The trusted hostnames for the application.
    # Example .env configuration
    TRUSTED_PROXIES=192.168.1.1,10.0.0.1
    TRUSTED_HOSTS=example.com