Helpers

Phlyty ships with a number of built-in helper methods in the Phlyty\App class. These fall under roughly four categories:

  • Workflow-related helpers (halt, stop, pass, redirect, events, event, trigger, getLog)
  • HTTP-related helpers (request, response)
  • Route-related helpers (params, urlFor)
  • View-related helpers (view, viewModel, render, flash)

Workflow Helpers

Workflow helpers shape the flow of the application. They allow you to return from execution early, either because the response is ready, or because we know an error condition has occurred; redirect to another URI; pass on execution to another route and controller; or work with events.

halt($status, $message='')

Halts execution immediately, setting the response status to $status, and, if $message is provided, setting the response body to that message. No further code will be executed in the controller following this call.

$name = $app->params('name', false);
if (!$name) {
    $app->halt(500, 'Missing name; cannot continue execution');
}
// do something with $name now...
stop()

Halts execution, sending the response as it currently exists. You might call this if you wanted to return a file download, for instance.

$image = $app->params('image', false);
if ($image && file_exists($image)) {
    $stream = fopen($image, 'r');
    $out    = fopen('php://output', 'w');
    stream_copy_to_stream($stream, $out);
    $app->response()->setBody($out);
    $app->stop();
}
// show some error message here...
pass()

Tells the application that no more processing of this controller should be done, but that it should continue iterating through routes to look for another one that matches the current URI.

$app->get('/:locale', function ($app) {
    $locale = $app->params()->getParam('locale', 'en_US');
    Locale::setDefault($locale);
    $app->pass();
});

$app->get('/[:locale]', function ($app) {
    // This matches the previous route, which means when pass() is
    // called by the previous controller, this route will be matched
    // and this controller invoked.
    //
    // Display home page
});
redirect($url, $status = 302)

Sets the response status code to $status and the Location header to $url, and immediately halts execution and sends the response. Any code following the call in the controller will not be executed.

$app->get('/user/:username', function ($app) {
    $username = $app->params()->getParam('username', false);
    if (!$username) {
        $this->redirect('/login');
    }
    // Code below here will only execute if we did not redirect
});
events()

Returns a Zend\EventManager\EventManager instance. This allows you to attach event listeners as well as trigger events. See the section on events for more information.

$app->events()->attach('route', function ($e) use ($app) {
    $route = $e->getRoute();
    if (!in_array($route->getName(), ['profile', 'comment', 'post']) {
        return;
    }

    // check if we have an authenticated user, and throw an exception
    // otherwise
    // ...
}, -10); // registering to execute after routing finishes
event()
Returns a new Phlyty\AppEvent instance with the target set to the Phlyty\App instance, and the route populated with the currently matched route.
trigger($name, array $params = [])

Trigger the named event, optionally passing parameters to compose in the Phlyty\\AppEvent instance.

$app->get('/', function ($app) {
    $app->trigger('homepage', $app->params()->getParams());
});
getLog()

Gets the currently registered Zend\Log\Logger instance, lazy-loading one if none is present. You will need to attach writers to the log instance, and then invoke one or more logging methods.

$logger = $app->getLog()
$logger->addWriter('stream', [
    'stream'        => 'php://stderr',
    'log_separator' => "\n",
]);
$logger->info('This is an informational message');