PHP code example of responsive-sk / slim4-themes

1. Go to this page and download the library: Download responsive-sk/slim4-themes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

responsive-sk / slim4-themes example snippets


use Slim4\Themes\Provider\ThemeServiceProvider;

// Create container
$container = new \DI\Container();

// Register theme services
$themeServiceProvider = new ThemeServiceProvider();
$themeServiceProvider->register($container, [
    'cookie_name' => 'theme',
    'query_param' => 'theme',
]);

use Slim4\Themes\Provider\ThemeResolver;
use Slim4\Themes\Provider\ThemeProvider;
use Slim4\Themes\Middleware\RequestAwareMiddleware;
use Slim4\Themes\Interface\ThemeInterface;
use Slim4\Themes\Interface\ThemeLoaderInterface;

// Register theme services
$containerBuilder->register(ThemeResolver::class)
    ->setArguments([
        new Reference(ThemeLoaderInterface::class),
        '%theme.cookie_name%',
        '%theme.query_param%'
    ])
    ->setPublic(true);

$containerBuilder->register(ThemeProvider::class)
    ->setArguments([new Reference('service_container')])
    ->setPublic(true);

$containerBuilder->register(RequestAwareMiddleware::class)
    ->setArguments([
        new Reference(ThemeProvider::class),
        '%theme.cookie_name%',
        '%theme.query_param%'
    ])
    ->setPublic(true);

$containerBuilder->setAlias(ThemeInterface::class, ThemeProvider::class)
    ->setPublic(true);

// Add middleware
$app->add($container->get(RequestAwareMiddleware::class));

// Get theme from container
$theme = $container->get(ThemeInterface::class);

// Use theme
$templatePath = $theme->getTemplatePath('home.twig');

use Slim4\Themes\Provider\ThemeResolver;

// Create theme resolver
$themeResolver = new ThemeResolver(
    $themeLoader,
    'theme',
    'theme'
);

// Resolve theme
$theme = $themeResolver->resolveTheme($request);

use Slim4\Themes\Provider\ThemeProvider;

// Create theme provider
$themeProvider = new ThemeProvider($container);

// Set request
$themeProvider->setRequest($request);

// Get theme
$theme = $themeProvider->getTheme();

use Slim4\Themes\Middleware\RequestAwareMiddleware;

// Create middleware
$middleware = new RequestAwareMiddleware(
    $themeProvider,
    'theme',
    'theme'
);

// Add middleware
$app->add($middleware);

use Slim4\Themes\Provider\ThemeServiceProvider;

// Create service provider
$serviceProvider = new ThemeServiceProvider();

// Register services
$serviceProvider->register($container, [
    'cookie_name' => 'theme',
    'query_param' => 'theme',
]);

// Before
$app->add($container->get(\Slim4\Themes\Middleware\ThemeMiddleware::class));

// After
$app->add($container->get(\Slim4\Themes\Middleware\RequestAwareMiddleware::class));

// Before
$theme = $request->getAttribute('theme');

// After
$theme = $container->get(\Slim4\Themes\Interface\ThemeInterface::class);

// Register theme services
$themeServiceProvider = new \Slim4\Themes\Provider\ThemeServiceProvider();
$themeServiceProvider->register($container, [
    'cookie_name' => 'theme',
    'query_param' => 'theme',
]);