1. Home
  2. Docs
  3. Application Framework
  4. Classes & Patterns
  5. Controllers



The controller pattern is used to create a class which generates the output for a given page (admin, front end, or both). A controller can encapsulate multiple “screens” which are loaded according to the url parameters used to access the page it is assigned to.

@How A Controller Works

A controller acts as a handler for all requests to the associated page specified in its configuration.

Any time the controller is going to handle rendering a page, the init() method on the controller is called which allows the controller to perform any pre-processing of the page request (such as internal setup, or authorization checks). When the page is loaded, the do_index() method on the controller instance is called to process the request and render the page.

If a &do=action parameter exists on the page request, then the method which is called to process the page changes to do_action(). You can create handlers for any number of do= actions in your controller to suit your needs.

@Example Code

use MWP\Framework\Pattern\Controller;

class MyController extends Controller
    public function do_index()
        return "Mission Accomplished.";

/* Create a page in the WP Admin which uses your controller */
$admin_controller = MyController::create( 'admin', [
    'adminPage' => [
        'title' => 'Mission Status',

/* Assign your controller as the handler for a front end post page */
$front_controller = MyController::create( 'front', [
    'postPage' => [
        'post_id' => MyPlugin::instance()->getSetting( 'mission_status_page_id' ),

@Static Method Reference

Controller::create( $name, $config )
 * Create Instance
 * @param       string              $name           The name of the controller
     * @param       array               $config         Configuration parameters for your controller
 * @throws      ErrorException
 * @return      Controller
public static function create( $name, $config=[] )

This method allows you to create a new named instance of your controller with the given configuration parameters. The parameters are available to your instance via the $this->config property.

Controller::get( $name )
 * Get a controller instance
 * @param   string      $name           The name used to reference the controller
 * @return Controller|NULL
public static function get( $name )

Get a previously created controller by name.

@Instance Method Reference

 * Constructor
 * @return    void
protected function constructed()   

The constructed() method on your controller instance is called when the controller is first instantiated via the factory Controller::create() method. Since controllers are created via factory methods and not instantiated directly, the constructed() method is called for you so that you can do any necessary setup of your controller instance once it is created.

The constructed() method is called on your controller just before it is registered to any admin or post pages specified in its configuration, so you can validate or change its configuration if needed from this method.

 * Initialize
 * @return    void
public function init()   

The init() method on your controller instance is called any time (and only when) your controller is being prepared to generate output. This method can be used to do common setup and validation routines that are shared between multiple do_action() output screens.

$instance->getUrl( $args )
 * Get a specific type of form
 * @param   array     $args           Optional query args to add to the url
 * @return  MWP\Framework\Helpers\Form
public function getUrl( $args=[] )

This method will return the url to the page which hosts your controller. If your controller is registered to both an admin page and a post page (front end), then the url returned will be in the context of the page currently being viewed. Either admin or front end.

The returned url will be the base url to your controller. You can specify additional parameters to add to the returned url by passing them in as $args.

Was this article helpful to you? Yes No

How can we help?