We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Using composer packages as contollers and models

Hello. I currently have a package built in psr4 style.

If I autoload the package and take out the defined namespace in that package I can $loader->registerDirs() and point to the src directory and Phalcon recognizes the controller.

<?php
   $loader = new Loader();
    $loader->registerDirs(array(
        __DIR__.'/controllers/',
        __DIR__.'/models/',
        __DIR__.'/../packages/appstack/robots/src'
    ))->register();

If I put the namesspace back, it no longer finds the controller.

<?php

namespace MyPackage\Robots;

use \Phalcon\Mvc\Controller;

class RobotController extends Controller
{

I want to be able to create /packages/myapp/blog/BlogController.php and /packages/myapp/store/StoreController.php that are autoload via composer and recognized by Phalcon to find the controllers and models within



1.7k

I think, you should use $loader->registerNamespaces() instead of $loader->registerDirs(). https://docs.phalcon.io/en/latest/api/Phalcon_Loader.html

edited Aug '16

Try to use this im using it on my micro rest api

        $loader = new Loader();
        $loader->registerNamespaces(array(
          'Controllers' => __DIR__.'/controllers/',
          'Models' => __DIR__.'/models/',
          'MyPackage\Robots' => __DIR__.'/../packages/appstack/robots/src'
        ))->register();
edited Jul '16

Ok so the combination of registerNameSpaces and Router works!! I could accomplish multiple namespaces and packages

If I use DefaultNameSpace on dispatcher it locks it into one

Not including the route and defaultnamespace it doesn't recognize its existence even tho I am vendor autoloading and ptinting to directory.

    $loader->registerNamespaces(array(
            'MyApp\Controllers' => __DIR__.'/controllers/',
            'MyApp\Models'      => __DIR__.'/models/',
            'MyApp\Robots\Controllers' => __DIR__.'/../packages/myapp/robots/src/controllers/',
            'MyApp\Robots\Models' => __DIR__.'/../packages/myapp/robots/src/models/'
        ));

and

$this->router->add(
            "/robot",
            array(
                "controller" => "\MyApp\Robots\Controllers\Robot",
                "action"     => "index"
            )
        );

I was hoping I didn't have to do routes, I just wanted it to know to check all my directories to look for the namespace (what composer autoload does)

edited Jul '16

I guess until it will look for the controller automatically i will run this for dev :)

  // WILDCARD ROUTE SYSTEM
        $this->router->add("/:controller",array("namespace" => "\MyApp\Robots\Controllers","controller" => 1));
        $this->router->add("/:controller/:action",array("namespace" => "\MyApp\Robots\Controllers","controller" => 1,"action" => 2));
        $this->router->add("/:controller/:action/:params",array("namespace" => "\MyApp\Robots\Controllers","controller" => 1,"action" => 2,"params"    => 3));
edited Jul '16

You can work as well with defualt behaviour router without need to define your actions. You just need to set setDefaultNamespace.

edited Jul '16

You can work as well with defualt behaviour router without need to define your actions. You just need to set setDefaultNamespace.

That is what I was having trouble getting to happen. It would continually say \MyApp\Robots\RobotController wasnt found unless I slapped the route in there... maybe I just didn't map it right.

Route shouldn't be necessary as long you are using Router without false in constructor.