Add a blog to your Symfony Application with Hyvor Blogs
Tags: php, symfony
Published: 2022-12-09 11:17:31

If you want to add a blog to your [Symfony](https://symfony.com/) application, this guide is for you.

[Hyvor Blogs](https://blogs.hyvor.com/) is a simple and powerful blogging platform. In this tutorial, we will see how to create a blog with Hyvor Blogs and host it at `/blog` in your Symfony application. All contents of the blog will live inside a cache pool in your application. We will use webhooks for cache invalidation.

Part 1: Set up Hyvor Blogs
--------------------------

First, we will create a blog in Hyvor Blogs and configure the basic settings needed for self-hosting.

-   First, create a blog at the [Hyvor Blogs Console](https://blogs.hyvor.com/console/new). You will get a subdomain, which you need in the next step.

-   Go to Settings → Hosting

    -   Update Hosting on/at to Self-hosting

    -   Set the Self-hosting URL to the absolute URL of your Symfony application's blog route. For this tutorial, set it to `https://mywebsite.com/blog`. You can customize the `/blog` route later if needed.

        Note: To test webhooks, your Symfony application URL should be publicly accessible. Therefore, if you are developing locally, we recommend using a tool like [ngrok](https://ngrok.com/) to expose your Symfony site temporarily on the internet.

    -   Click Save

        ![Hosting setting up in Hyvor Blogs Console](https://blogs.hyvor.com/blog/media/r0EILgfcGMXbVAk8FFuLXtDdprFg9BP0drwncOh2.png)

        Setup Self-hosting

-   Go to Settings → API Keys

    -   Click CREATE

        -   Set a name (ex: "For Symfony Blog")

        -   Select Delivery API as the API

        -   Create API Key

        This API key will be needed in the next step.

-   Go to Settings → Webhooks and create a Webhook with the following values.

    -   URL: Set this to "your website URL + /hyvorblogs/webhook". Ex: `https://mywebsite.com/hyvorblogs/webhook`

    -   Select the following events

        -   `cache.single`

        -   `cache.templates`

        -   `cache.all`

You will need the Webhook Secret in the next step.

Part 2: Configure Your Symfony Application
------------------------------------------

Next, we will configure your Symfony application to "communicate" with Hyvor Blogs APIs/Webhooks to render your blog correctly.

First, install the `hyvor/hyvor-blogs-symfony` package (bundle) in your project using composer.

```bash
composer require hyvor/hyvor-blogs-symfony
```

Then, add the bundle to `config/bundles.php` file:

```php
<?php

return [

   // other bundles...
   Hyvor\BlogsBundle\HyvorBlogsBundle::class => ['all' => true],

];
```

Then, add the following configuration to the `config/packages/hyvor_blogs.yaml` file.

```yaml
hyvor_blogs:
  webhook_path: /hyvorblogs/webhook
  blogs:
    -
        subdomain: your-subdomain
        base_path: /blog
        delivery_api_key: '**********'
        webhook_secret: '**********'
        cache_pool: cache.app
```

`webhook_path` is the route in your application that will handle the Webhooks from Hyvor Blogs.

`blogs` key contains an array of blog configurations. It allows you to set up multiple blogs in the same Symfony application. The following configurations are supported:

-   `subdomain` is the subdomain of the blog, which you created in the first step. You can get it to Console → Settings -> Hosting.

-   `base_path` is where your blog will be rendered in your Symfony application.

-   `delivery_api_key` is the Delivery API key that you created earlier at Console → Settings -> API Keys

-   `webhook_secret` is the secret key you got at Console → Settings -> Webhook

-   `cache_pool` is the [Symfony Cache Pool](https://symfony.com/doc/current/components/cache/cache_pools.html) that will be used to save the blog content. By default, it uses the app cache pool.

Finally, clear the Symfony cache to refresh the service container.

```bash
php bin/console cache:clear
# or
php bin/console cache:clear --env=prod
```

That is everything. Now, try visiting the `/blog` path of your blog. If everything is configured correctly, you should see your blog. Try updating the content of a post to make sure the content is updated on your blog, ensuring webhooks are working properly.

Finally
-------

-   If you have any troubles, comment below or contact support.

-   If you would like to know how this works internally, see our [self-hosting with web frameworks](https://blogs.hyvor.com/docs/self-hosting-web-framework) documentation.

-   Once you have set up everything, you can customize your blog theme and start writing. Our [documentation](https://blogs.hyvor.com/docs) has all the information you need.

-   If you like to set up multiple blogs within the same applications, you can add more configuration arrays to the `blogs` array in the config file.

-   Any problems with the Symfony package? Raise an issue or contribute on [Github](https://github.com/hyvor/hyvor-blogs-symfony).