Hi!
I've followed the hmvc architecture and designed a simple web application but now I have a problem:
I want to use a module in apps/frontend/components/User.php, the file is:
<?php
namespace Component;
class User extends \Phalcon\Mvc\User\Component
{
public function createSession(\Users $user)
{
$this->session->set('id', $user->id);
$this->session->set('role', $user->role);
$this->redirectDependingOnRole();
}
public function redirectDependingOnRole()
{
switch ($this->session->get('role')) {
case 'admin':
$this->response->redirect('admin/');
break;
case 'user':
$this->response->redirect('dashboard/');
break;
case 'guest':
$this->response->redirect('/');
break;
}
}
public function isOnline()
{
return $this->session->get('id') != null;
}
public function isAdmin()
{
return $this->session->get('role') == 'admin';
}
public function helloWorld()
{
echo "hello world!";
}
}
The apps/frontend/Module.php is:
<?php
namespace Frontend;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\DiInterface;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\ModuleDefinitionInterface;
class Module implements ModuleDefinitionInterface
{
/**
* Registers the module auto-loader
*/
public function registerAutoloaders(DiInterface $di = null)
{
$loader = new Loader();
$loader->registerNamespaces(array(
'Frontend\Controllers' => __DIR__ . '/controllers/',
'Frontend\Models' => __DIR__ . '/models/',
));
// Registro de componentes propios
$loader->registerClasses([
'Component\User' => __DIR__ . '/components/User.php',
'Component\Helper' => __DIR__ . '/components/Helper.php'
]);
$loader->register();
}
// more content...
Then in my frontend/controllers/IndexController.php:
<?php
namespace Frontend\Controllers;
use \Phalcon\Tag;
class IndexController extends ControllerBase
{
public function onConstruct()
{
parent::initialize();
}
public function indexAction()
{
Tag::setTitle('Home');
}
public function testAction()
{
$this->component->user->helloWorld();
}
}
The function testAction() is failing always, I don't know why.
Also I want to know a way to have a "global" components directory for be used by all the modules. Thanks.