Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

multi module, use addModuleResource to create api do not work, error message is "class xxxxController does not exist"

my project's structure like that: public/ public/index.php ishgo/index/ ishgo/index/Module.php ishgo/index/controllers/ ishgo/index/controllers/UserController.php

I want to know why the users api do not work, always show the error message "class UserController does not exist"... please help!


public/index.php

<?php

use \Phalcon\Mvc\Router,
    \Phalcon\Mvc\Application,
    \Phalcon\DI\FactoryDefault,
    \Phalcon\Db\Adapter\Pdo\Mysql;

try {

    //define app path
    define('ROOT_PATH', dirname(__DIR__));
    define('APP_PATH', ROOT_PATH . '/ishgo');

    $di = new FactoryDefault();
    $di->set('router', function () {
        $router = new \Phalcon\Mvc\Router\Annotations();
        $router->setDefaultModule('index');
        $router->addModuleResource('index', 'User', '/api/users');

        return $router;
    });


    $di->set('db', function() {
        return new Mysql(array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => '',
            'dbname'   => 'ishgo'
        ));
    });

    //Create an application
    $application = new Application($di);

    // Register the installed modules
    $application->registerModules(
        array(
            'index' => array(
                'className' => 'Ishgo\Index\Module',
                'path'      => APP_PATH . '/index/Module.php',
            ),
            'admin' => array(
                'className' => 'Ishgo\Admin\Module',
                'path'      => APP_PATH . '/admin/Module.php',
            ),
            'work'  => array(
                'className' => 'Ishgo\Work\Module',
                'path'      => APP_PATH . '/work/Module.php',
            )
        )
    );

    //add common functions
    include APP_PATH . '/functions.php';
    include ROOT_PATH . '/vendor/autoload.php';
    session_start();
    //Handle the request
    echo $application->handle()->getContent();

} catch(\Exception $e) {
    echo $e->getMessage();
}

ishgo/index/Module.php <?php

namespace Ishgo\Index;

use Phalcon\Loader,
    Phalcon\Mvc\Dispatcher,
    Phalcon\Mvc\View,
    Phalcon\Mvc\ModuleDefinitionInterface;

class Module implements ModuleDefinitionInterface
{

    /**
     * Register a specific autoloader for the module
     */
    public function registerAutoloaders()
    {

        $loader = new Loader();

        $loader->registerNamespaces(
            array(
                'Ishgo\Index\Controllers' => APP_PATH . '/index/controllers/',
                'Ishgo\Index\Models'      => APP_PATH . '/index/models/',
            )
        );

        $loader->register();
    }

    /**
     * Register specific services for the module
     */
    public function registerServices($di)
    {

        //Registering a dispatcher
        $di->set('dispatcher', function() {
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultNamespace('Ishgo\Index\Controllers');
            return $dispatcher;
        });

        //Registering the view component
        $di->set('view', function() {
            $view = new View();
            $view->setViewsDir(APP_PATH . '/index/views/');
            return $view;
        });
    }

}

ishgo/index/controllers/UserController.php

<?php
namespace Ishgo\Index\Controllers;

/**
 * @RoutePrefix("/api/users")
 */
class UserController extends \Phalcon\Mvc\Controller
{
    /**
     * @Get("/{id:\d*}")
     */
    public function indexAction($id=0)
    {
        if (empty($id)) {
            echo 'all users';
        } else {
            echo 'user by id:' . $id;
        }

    }


    /**
     * @Post("/{id:\d+}")
     */
    public function saveAction($id)
    {
        echo 'update user by id:' . $id;
    }


    /**
     * @Put("/")
     */
    public function addAction()
    {
        echo 'add user';
    }


    /**
     * @Delete("/{id:\d+}")
     */
    public function deleteAction($id)
    {
        echo 'delete user by id:' . $id;
    }



    /**
     * @Post("/login")
     */
    public function loginAction()
    {
        echo 'user login';
    }

}


81.8k

This is likely a misconfiguration in the autoloader, try adding an events manager to the auto-loader to find out why it can't load the classes: http://docs.phalconphp.com/en/latest/reference/loader.html#autoloading-events



4.6k

I add the events manager to the Module.php/registerAutoloaders, but the file of Module.php seems not execute



4.6k

I have upload the code to the github...

code

please help me to find out the problem...



81.8k
Accepted
answer

I get it working: https://www.dropbox.com/sh/o7c30m7zx8httsa/S8HmGtKOfJ

As general, the way in which annotated routes must be loaded was changed, it needed the full class name including its namespace and the autoloader is now global so as the application can process the routes in annotations from all modules.



4.6k

thank you very much!



4.6k

hi, I find out some problems...

one: why i use http get method to visit /api/users/123 , but the deleteAction is executed... if i change the @Delete("/{id:\d+}") to @Route("/{id:\d+}", methods="DELETE") , and then it is fine!

two: the @Put("/") seems do not work, when i use http put to visit this api, i always rewrite to the default namespace/controller/action