Phalcon interview questions

Phalcon interview questions Download Phalcon interview questions PDF

Phalcon interview questions

Below is the list of Application Events in Phalcon.

Event NameTriggered
bootExecuted when the application handles its first request
beforeStartModuleBefore initialize a module, only when modules are registered
afterStartModuleAfter initializing a module, only when modules are registered
beforeHandleRequestBefore execute the dispatch loop
afterHandleRequestAfter execute the dispatch loop

The setVar() method is used to pass data from the controller to view template in Phalcon.


Usage:- $this->view->setVar("username", $user->username);

In Phalcon you can register namespaces, prefixes, directories or classes by loader class. Below are sample usage for same.

Registering namespaces/prefixes in Phalcon

use Phalcon\Loader;
$loader = new Loader();

// Register some namespaces
$loader->registerNamespaces(
    [
       "Example\Base"    => "vendor/example/base/",
       "Example\Adapter" => "vendor/example/adapter/",
       "Example"         => "vendor/example/",
    ]
);

// Register autoloader
$loader->register();

Registering directories in Phalcon

use Phalcon\Loader;

// Creates the autoloader
$loader = new Loader();

// Register some directories
$loader->registerDirs(
    [
        "library/MyComponent/",
        "library/OtherComponent/Other/",
        "vendor/example/adapters/",
        "vendor/example/",
    ]
);

// Register autoloader
$loader->register();

Registering classes in Phalcon

use Phalcon\Loader;

// Creates the autoloader
$loader = new Loader();

// Register some classes
$loader->registerClasses(
    [
        "Some"         => "library/OtherComponent/Other/Some.php",
        "Example\Base" => "vendor/example/adapters/Example/BaseClass.php",
    ]
);

// Register autoloader
$loader->register();

Phalcon uses an independent module “Phalcon\Validation” for validating users requests. This component can be used to implement validation rules on data objects that do not belong to a model or collection.
The loosely-coupled design of this component allows you to create your own validators along with the ones provided by the framework.
The following example shows basic usage of validations in Phalcon :

<?php

use Phalcon\Forms\Element\Text;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\StringLength;

$name = new Text(
    "name"
);

$name->addValidator(
    new PresenceOf(
        [
            "message" => "The name is required",
        ]
    )
);

$name->addValidator(
    new StringLength(
        [
            "min"            => 10,
            "messageMinimum" => "The name is too short",
        ]
    )
);

$form->add($name);

Single Module Application: Single MVC applications consist of one module only. Namespaces can be used but are not necessary.

Multi-Module Application: A multi-module application uses the same document root for more than one module.