PHP code example of eth8505 / laminas-mvc-themer

1. Go to this page and download the library: Download eth8505/laminas-mvc-themer 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/ */

    

eth8505 / laminas-mvc-themer example snippets



return [
	'modules' => [
		'LaminasMvcThemer',
		// ...
	],
];
~~~

## How to use

### 1 Creating a theme
As described above, themes need to be registered with the theme plugin manager. A theme must implement the
`ThemeInterface`. All methods exept for `getName` may return an empty array. Check the `DefaultTheme` class
for an empty implementation of a theme specifying only a name.

#### 1.1 Specifying custom stylesheets
Implement the `getStylesheet` method in your theme, and return an array. Note that all paths will be pushed through
the `BasePath` view helper and hence must be relative to your `public/` directory.

~~~php


use LaminasMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getStylesheets() : array {
        return [
            'css/theme/my/custom/file.css'
        ];
    }
    // ...
}
~~~

All stylesheets are injected using the `appendStylesheet` method of the `HeadLink` viewhelper.

#### 1.2 Specifying custom javascripts
Implement the `getScripts` method in your theme, and return an array. Note that all paths will be pushed through
the `BasePath` view helper and hence must be relative to your `public/` directory.

~~~php


use LaminasMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getScripts() : array {
        return [
            'css/theme/my/custom/file.js'
        ];
    }
    // ...
}
~~~

All scripts are injected using the `prependFile` method of the `HeadScript` viewhelper.

#### 1.3 Specifying custom variables
Implement the `getVariables` method in your theme, and return an array. Note that all paths will be pushed through
the `BasePath` view helper and hence must be relative to your `public/` directory.

~~~php


use LaminasMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getVariables() : array {
        return [
            'heading1' => 'one',
            'heading2' => [
                'key1' => 'test'
            ]
        ];
    }
    // ...
}
~~~

Theme variables are not automatically injected into your view models, as this could interfere with whatever you
set in your view models. However, the module provides a `theme()` view helper allowing access to the theme variables.

~~~php
<html>
    <body>
        <h1><?= $this->theme()->var('heading1') 


use LaminasMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getMetaTags() : array {
        return [
            [
                'type' => 'name',
                'name' => 'robots',
                'content' => 'noindex,nofollow'
            ],
            [
                'type' => 'http-equiv',
                'name' => 'refresh',
                'content' => '30'
            ]
        ];
    }
    // ...


return [
    'laminas-mvc-themes' => [
        'invokables' => [
            MyTheme::class
        ]
    ]
];
~~~

or register it in your module class using the `ThemeProviderInterface`:
~~~php


namespace MyModule;

use LaminasMvcThemer\Theme\ThemeProviderInterface;

class Module implements ThemeProviderInterface {
    
    /**
     * @inheritdoc 
     */
    public function getThemeConfig() {

        return [
            'invokables' => [
                MyTheme::class
            ]
        ];
        
    }
    

namespace MyModule;

use LaminasMvcThemer\Resolver\ThemeResolverInterface;
use LaminasMvcThemer\Theme\ThemeInterface;
use LaminasMvcThemer\Theme\ThemePluginManager;
use Laminas\Http\Request;

class Module implements ThemeResolverInterface {

    private $request;
    
    private $pluginManager;

    public function __construct(Request $request, ThemePluginManager $pluginManager) {
        $this->request = $request;
        $this->pluginManager = $pluginManager;
    }

    public function resolve() : ThemeInterface  {
        
        if ($this->request->getUri()->getHost() === 'my.awesome.host') {
            $theme = ThemeOne::class;
        } else {
            $theme = ThemeTwo::class;
        }
        
        return $this->pluginManager->get($theme);
        
    }