How do you configure a router annotation with multiple modules? I tried everything that is way it always gives an error that does not recognize the controller class or controllerbase
|
Mar '17 |
4 |
804 |
0 |
Hi,
from doc
If you’re using modules in your application, it is better use the addModuleResource() method:
<?php
use Phalcon\Mvc\Router\Annotations as RouterAnnotations;
$di["router"] = function () {
// Use the annotations router
$router = new RouterAnnotations(false);
// Read the annotations from Backend\Controllers\ProductsController if the URI starts with /api/products
$router->addModuleResource("backend", "Products", "/api/products");
return $router;
};
$loader->registerNamespaces (
array (
'Models' => $config->application->modelsDir,
'Web' => $config->application->libraryDir
)
)->register ();
$di = new FactoryDefault();
$di->setShared (
'router',
function () {
$router = new RouterAnnotations( false );
$router->addModuleResource('questionary', 'Web/Questionary/index');
});
$application = new Application();
$application->registerModules(array(
'frontend' => array(
'className' => 'Web\Frontend\Module',
'path' => __DIR__ . '/../apps/modules/frontend/Module.php'
),
'home' => array(
'className' => 'Web\Home\Module',
'path' => __DIR__ . '/../apps/modules/home/Module.php'
),
'questionary' => array(
'className' => 'Web\Questionary\Module',
'path' => __DIR__ . '/../apps/modules/questionary/Module.php'
)
));
$di->setShared (
'router',
function () {
$router = new RouterAnnotations( false );
$router->addModuleResource('questionary', 'Web/Questionary/Categoria');
/* $router->setDefaults (
array (
'namespace' => 'Web\Frontend\Controllers',
'module' => 'frontend',
)
);
$router->notFound (
array (
'controller' => 'error',
'action' => 'notfound'
)
);
*/
return $router;
}
);
$application->setDI($di);
echo $application->handle()->getContent();
...
Module.php
...
public function registerAutoloaders ( DiInterface $di = null )
{
$loader = new Loader();
$loader->registerNamespaces (
array (
'Web\Questionario\Controllers' => __DIR__ . '/controllers/',
'Web\Questionario\Models' => __DIR__ . '/models/',
'Web\Questionario\Forms' => __DIR__ . '/forms/',
)
);
$loader->register ();
}
Result:
Class Web/Questionary/CategoriaController does not exist
it work withou module...
/**
* Read auto-loader
*/
$loader = new \Phalcon\Loader();
$loader->registerNamespaces (
array (
'Models' => $config->application->modelsDir ,
'Web' => $config->application->libraryDir ,
'Web\Questionary\Controllers' => $config->application->modulesDir . 'questionary/controllers/'
)
)->register ();
$di = new FactoryDefault();
$di->setShared (
'router' ,
function ()
{
$router = new RouterAnnotations( false );
$router->addResource ( 'Web\Questionary\Controllers\Categoria', '/questionary/categoria' );
return $router;
} );
$application = new Application($di);
It worked
/**
* Read auto-loader
*/
$loader = new \Phalcon\Loader();
/**
* namespace global
*/
$loader->registerNamespaces (
array (
'Models' => $config->application->modelsDir ,
'Web' => $config->application->libraryDir ,
'Web\Questionary\Controllers' => $config->application->modulesDir . 'questionary/controllers/'
)
)->register ();
$di = new FactoryDefault();
$di->setShared (
'router' ,
function ()
{
$router = new RouterAnnotations( false );
$router->addResource ( 'Web\Questionary\Controllers\Categoria', '/questionary/categoria' );
return $router;
} );
$application = new Application($di);
/**
* register modules
*/
$application->registerModules(array(
'Questionary' => array(
'className' => 'Web\Questionary\Module',
'path' => $config->application->modulesDir . 'questionary/Module.php'
)
));