OCClient plugin for CakePHP
A CakePHP 3.X plugin for the OpenComponents PHP Client, (*1)
Installation
You can install this plugin into your CakePHP application using composer., (*2)
The recommended way to install composer packages is:, (*3)
composer require alvarium/cake-oc
Configuration
After that, load the plugin in your bootstrap.php
file:, (*4)
Plugin::load('Alvarium/OCClient', ['bootstrap' => true]);
Create a file named occlient.php
in your config
folder defining your
open-components registry endpoint:, (*5)
<?php
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:, (*6)
<?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',
]);
}
}
Usage
After all configuration has been done, and having both component and helper loaded,
to load your opencomponents scripts in your pages you just need to define the
components using setComponents
in your controller method:, (*7)
<?php
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)., (*8)
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., (*9)
If so, you can just avoid using the helper and add the scripts wherever you want:, (*10)
foreach ($oc_components as $component) {
echo $component;
}
License
This plugin code is licensed under a GNU GPL v.3 license., (*11)