Routes and Controllers

The controller class

You must create your controller following two rules: The name of your controller class must have the "Controller" suffix: [className]Controller The Class must extends the abstract Simettric\Sense\Controller\AbstractController class

use Simettric\Sense\Annotations\Route;
use Simettric\Sense\Controller\AbstractController;

class YourController extends AbstractController{

    /**
     * @Route("/demo/", name="test_route")
     */
    function demoAction(Request $request, WP_Query $query) {

        [...]

    }

}

If you want to use namespaces in your controller you should modify you composer.json to autoload your namespaces. For example, in compose.json add:

"extra": {
    ...
},
"autoload": {
    "psr-4": {"MyCompany\\myPlugin\\": "public/wp-content/plugins/my-cool-plugin/"}
}

Routes

Sense uses Doctrine Annotations for the route configuration.

All the routes needs a "name" and a path. Optionally, you can define params in the url path in this way:

    use Simettric\Sense\Annotations\Route;

    /**
     * @Route("/demo/{name}", name="test_route")
     */
    function demoAction($name, Request $request, WP_Query $query) {

        [...]

Also, you can specify the Method and regexp requirements for your route params:

    /**
     * @Route("/demo/{name}", requirements="{$name=\w+}", method="GET", name="test_route")
     */
    function demoAction($name, Request $request, WP_Query $query) {

        [...]

Generating a route in your controller

You can generate a route in your controller with the generateUrl() method:

function profileAction() {

   $params = array("id"=>get_current_user_id());
   $url    = $this->generateUrl("your_profile", $params);

   [...]
}

ActionResult

Your Controller always needs to return an ActionResult object.

Sense implements two types of ActionResult:

WPTemplateActionResult

With this action result, you can specify the template file to show for the action and optionally pass the available parameters in the View.

function aboutAction() {

   $params = array("param"=>"value");
   return $this->resultTemplate("Company/about.php", $params);
}

HTTPResponseActionResult

With this action result, you can specify a Symfony HTTP Foundation Response object that Sense will use as response.

function aboutAction() {

   return $this->resultResponse(new JSONResponse($data));
}

// there is an alternative way to return a Response object.
function aboutAction() {

   return $this->resultResponse("content", $code=200, $headers=array());
}

function aboutAction() {

   return $this->resultRedirect("url", $code=301, $headers=array());
}

Note: for RedirectResponse and JSONResponse objects, Sense will translate them to wp_redirect and wp_json functions in order to maintain compatibility with other WordPress Plugins.

Define your own

You can define your own ActionResult class implementing the ActionResultInterface

Templates

Sense uses the WordPress template system. You can create templates those works in the same way than the WordPress ones.

//your-plugin-path/Views/Folder/template_name.php     
<?php

    get_header();
?>

<h1>Your view title</h1>

Your view html.

<?php

    get_footer();

If you want to access to a View parameter, you need to use the senseView() function.

Note: sense_view() works as a helper to \Simettric\Sense\Kernel::getInstance()->getContainer()->get("view")

<?php
    $user = sense_view()->get("user");
    //you can use \Simettric\Sense\Kernel::getInstance()->getContainer()->get("view")->get("user") too
?>

<h1><?php echo $user->getName() ?></h1>

Also you can use the sense_url in order to generate routes in your views:

<a href="<?php echo sense_url("route_name", ["param"=>"value"], $absolute=true) ?>">link</a>

Assets and metas

You can define your html title and description in your controller:

function aboutAction() {

   $this->setTitle("My page title");
   $this->setDescription("My page description");

   return $this->resultTemplate("Company/about.php");
}

Also, you can add additional scripts and styles.

function aboutAction() {

    $this->addScript("custom_js", "/url_to_js/script.js", $version="1.1", $deps=array("jquery", "bootstrap"), $footer=true);

    $this->addStyle("custom_css", "/url_to_css/custom.css", $version="1.1", $deps=array("bootstrap"));
}

These helpers use the WordPress Asset System behind the scenes. Consult the documentation in order to know how the deps parameters work.

Admin Controllers

Sometimes, you need a controller for your WPAdmin.

You can create a Controller class in order to create a new admin menu page in your wp-admin.

use Simettric\Sense\Annotations\AdminRoute;

class DemoController extends AbstractAdminController
{

    /**
     * @AdminRoute("/demo/", name="test_route")
     */
    function demoAction()
    {
        return $this->resultTemplate('demo.php');
    }

}

This will create a new admin menu in your wp-admin and loads the contents in your AdminView/demo.php template file.