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 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用