WordpressMVC

  1. Get Started
  2. Documentation
  3. Tutorials
  • Get Started
  • The Basics
    • Ayuco
    • Main Class
    • Configuration
  • Hooks
    • Actions
    • Filters
    • Widgets
    • Shortcodes
  • MVC
    • Models
    • Post Models
    • Option Models
    • Category Models
    • User Models
    • Views
    • Controllers
  • Assets
  • Advanced
    • Request
    • Response
    • Cache
    • Logger
    • Deployment
    • Functions
    • Add-ons

Controllers

Last updated: March 13, 2017

Controllers are created to handle any business logic and algorithms.

Storage path

Controllers are stored at [path to project]/app/Controllers/.

Ayuco Command

Create command:

php ayuco create controller:{controller@method}

Where:

  • {controller}: The name of the controller class, {@method} is optional.

Views

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

Sample
            <?php

namespace MyNamespace\Controllers;

use WPMVC\MVC\Controller;
use MyNamespace\Models\MyModel;
/**
 * My controller will use "MyModel" (specified above),
 * and will extend from base controller class.
 */
class MyController extends Controller
{
    /**
     * Returns a loaded model based on an object id.
     * @param int $object_id
     * @return object model
     */
    public function filter_object($object_id)
    {
        return MyModel::find($object_id);
    }
    /**
     * Returns a view.
     * @return string view
     */
    public function return_view()
    {
        return $this->view->get('components.my-view');
    }
    /**
     * Echo a view directly without framework's involvement.
     */
    public function show_view()
    {
        $this->view->show('components.my-view');
    }
}
        

Working with the framework

The framework will auto-echo (print) the view returned by the controller when a WordPress action hook is called. This will not happened with wordpress filters.

User

Use $this->user property to access current user information.

Automated Controller

Automated post model controllers can be created within WordPress MVC, this type of controllers include extra functionality out-of-the-box to support an easy way to implement and save post meta data.

Automated controllers will look for defined metadata in the post model's aliases property, will generate a metabox based on predefined view template and will perform save functionality, all of these without any major programability.

Automated controllers can be created by registering new post type models or by adding them manually to Main Class and creating a controller that extends ModelController class.

Manual Addition Sample
            <?php

class Main extends Bridge
{
    public function init()
    {
        // Adds model by name of the class model.
        // In this case, the model class is "MyPostModel"
        $this->add_model('MyPostModel');
    }
}
        

Internal class methods are called before metabox display and model save to enable customization, on_metabox(&$model) and on_save(&$model) respectively.

Trigger Methods Sample
            <?php

namespace MyNamespace\Controllers;

use WPMVC\Request;
use WPMVC\Controllers\ModelController as Controller;

class MyController extends Controller
{
    /**
     * Model to perform automation upon.
     */
    protected $model = 'MyNamespace\Models\MyModel';
    /**
     * Called before metabox is created.
     * Can be used to modify content before it is displayed on a view.
     * @param object &$model
     */
    public function on_metabox(&$model)
    {
        if ($model->css === 'white')
            $model->css = '#fff';
    }
    /**
     * Called before model is saved.
     * @param object &$model
     */
    public function on_save(&$model)
    {
        if (Request::input('force_trash', false))
            $model->post_status = 'trash';
    }
}
        

To enable WordPress autosave functionality during class method  on_save(&$model), add property $this->autosave to the controller.

Enable autosave Sample
            <?php
namespace MyNamespace\Controllers;

use WPMVC\Controllers\ModelController as Controller;

class MyController extends Controller
{
    /**
     * Enables controller to save during Wordpress autosave.
     * @var bool
     */
    protected $autosave = true;

    /**
     * Called before model is saved.
     * Now called when model is manually
     * saved (user action by clicking the "Update" button) or when Wordpress
     * performs autosave.
     */
    public function on_save( &$model )
    {
        // TODO [code...]
    }
}
        

 

© 2019 10 Quality Studio . All rights reserved.