Namespaces


    Namespaces can be used to avoid class name collisions; this means that if you have two controllers in an application with the same name, a namespace can be used to differentiate them. Namespaces are also useful for creating bundles or modules.

    Using namespaces has some implications when loading the appropriate controller. To adjust the framework behavior to namespaces is necessary to perform one or all of the following tasks:

    Specify it in the routes as a separate parameter in the route’s paths:

    1. $router->add(
    2. '/admin/users/my-profile',
    3. [
    4. 'namespace' => 'Store\Admin',
    5. 'controller' => 'Users',
    6. 'action' => 'profile',
    7. ]
    8. );

    Passing it as part of the route:

    1. use Phalcon\Mvc\Dispatcher;
    2. // Registering a dispatcher
    3. $di->set(
    4. 'dispatcher',
    5. function () {
    6. $dispatcher = new Dispatcher();
    7. $dispatcher->setDefaultNamespace(
    8. 'Store\Admin\Controllers'
    9. );
    10. return $dispatcher;

    The following example shows how to implement a controller that use namespaces:

    Take the following into consideration when using models in namespaces:

    1. <?php
    2. namespace Store\Models;
    3. use Phalcon\Mvc\Model;
    4. class Robots extends Model
    5. {
    6. }

    In PHQL you must write the statements including namespaces:

    1. <?php