dty9731 2017-01-25 20:32
浏览 84
已采纳

Laravel 5.3嵌套方法中的异常处理

I just ran into a problem where i can't seem to find a proper way of handling. I know this is a stupid example, but its the fastest i could think of.

UserController

trait SaveUser;

public function example(Request $request){
    $user = $this->saveUser($request->name);
    return $user;
}

Trait SaveUser;

public function saveUser($name){

    $is_valid = $this->validateUser;
    if(!is_valid) return response()->json(['data' => null,'message' => "please enter a name for the user",],500);
    try{
        $user = new User();
        $user->name = $name;
        $user->save()

        return $user;

    }catch(\Exception $e){
        return response()->json(['data' => null,'message' => "error saving the user.",],500);
    }
}

private function validateUser($name){
    if($name == null)
        return false;

    return true;

}

Lets say i am saving a user, requesting the name from the api request. I send it to a trait because its going to be used in many places, if the user name is not valid, it returns a json response saying it needs to be set, else if an error occurs while saving the user, return a code 500 response. Now this is a simple example, on my case, i have the validate method validate many other things and each have a unique message for a response, same for attributes of the user, etc..

So pretty much my problem is, that when the try catch catches an execption or something is not valid, it returns return response()->json(['data' => null,'message' => "error saving the user.",],500); to the $user in the UserController variable. What i would like it to do is, end my code in the exception or in the return response()-> and return the response. Is there anyway i can just end the code anywhere that i have return response()->...?

I am using this in an API, this is just a quick example. Any help would be appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • doudianhuo1129 2017-01-25 21:46
    关注

    I really don't mean any offense by this but this seems to be a case where you are trying to cover up bad practices with more bad practices.

    Your first problem is trying to return responses from methods which aren't in your routes file. You can do this and it should work just fine because in your example, your just returning that response anyway which is being stored in the $user variable.

    The cover-up for this which you are asking for is how to now handle functions returning either Response objects or User objects. So you would essentially have to use instanceof to determine if this object is a Response or User. if it's a response, return it, otherwise, keep doing more work.

    This isn't terrible because you only have to add a few more conditions but I don't think it's a road you want to start going down because it has potential to snowball from there and create very un-maintainable code.

    I'd suggest you start thinking about it this way. Why is the saveUser function worried about validating the user when we already have a validateUser function. Let the function which is being called by the routes do all the "controlling" and let your other functions do just what they were intended to do and nothing else. They should basically just act as helper functions to separate out different parts of your logic.

    There are many ways to handle this but I'd suggest something like the following.

    public function example(Request $request)
    {
        //  Do all the "controlling" in this function, other functions should just be handling all the business logic.
        if (! $this->validateUser($request->name)) {
            return response()->json(['data' => null,'message' => "please enter a name for the user",],500);
        }
    
        try {
            $user = $this->saveUser($request->name);
        } catch (Exception $e) {
            return response()->json(['data' => null,'message' => "error saving the user.",],500);
        }
    
        // Here we know this will always be a User object.
        return $user;
    }
    
    public function saveUser($name)
    {
        $user = new User();
        $user->name = $name;
        $user->save();
    
        return $user;
    }
    
    private function validateUser($name)
    {
        if($name == null) {
            return false;
        }
    
        return true;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line