0

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

  • 1
    Why your Blog model is namespaced as Controller? Whats up with News in your error, its nowhere in your code sample? – Nikolay Mihaylov Aug 13 '16 at 07:10
  • 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 Aug 13 '16 at 07:15
  • i want to call news model from models directory. – baby machinery Aug 13 '16 at 07:15
  • 1
    Did you forget to change the namespace in your `news` model to `Multiple\Frontend\Models`? – Timothy Aug 13 '16 at 09:05
  • Timothy! If i change Multiple\Frontend\Models then i got Fatal error like: Error: Class 'Multiple\Frontend\Controllers\News' not found. its only work if i paste all models into my controller dir – baby machinery Aug 13 '16 at 10:35
  • 1
    Can you confirm that **every single one of your models** has the namespace `Multiple\Frontend\Models`. Can you **triple** check this please? – Timothy Aug 13 '16 at 10:52
  • Yes! Timothy. 100% Confirm – baby machinery Aug 13 '16 at 10:53
  • is this lines causing any problem: public function registerServices(DiInterface $di) { $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers"); return $dispatcher; }); please? – baby machinery Aug 13 '16 at 10:57
  • No, that line tells your dispatcher where it must start looking for a matching controller. – Timothy Aug 13 '16 at 11:04
  • i'm just following multi module from https://github.com/phalcon/mvc/tree/master/multiple. just trying to use model. i didn't change anything from the example. i cant find out my wrong – baby machinery Aug 13 '16 at 11:06

2 Answers2

2

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 ?

Juri
  • 1,369
  • 10
  • 16
  • 1
    What you mean no working ? Just use loader and load your model classes before registering modules, here example - http://pastebin.com/CgzT7mGc And then just `use Multiple\Frontend\Models\Blogs` or whatever. It has to work, im doing the same in my application. **As i already wrote, registerNamespaces is only hit when the dispatcher hit a module(like route) - this is why example works without any problem. If you want to use your models in multiple modules - you need to register them with loader outside of module class.** – Juri Aug 13 '16 at 14:11
  • 1
    @Juri, he uses Sublime Text, so no IDE :) – Timothy Aug 13 '16 at 17:33
  • So i updated my answer. It's definitely beacause he doesn't have use i think. – Juri Aug 13 '16 at 21:44
  • Juri! Timothy is right i use sublime text. and look my code on top i already do that after register namespace for controller and model both after application $di. Im asking why i need every time to add models in my controller? – baby machinery Aug 14 '16 at 04:55
  • 1
    What you mean to add models in your controllers ? This is how php works. You need to use `use class` if it's in another namespace. – Juri Aug 15 '16 at 10:14
  • Thanx! Juri 4 explain. but its like same to copy models in controller dir or tell controller where is the models dir – baby machinery Aug 16 '16 at 07:07
  • No, you just need to tell controller from which namespaces got your model :) If you would have no namespaces then this would already work. If model is in `Multiple\Frontend\Models` and controller in `Multiple\Frontend\Controllers` to use models in controller you need to use full namespaces like `\Multiple\Frontend\Models\News` or put `use Multiple\Frontend\Models\News` and then you can use just News anywhere. – Juri Aug 18 '16 at 06:34
0

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()
{}
}
munaz
  • 166
  • 2
  • 14
  • Gr8 Boss! its working... But i have a question that if i use multi module then every controller i should add models is that? – baby machinery Aug 14 '16 at 04:52
  • i don't know but i just solved my problem like this. ask some one more expert then me like Timothy. Thanx! – munaz Aug 14 '16 at 06:14