Using dependency injection over laravel facades

后端 未结 6 1374
鱼传尺愫
鱼传尺愫 2021-01-30 16:48

I have read a number of sources that hint that laravel facade\'s ultimately exist for convenience and that these classes should instead be injected to allow loose coupling. Even

6条回答
  •  甜味超标
    2021-01-30 16:50

    Well your thoughts and concerns and correct and I had them as well. There are some benefits of Facades ( I generally dont use them ), but if you do use just I would suggest using them only in the controllers, as the controllers are just entry and exit points for me at least.

    For the example you gave I'll show how I generally handle it:

    // MyServiceInterface is binded using the laravel container
    use Interfaces\MyServiceInterface;
    use Illuminate\Http\Request;
    use Illuminate\Validation\Factory as Validator;
    
    ...
    class ExampleController {
    
        protected $request;
    
        public function __constructor(Request $request) {
            // Do this if all/most your methods need the Request
            $this->request = $request;
        }
    
        public function exampleController(MyServiceInterface $my_service, Validator $validator, $user_id) 
        { 
            // I do my validation inside the service I use,
            // the controller for me is just a funnel for sending the data
            // and returning response
    
            //now I call the service, that handle the "business"
            //he makes validation and fails if data is not valid
            //or continues to return the result
    
            try {
                $viewinfo = $my_service->getViewInfo($user_id);
                return view("some_view", ['view_info'=>$viewinfo]);
            } catch (ValidationException $ex) {
                return view("another_view", ['view_info'=>$viewinfo]);
            }
        }
    }
    
    
    
    class MyService implements MyServiceInterface {
    
        protected $validator;
    
        public function __constructor(Validator $validator) {
            $this->validator = $validator;
        }
    
        public function getViewInfo($user_id, $data) 
        { 
    
            $this->validator->validate($data, $rules);
            if  ($this->validator->fails()) {
                //this is not the exact syntax, but the idea is to throw an exception
                //with the errors inside
                throw new ValidationException($this->validator);
            }
    
            echo "doing stuff here with $data";
            return "magic";
        }
    }
    

    Just remember to break your code to small individual pieces that each one handles his own responsibility. When you properly break your code, in most cases you will not have so many constructor parameters, and code will be easily testable and mocked.

    Just one last note, if you are building a small application or even a page in a huge application for example a "contact page" and "contact page submit", you can surely do everything in the controller with facades, it simply depends on the complexity of the project.

提交回复
热议问题