Home

Awesome

MniPagesBundle

Much awesomeness, very good, coming soon.

What is this?

Typical PHP web frameworks are using controllers and actions:

This pattern is easy to understand, because every request just calls a controller action. But this model doesn't feel natural: even though they all are HTTP requests, at a higher level a web page and an AJAX request are not the same thing.

There is another way: component-based architecture. This pattern introduces the concept of Page, Component and Action.

A small example

Pages

A page is a class:

class HomePage extends Page
{
    protected $title = 'Hello!';
    protected $pageCountComponent;

    public function __construct()
    {
        $this->pageCountComponent = new PageCountComponent();
    }
}

and a template:

{% extends '::layout.html.twig' %}

{% block content %}

    <h1>{{ title }}</h1>

    {{ component(pageCount) }}

{% endblock %}

Each property of the page is accessible in the template.

Components

A component is a class too:

class PageCountComponent extends Component
{
    protected $count;

    public function __construct()
    {
        $this->count = $this->get('session')->get('pageCount', 0);
    }
}

and a template:

<p>
    Page count: {{ count }}.
</p>

Actions

Let's add an action that resets the page count and refresh the component (not the page!):

<p>
    Page count: {{ count }}.
    <button type="button" data-component-action="reset" data-component-refresh>Reset</button>
</p>
class PageCountComponent extends Component
{
    protected $count;

    public function __construct()
    {
        $this->count = $this->get('session')->get('pageCount', 0);
    }

    public function reset()
    {
        $this->count = 0;
        $this->get('session')->set('pageCount', 0);
    }
}

Easy! The Javascript library already takes care of AJAX requests and refreshing components.

Getting started

Requirements

Installation

{
    "require": {
        "mnapoli/pages-bundle": "*"
    }
}
public function registerBundles()
{
    return array(
        // ...
        new Mni\PagesBundle\MniPagesBundle(),
    );
}
{% javascripts
    '@MniPagesBundle/Resources/public/js/*' %}
    <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

Documentation

Demo

The demo is in the demo/ folder. It's a standard Symfony app, you can get it working easily:

$ cd demo/
$ composer install
$ app/console server:run

License

MniPagesBundle is licensed under the MIT license. See the LICENSE file for more informations.