# Integrate CakePHP with Medianova CDN

**CakePHP** is an open-source PHP framework based on the MVC (Model-View-Controller) pattern, similar to Zend, Laravel, and Symfony.\
This guide explains how to configure **Medianova CDN** for **CakePHP version 2.4 and later**, enabling your application to serve static content from the nearest CDN edge node instead of the origin server.

{% hint style="info" %}
Before starting the integration, back up your CakePHP project files and database.
{% endhint %}

### Prerequisites

* A configured **CDN Resource**.
* A running **CakePHP 2.4+** application
* Write access to your `./Config/bootstrap.php` file

### Integration Steps

{% stepper %}
{% step %}

### Create a CDN Resource

* Log in to the [**Medianova Control Panel**](https://cloud.medianova.com).
* Create a new **CDN Resource** for your application.
* Copy your Resource URL (for example: `https://example.mncdn.com/`).
  {% endstep %}

{% step %}

### Define CDN Base URLs

* Open the configuration file:\
  \&#xNAN;**`./Config/bootstrap.php`**
* Add the following variables to define Medianova CDN paths for your assets:

```php
<?php
Configure::write('App.imageBaseUrl', 'https://<CDN_ZONE_URL>/img/');
Configure::write('App.cssBaseUrl',   'https://<CDN_ZONE_URL>/css/');
Configure::write('App.jsBaseUrl',    'https://<CDN_ZONE_URL>/js/');
```

{% hint style="info" %}
Replace `<CDN_RESOURCE_URL>` with your actual CDN Resource address, such as `https://example.mncdn.com/`.
{% endhint %}
{% endstep %}

{% step %}

### Use the HTML Helper for Images

Use the `HtmlHelper::image()` function to generate image URLs automatically through the CDN.

```php
<?php echo $this->Html->image('medianova-logo.png', ['alt' => 'Medianova Logo']); ?>
```

**Output:**

```html
<img src="https://example.mncdn.com/img/medianova-logo.png" alt="Medianova Logo" />
```

{% endstep %}

{% step %}

### Use the HTML Helper for CSS Files

To load your CSS files from the CDN, use the `HtmlHelper::css()` function:

```php
<?php echo $this->Html->css('style.css'); ?>
```

**Output:**

```html
<link rel="stylesheet" type="text/css" href="https://example.mncdn.com/css/style.css" />
```

{% endstep %}

{% step %}

### Use the HTML Helper for JavaScript Files

To load JavaScript assets from the CDN, use the `HtmlHelper::script()` function:

```php
<?php echo $this->Html->script('script.js'); ?>
```

**Output:**

```html
<script type="text/javascript" src="https://example.mncdn.com/js/script.js"></script>
```

{% endstep %}

{% step %}

### Verify Integration

* Save your configuration and clear the CakePHP cache.
* Open your website in a browser and view the HTML source (`Ctrl + U`).
* Confirm that image, CSS, and JS assets are loaded from your **Medianova CDN Resource**.

{% hint style="info" %}
Example:\
`https://example.mncdn.com/css/style.css`
{% endhint %}
{% endstep %}
{% endstepper %}

### Troubleshooting

| Problem                                      | Cause                                               | Solution                                                                                             |
| -------------------------------------------- | --------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **Assets still load from the origin server** | CDN URLs not defined or configuration not reloaded. | Check the `bootstrap.php` entries and clear the CakePHP cache.                                       |
| **Assets missing or 404 errors**             | Incorrect CDN path or directory mismatch.           | Verify that your `img`, `css`, and `js` directories match the structure in your CDN Resource.        |
| **Mixed content warning (HTTP/HTTPS)**       | HTTPS not enabled on CDN resource.                  | Enable **Shared SSL** or **Custom SSL** in the **Medianova Control Panel** before using secure URLs. |
