如何在laravel 4.1中的控制器中调用模块的模型和控制器文件

如何在laravel 4.1中的控制器中调用模块的模型和控制器文件

问题描述:

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?

我使用laravel 4.1创建了一个模块化系统我有一个树形图如下: p> \ ñ

 应用程序/ \ NAPP /控制器/ \ NAPP /模块/ \ NAPP /模块/模块名/ \ NAPP /模块/模块名/控制器/ \ NAPP /模块/模块名/控制器/ modulecontroller.php \ NAPP /  modules / modulename / models / 
app / modules / modulename / models / modulemodel.php 
  code>  pre> 
 
 

我想做的是从app中的控制器调用模型 / controllers / class。 我可以调用该模块及其模型吗? p> div>

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.