Controllers

Controllers are created to handle any business logic and algorithms.

Store your controllers at controllers folder.

Create

When creating a controller, the minimum things required are:

  • Specify a namespace.
  • Add use to include the Controller class.
  • Extend from Controller.

Example:

<?php

namespace MyApp\Controllers;

use Amostajo\LightweightMVC\Controller;

class PostController extends Controller
{
    // Your code here
}

Use

Controllers are called by Main.php, like:

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

Your controller would look like this:

class PostController extends Controller
{
    public function filter ( $content )
    {
        // CUSTOM CODE
        return $content;
    }
}

Views

Use the $this->view property within a controller to access views.

When $this->mvc->call() is used in Main.php, Lightweight MVC will echo any returned result from the controller.

In the example below,  Lightweight MVC will echo and display the view returned by the controller.

class Main extends Plugin
{
    public function show_metabox()
    {
        return $this->view->get( 'metaboxes.viewkey', $params );
    }
}

When $this->mvc->action() is used, Lightweight MVC will return the controller’s result to Main.php, no echo / display will be done.

User access

Use the $this->user property within a controller to access WordPress’ current user information.