Fork me on Github

Silex Integration

Use CalendR inside your Silex application thanks to CalendRServiceProvider

Usage And registration

<?php
$app->register(new CalendR\Extension\Silex\Provider\CalendRServiceProvider(), array(
    'calendr.event.providers' => array(
        'my_awesome_event_provider' => new MyAwesomeEventProvider(),
    )
));

To add CalendR support to Silex.

Now, you can access to the CalendR\Calendar factory by using $app['calendr'] in your Silex application.

Note : If you use Twig in your silex app, the CalendR Twig extension will be automatically registered.

Example display a month and retrieve events

Controller :

<?php

$app->get('/calendar/{year}/{month}', function($year, $month) use($app) {
    $calendRMonth = $app['calendr']->getMonth($year, $month);

    return $this->render('show_calendar.html.twig', array(
        'month'  => $calendRMonth,
        'events' => $app['calendr']->getEvents($calendRMonth->getExtendedMonth())
    ));
});

Note : Using $month->getExtendedMonth() is not mandatory, but it will retrieve events for displayed days that are out of month, and reduce call to the Event provider.

Template :

<table>
    <thead>
        <tr>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Saturday</th>
            <th>Sunday</th>
        </tr>
    </thead>
    <tbody>
        {% for week in month %}
            <tr>
                {% for day in week %}
                    <td>
                        {{ day }}
                        {% set day_events = events.find(day) %}
                        {% if day_events|length > 0 %}
                            <ul>
                                {% for event in day_events %}
                                    <li>{{ event }}</li>
                                {% endfor %}
                            </ul>
                        {% endif %}
                    </td>
                {% endfor %}
            </tr>
        {% endfor %}
    </tbody>
</table>

Note : Passing the events to the template is not mandatory, you can retrieve them directly in the template by using the twig function calendr_events(month).