douqingji3026 2015-08-03 11:46
浏览 107
已采纳

如何在Laravel中避免使用HMVC设计模式?

So I have been reading through From Apprentice to Artisan by Taylor Otwell, Laravel Author

And I came across this 'mantra' : HMVC usually indicates poor design.

Which is kind of true...

Also Taylor has suggested

Feel the need to call controllers from other controllers? This is often indicative of poor application design and too much business logic in your controllers. Extract the logic into a third class that can be injected into any controller.

And I don't seem to find such way yet..

How can I avoid HMVC and extract the logici nto a third class that can be injected to any controller?

  • 写回答

1条回答 默认 最新

  • douliang1891 2015-08-03 11:46
    关注

    I came up with a neat way to do it, and it seems to have helped me speed up my workflow...

    I think this alternative I made can replace HMVC, as well as the conventional way of using controllers... as now controllers are but somehwere where our -what I called- 'motors' are injected.

    Check out my article at coderwall where I went through the whole thing.

    Read through it, and hopefully it will provide a better way of doing things, starting from the models to and finishing at controllers.

    However if you wish to proceed your own way, make sure what was required to be shared between two controllters hiarchecly gets shared in a more neater way, as taylor suggested, shared through injection.

    For instance, you are in AdminsController and you feel the need to call an action from UsersController, just make that action and its siblings into a third class, and in your AdminsController

    //AdminsController
    
     use ThirdClass;
    
    public function __construct(ThirdClass $mything)
    {
        $this->myThirdClass = $mything;
    }
    
    public function mySharedAction()
    {
        $this->myThirdClass->mySharedActionFromUsersController();
    }
    

    And like so.

    Update

    If you have gone through my article at coderwall, the one I have mentioned above, I have made a little package that generates all of the mentioned components in there.

    Check it out at github

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?