简体   繁体   中英

How do I avoid HMVC design pattern in Laravel?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM