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';
}
}