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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度