Controller API

The following is the list of available tools provided by the Controller class. More in-depth information is found on the Controller API page.

Public attributes

$this->request

Holds a reference to the request that is currently active in the process. It can be used to see information on the current page state. See Handling requests for additional information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
namespace App\Controller;

class MyController extends AppController {

    // Inherited
    // public $request;

    public function index()
    {
        $this->request->hasGet("foo");
        $this->request->post("bar");
    }
}
?>

$this->view

Holds a reference to the active View object. See Sending variables to views for additional information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
namespace App\Controller;

class MyController extends AppController {

    // Inherited
    // public $view;

    public function index()
    {
        $this->view->check("foo");
        $this->view->get("bar");
        $this->view->set("raynors", "raiders");
        $this->view->loadHelper("Form");
    }
}
?>

$this->shortcodes

Allows automatic creation of shortcodes within the scope of the Controller. See Automatic shortcodes for additional information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace App\Controller;

use App\Model\ForumPost;

class ForumController extends AppController {

    public $shortcodes = array(
        "app\_post_score"        => "scoringWidget",
    );

    public function scoringWidget()
    {
        $reply = get_post(123);
        $this->view->set("reply", new ForumPost::getEntity($reply));

        return $this->view->loadTemplate("forums/scoreWidget");
    }
}
?>

$this->helpers

Allows automatic instantiation of Helpers within the scope of the Controller. See Helpers for additional information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
namespace App\Controller;

class AppController extends AppController {

    public $helpers = array(
        "Acf" => array("name" => "Acf"),
        "I18n" => array("name" => "i18n"),
        "Form",
        "Youtube",
        "Gtm"
    );

    public function before()
    {
        $this->view->loadHelper("Category");
    }
}
?>

Public methods

init()

Initiates the Controller object by setting up the Request, the associated View and various autoloaders.

init() is invoked each time a route requests a Controller and it is the first method that gets called. While its acceptable to set configurations in this method, it is more elegant to place it in the before() method that get called just after in the call stack.

Make sure to call parent::init(); if you need to plug into it as it would prevent the object from building properly.

after()

Executed after each calls to a Controller’s action. Recommended area for placing cleanup functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
namespace App\Controller;

use Strata\Strata;

class AppController extends AppController {

    public function after()
    {
        Strata::log("this is done", "doneLogger");
    }
}
?>

before()

Executed before each calls to a Controller’s action. Recommended area for initiating objects and running code that is common across a Controller’s methods.

1
2
3
4
5
6
7
8
9
10
11
<?php
namespace App\Controller;

class EmailController extends AppController {

    public function before()
    {
        $this->view->setConfig("layout", "layout/email-layout");
    }
}
?>

before()

Base action when no action is found. This is used mainly as a precautionary fallback when a route matches a controller but not a method.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
namespace App\Controller;

class MyController extends AppController {

    public function noActionMatch()
    {

    }

}
?>

notFound()

Applies a valid 404 status to the current Request.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
namespace App\Controller;

class MyController extends AppController {

    public function show($postID)
    {
        $post = get_post($postID);
        if (!$post) {
            return $this->notFound();
        }

    }

}
?>

serverError()

Applies a valid 500 status to the current Request.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
namespace App\Controller;

class MyController extends AppController {

    private $featureIsReady = false;

    public function show($postID)
    {
        if (!$this->featureIsReady) {
            return $this->serverError();
        }
    }

}
?>

ok()

Applies a valid 200 status to the current Request.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace App\Controller;

class MyController extends AppController {

    public function notFoundOverride($postID)
    {
        if (is_404()) {
            $this->ok();
        }
    }

}
?>

redirect($controllerName, $action = “index”, $arguments = array())

Redirects a Controller call to another during a same route.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
namespace App\Controller;

class MyController extends AppController {

    use ContactTrait;

    public function show($slug)
    {

    }

    public function saveContact($slug)
    {
        if ($this->request->isPost()) {
            $this->attemptContactFormSave();
            return $this->redirect("MyController", "show", array($productLineSlug));
        }

        $this->notFound();
    }

}
?>