2015-01-06 10:08



I have been reading a lot in the past few weeks about dependency injection, inversion, techniques, IoC and so on and so on... Although I understand the principle I can't get a few stuff so far.

Lets say we have a controller that has to do a subscription for some service. What we need is the following:

  1. Validate request (month, year, plan)
  2. Add subscription to database
  3. Add Billing invoice to database(payment information)
  4. Update user to specify that he is subscribed
  5. Process the payment
  6. Display the information to the user and redirect to success page

This is a complex task and if we have to follow the Skinny Controller and Fat Model means it will be hard. Many developers also state that if you have more then 4 dependencies you have to simplify your classes and follow the one Responsibility principle. This brings a lot of questions for me.

  • How do I proceed here?
  • How do I isolate the controller?
  • How do I make it all testable?
  • How do I pass all the dependencies from the controller/classes?
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongmufen8105 dongmufen8105 6年前

    You can make your work a lot easier using Laracasts/Commander which based on commands.

    Also checkout Jeffrey Way Commander (

    Your controller will look like:

    1. Fetch input
    2. Call validator
    3. Call $this->execute(New SubscribeUserCommand($email, $data1, $data2)) which calls SubsribeUserCommandHandler handle method. A command is a simple data object holding everything the handle method needs.
    4. Inside the command handler call any other commands to process your request
    5. Look at the result of your command handler and display the required data to the user.

    Commander also highly recommends you work with events to trigger email, or notifications...


    • Few dependencies in your controller,
    • Readable controller
    • Test each command separately

    Down: - More classes and offert required to set it up

    Hope this helps or at leasts gives you an idea how to solve your problem. I using this method in my project where users registers, receives welcome e-mail, roles... my controller is about 5 lines long the rest is inside commands and command handlers

    点赞 评论 复制链接分享