Lightweight MVC Overview

The framework comes integrated with Lightweight MVC by default.

Lightweight MVC is a small framework that adds Models, Views and Controllers to WordPress.

Lightweight MVC utilizes existing WordPress functionality preventing from overloading the already heavy loaded WordPress core.

This framework was inspired by Laravel to add the MVC design pattern to WordPress development in an efficient, elegant and optimized way.

Lightweight MVC is already configured in the template and it is downloaded as dependency via composer.

Usage

Lightweight MVC engine is integrated with Main.php, call the engine with $this->mvc property.

In the following example, Main.php is calling PostController to save a post modification:

class Main extends Plugin
{
    public function on_admin()
    {
        add_action( 'save_post', array( &$this, 'save_post' ) );
    }

    public function save_post( $post_id )
    {
        $this->mvc->call( 'PostController@save', $post_id );
    }
}

In the following example, Main.php is calling PostController to return a filtered content:

class Main extends Plugin
{
    public function filter_content( $content )
    {
        return $this->mvc->action( 'PostController@filter', $content );
    }
}

Use action() for when a return value is expected and call() when it is not.

Displaying a view

A view can be generated with Main.php without calling to a controller:

class Main extends Plugin
{
    public function show_view()
    {
        return $this->view( 'view.key' );
    }
}

See Views documentation to understand this better.