Вопрос

I created a modular system by using laravel 4.1 I have a tree scheme as follows:

app/
app/controllers/
app/modules/
app/modules/modulename/
app/modules/modulename/controllers/
app/modules/modulename/controllers/modulecontroller.php
app/modules/modulename/models/
app/modules/modulename/models/modulemodel.php

What I want to do is to call the model from a controller in app/controllers/ class. How I can call that module and its model?

Это было полезно?

Решение

Make sure your /app/modules is added to your composer.json file's autoload : classmap and issue composer dump-autoload or php artisan dump-autoload. Then you can just create an instance like new ModuleModel or whatever name you gave your class. Though it's better to pass to your controller by dependency injection. This way your code will be easier to test because you can pass in stub data.

public function __construct(ModuleModel $module_model_instance) {
   $this->module_model_instance = $module_model_instance;
}

Другие советы

I'd rather add this as a comment but have insufficient rep.

If everything is correctly autoloaded by composer in the PSR-0 (or 4) section, then you should just be able to reference it using it's namespace?

I sometimes need to run

composer dump-autoload

to refresh the autoloaded files, especially if using vagrant.

Hope this is helpful. I'm not sure if I've fully understood your problem.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top