dpea85385
2014-05-07 07:42
浏览 56
已采纳

如何在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?

图片转代码服务由CSDN问答提供 功能建议

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

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

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dshu1235 2014-05-07 07:53
    已采纳

    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;
    }
    
    打赏 评论
  • doutui6241 2014-05-07 07:52

    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.

    打赏 评论

相关推荐 更多相似问题