W3cubDocs

/Phalcon 3

Understanding How Phalcon Applications Work

If you’ve been following the tutorial or have generated the code using Phalcon Devtools, you may recognize the following bootstrap file:

use Phalcon\Mvc\Application;

// Register autoloaders
// ...

// Register services
// ...

// Handle the request
$application = new Application($di);

try {
    $response = $application->handle();

    $response->send();
} catch (\Exception $e) {
    echo "Exception: ", $e->getMessage();
}

The core of all the work of the controller occurs when handle() is invoked:

$response = $application->handle();

Manual bootstrapping

If you do not wish to use Phalcon\Mvc\Application, the code above can be changed as follows:

// Get the 'router' service
$router = $di["router"];

$router->handle();

$view = $di["view"];

$dispatcher = $di["dispatcher"];

// Pass the processed router parameters to the dispatcher

$dispatcher->setControllerName(
    $router->getControllerName()
);

$dispatcher->setActionName(
    $router->getActionName()
);

$dispatcher->setParams(
    $router->getParams()
);

// Start the view
$view->start();

// Dispatch the request
$dispatcher->dispatch();

// Render the related views
$view->render(
    $dispatcher->getControllerName(),
    $dispatcher->getActionName(),
    $dispatcher->getParams()
);

// Finish the view
$view->finish();

$response = $di["response"];

// Pass the output of the view to the response
$response->setContent(
    $view->getContent()
);

// Send the response
$response->send();

The following replacement of Phalcon\Mvc\Application lacks of a view component making it suitable for Rest APIs:

use Phalcon\Http\ResponseInterface;

// Get the 'router' service
$router = $di["router"];

$router->handle();

$dispatcher = $di["dispatcher"];

// Pass the processed router parameters to the dispatcher

$dispatcher->setControllerName(
    $router->getControllerName()
);

$dispatcher->setActionName(
    $router->getActionName()
);

$dispatcher->setParams(
    $router->getParams()
);

// Dispatch the request
$dispatcher->dispatch();

// Get the returned value by the last executed action
$response = $dispatcher->getReturnedValue();

// Check if the action returned is a 'response' object
if ($response instanceof ResponseInterface) {
    // Send the response
    $response->send();
}

Yet another alternative that catch exceptions produced in the dispatcher forwarding to other actions consequently:

use Phalcon\Http\ResponseInterface;

// Get the 'router' service
$router = $di["router"];

$router->handle();

$dispatcher = $di["dispatcher"];

// Pass the processed router parameters to the dispatcher

$dispatcher->setControllerName(
    $router->getControllerName()
);

$dispatcher->setActionName(
    $router->getActionName()
);

$dispatcher->setParams(
    $router->getParams()
);

try {
    // Dispatch the request
    $dispatcher->dispatch();
} catch (Exception $e) {
    // An exception has occurred, dispatch some controller/action aimed for that

    // Pass the processed router parameters to the dispatcher
    $dispatcher->setControllerName("errors");
    $dispatcher->setActionName("action503");

    // Dispatch the request
    $dispatcher->dispatch();
}

// Get the returned value by the last executed action
$response = $dispatcher->getReturnedValue();

// Check if the action returned is a 'response' object
if ($response instanceof ResponseInterface) {
    // Send the response
    $response->send();
}

Although the above implementations are a lot more verbose than the code needed while using Phalcon\Mvc\Application, it offers an alternative in bootstrapping your application. Depending on your needs, you might want to have full control of what should be instantiated or not, or replace certain components with those of your own to extend the default functionality.

© 2011–2017 Phalcon Framework Team
Licensed under the Creative Commons Attribution License 3.0.
https://docs.phalconphp.com/en/latest/reference/applications-explained.html