Controllers
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[@method2...]}
{controller@method} | The name of the controller class, {@method} is optional. Additional methods can be defined by adding an additional @ . |
Sample:
php ayuco create controller:MyController@print_hello_world
The example above will create the controller "MyController", located as file [path to project]/app/Controllers/MyController.php
. The class definition will look like:
namespace MyNamespace\Controllers; use WPMVC\MVC\Controller; /** * Controller will extend from base controller class. */ class MyController extends Controller { public function print_hello_world() { } }
Multiple controller methods can be defined with the command.
php ayuco create controller:MyController@init@render
The example above will create the controller "MyController" with the methods init()
and render()
:
class MyController extends Controller { public function init() { } public function render() { } }
Views
Use the $this->view
property within a controller to access views.
class MyController extends Controller { public function print_hello_world() { // Get a view $view = $this->view->get( 'hello-world' ); // Print a view $this->view->show( 'hello-world' ); } }
The framework will auto-print a returned view if the method is the handler of an action hook (add_action), for example:
class MyController extends Controller { /** * Print hello world at when woocommerce_thankyou * action hook is triggerd. * @hook woocommerce_thankyou */ public function print_hello_world() { return $this->view->get( 'shortcode.hello-world' ); } }
User
Use $this->user
property to access current logged user information.
class MyController extends Controller { public function print_hello_world() { if ( $this->user ) { $this->view->show( 'hello-world', [ 'display_name' => $this->user->display_name, 'email' => $this->user->user_email, ] ); } } }
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 metadata.
Automated controllers will look for defined metadata in the post model's aliases property, will generate a meta box based on a predefined view template and will perform save functionality, all of these without any major programmability.
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 the ModelController class.
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 meta box display and model save to enable customization, on_metabox(&$model)
and on_save(&$model)
respectively.
class MyController extends Controller { /** * Model to perform automation upon. */ protected $model = 'MyNamespaceModelsMyModel'; /** * 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 the class method on_save(&$model)
, add property $this->autosave
to the 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...] } }