PHP code example of alvarium / cake-oc

1. Go to this page and download the library: Download alvarium/cake-oc 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/ */

    

alvarium / cake-oc example snippets



return [
    'OCClient' => [
        'serverRendering' => 'https://registry.your-company.io/',
    ]
];
~~~

Then, load the component + the helper in the controller(s) where you want the
plugin to be used:

~~~php

namespace App;

use Cake\Event\Event;

class PostsController extends AppController
{
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Alvarium/OCClient.Client');
    }

    public function beforeRender(Event $event)
    {
        parent::beforeRender($event);

        $this->viewBuilder()->helpers([
            'Alvarium/OCClient.OC',
        ]);
    }

namespace App;

// [...]

class PostsController extends AppController
{
    // [...]

    public function index()
    {
        $this->Client->setComponents([
            [
              'name' => 'your-awesome-component',
              'parameters' => [
                  'comp1_selector' => '#awesome-component',
                  'key' => 'value',
                  'key2' => 'value2',
              ],
            ],
            [
                'name' => 'another-awesome-component',
                'parameters' => [
                    'comp2_selector' => '#awesome-component-2',
                    'key12' => 'value12',
                    'key23' => 'value23',
                ]
            ]
        ]);
    }
~~~

Note how we define the `*_selector` key. That's because we're able to define our
selectors for each component (meaning we need to define a layer with the
appropriated selector in the view for each component).

That's in part because this plugin's helper loads components appending them to
the `script` view block. Depending on how do you work with your opencomponent
widgets this could bring some issues.

If so, you can just avoid using the helper and add the scripts wherever you want:

~~~php
foreach ($oc_components as $component) {
    echo $component;