W3cubDocs

/Phalcon 3

Working with 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.

Setting up the framework

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:

Use an autoload strategy that takes into account the namespaces, for example with Phalcon\Loader:

$loader->registerNamespaces(
    [
       "Store\\Admin\\Controllers" => "../bundles/admin/controllers/",
       "Store\\Admin\\Models"      => "../bundles/admin/models/",
    ]
);

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

$router->add(
    "/admin/users/my-profile",
    [
        "namespace"  => "Store\\Admin",
        "controller" => "Users",
        "action"     => "profile",
    ]
);

Passing it as part of the route:

$router->add(
    "/:namespace/admin/users/my-profile",
    [
        "namespace"  => 1,
        "controller" => "Users",
        "action"     => "profile",
    ]
);

If you are only working with the same namespace for every controller in your application, then you can define a default namespace in the Dispatcher, by doing this, you don’t need to specify a full class name in the router path:

use Phalcon\Mvc\Dispatcher;

// Registering a dispatcher
$di->set(
    "dispatcher",
    function () {
        $dispatcher = new Dispatcher();

        $dispatcher->setDefaultNamespace(
            "Store\\Admin\\Controllers"
        );

        return $dispatcher;
    }
);

Controllers in Namespaces

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

namespace Store\Admin\Controllers;

use Phalcon\Mvc\Controller;

class UsersController extends Controller
{
    public function indexAction()
    {

    }

    public function profileAction()
    {

    }
}

Models in Namespaces

Take the following into consideration when using models in namespaces:

namespace Store\Models;

use Phalcon\Mvc\Model;

class Robots extends Model
{

}

If models have relationships they must include the namespace too:

namespace Store\Models;

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public function initialize()
    {
        $this->hasMany(
            "id",
            "Store\\Models\\Parts",
            "robots_id",
            [
                "alias" => "parts",
            ]
        );
    }
}

In PHQL you must write the statements including namespaces:

$phql = "SELECT r.* FROM Store\Models\Robots r JOIN Store\Models\Parts p";

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