0
votes

I'm trying to implement phalcon multi module with namespace. normally its working. but models not loading from its location(/apps/models/). if I paste all of my models file into controller dir then its working. It should load from models dir. how could i solve this problem.

[Front Module]

$loader->registerNamespaces(
array(
'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
'Multiple\Frontend\Models'      => '../apps/frontend/models/',
 ));

[Blogs Model]

namespace Multiple\Frontend\Controllers;
use Phalcon\Mvc\Model;
class Blogs extends Model{}    

i also try "namespace Multiple\Frontend\Models;" but not success. getting error like:

Fatal error: Uncaught Error: Class 'Multiple\Frontend\Controllers\News' not found in C:\xampp\htdocs\pm\apps\frontend\controllers\IndexController.php:38 Stack trace: #0 [internal function]:

i have my dispatcher like:

public function registerServices(DiInterface $di)
{
    # Registering a dispatcher
    $di->set('dispatcher', function () {
        $dispatcher = new Dispatcher();
        $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers");
        return $dispatcher;
});

i think the error : "Error: Class 'Multiple\Frontend\Controllers\Blogs' not found" for this cause default namespace is frontend\controller. how to solve it? please

2
Why your Blog model is namespaced as Controller? Whats up with News in your error, its nowhere in your code sample? - Nikolay Mihaylov
What namespace should i use in blog model? is it "namespace Multiple\Frontend\Models"? if yes then it show fatal error news model is in models directory thats why this error. if i copy news model to controller dir it work perfect - baby machinery
i want to call news model from models directory. - baby machinery
Did you forget to change the namespace in your news model to Multiple\Frontend\Models? - Timothy
Can you confirm that every single one of your models has the namespace Multiple\Frontend\Models. Can you triple check this please? - Timothy

2 Answers

2
votes

You need to load your models obviously outside of modules. The registerNamespaces is only hit in this module when this module is hitted by dispatcher.

Actually i thought that you have problems with using models in different modules. If you have this error Multiple\Frontend\Controllers\News that this can't be found it means that you just don't have proper use statement and it's looking for class in same namespaces, just add use Multiple\Frontend\Models\News. Are you even using any IDE ?

0
votes

I think you need to add one extra line in your controller like...

namespace Multiple\Frontend\Controllers;
use Phalcon\Mvc\Controller;

use Multiple\Frontend\Models\Blogs as Blogs; //** This line should Add **//

class IndexController extends Controller
{
public function indexAction()
{}
}