Skip to main content

PHP applications

It is also possible to use Discovery CMS in the construction of PHP pages. On github you can download a simple connector:

 git clone https://github.com/discoveryreply-com/discovery-cms-php-client
cd discovery-cms-php-client
composer install

Once done, you need to modify the DiscoveryConnector class constructor call by editing the public/index.php example file to insert your own root API and token:

 $discoveryConnector = new DiscoveryConnector([
'api_root' => 'https://HOSTNAME',
'api_token' => 'YOUR_API_TOKEN',
'property_title' => 'YOUR_PROPERTY',
'components_namespace' => 'DiscoveryCms\\PhpClient\\SampleApp\\Components',
]);

Once done, you can launch the web server with:

php -S localhost:8080 -t public

If port 8080 is already in use, replace with another port.

The sample application defines PHP components that are simply classes in the path src/SampleApp/Components/. These classes follow a naming convention, i.e. they must have identical names to components defined on the CMS side and implement a public static function render(object $data) method that receives the payload from the API and renders it to HTML.

The other convention is that these components must be in the namespace corresponding to the components_namespace option passed to the DiscoveryConnector constructor.

For instance, if you create a CMS component with ID = EditorChoice, in the PHP frontend that connects to the CMS you must create a classe with the same name, using the namespace in configuration:

DiscoveryCms\PhpClient\SampleApp\Components\EditorChoice

At this point, imagining that the page with the home slug returns a payload like this:

{
"components": [
{
"_type": "MySite/CTA",
"_id": "17f2e0b7-6219-4119-b226-409aebd6dd97",
"body": "body example",
"headline": "headline example"
},
{
"_type": "MySite/EditorChoice",
"_id": "a7e48093-294b-4b4c-886b-f0c6983ffced",
"headline": "Goofy"
},
{
"_type": "MySite/Trending",
"headline": "trending products",
"_id": "742f838d-4e0c-44ac-8255-e5a35aa11f38"
}
],
...
}

CTA::render($data), EditorChoice::render($data) and Trending::render($data) will be called, each time passing the corresponding fragment of page to the component, so that it can access, for example, the headline, body, etc. For instance, the headline of EditorChoice entered in the CMS corresponds to the property $data->headline.

In case the component needs access to the page data, it is possible to use the special $data->_page property.