Laravel 4代码组织

时间:2014-03-17 22:07:35

标签: laravel-4 code-organization

我对Laravel 4代码组织有一些疑问。我不是最好的“干净的编码器”,来自Java世界,有时我的PHP / Laravel 4代码看起来很糟糕。我在我的控制器上发布了一个例子:

public function postCreate()
{

    $input = array(
        'title' => Binput::json('title'),
        'gender' => Binput::json('gender'),
        'first' => Binput::json('first'),
        'last' => Binput::json('last'),
        'birthdate' => Binput::json('birthdate'),
        'birthplace' => Binput::json('birthplace'),
        'citizenship' => Binput::json('citizenship'),
        'organizationId' => Binput::json('organizationId'),
        'typeId' => Binput::json('typeId'),
        'email' => Binput::json('email'),
        'phone_private' => Binput::json('phone_private'),
        'phone_mobile' => Binput::json('phone_mobile'),
        'address_street' => Binput::json('address.street'),
        'address_postcode' => Binput::json('address.postcode'),
        'address_city' => Binput::json('address.city'),
        'address_country' => Binput::json('address.country'),
        'educations' => Binput::json('educations'),
        'selectedLanguages' => Binput::json('selectedLanguages'),
        'work' => Binput::json('work'),
    );

    $rules = array (
        'gender' => 'required|max:1',
        'first' => 'required|min:2',
        'last' => 'required|min:2',
        'birthdate' => 'required',
        'organizationId' => 'required',
        'typeId' => 'required',
        'email' => 'required|email',
    );

    $v = Validator::make($input, $rules);

    if ($v->fails() || empty($input['educations']))
    {
        $data = array("flash" => 'Firstname, Lastname, Birthdate, Email and at least 1 entry in Educations required.');
        return Response::json($data, 500);
    }

    try {
        DB::connection()->getPdo()->beginTransaction();

        $member = new Member();

        $member->title = $input['title'];
        $member->gender = $input['gender'];
        $member->first = $input['first'];
        $member->last = $input['last'];
        $member->birthdate = $input['birthdate'];
        $member->birthplace = $input['birthplace'];
        $member->citizenship = $input['citizenship'];

        $work = new Work();
        $work->working = $input['work']['working'];
        if($input['work']['working'] == 1){
            $work->branch = $input['work']['branch'];
            $work->company = $input['work']['company'];
        }
        $work->save();
        $member->work()->associate($work);
        $member->save();

        foreach($input['educations'] as $eduInput){
            $edu = new Education();
            $edu->degree = $eduInput['degree'];
            if(!empty($eduInput['course'])){
                $edu->course = $eduInput['course'];
            }
            $edu->term = $eduInput['term'];
            $edu->completion = $eduInput['completion'];

            if(!empty($eduInput['faculty'])){
                try{
                    $faculty = Faculty::findOrFail($eduInput['faculty']['id']);
                    $edu->faculty()->associate($faculty);
                }catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e){

                }
            }

            if($eduInput['institutionId'] == 0){
                // University
                try{
                    $university = University::findOrFail($eduInput['university']['id']);
                    $edu->university()->associate($university);
                }catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e){

                }
            }else{
                // Freetext
                $edu->institution = $eduInput['institution'];
            }

            $edu->save();
            $member->educations()->save($edu);
        }

        foreach($input['selectedLanguages'] as $languageInput){

            try{
                $lang = Language::findOrFail($languageInput['id']);
                $member->languages()->attach($lang);
            }catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e){

            }
        }

        try{
            $memberType = MemberType::findOrFail($input['typeId']);
            $member->memberType()->associate($memberType);
        }catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e){

        }

        try{
            $organization = Organization::findOrFail($input['organizationId']);
            $member->organizations()->attach($organization);
        }catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e){

        }

        $email = new Email();
        $email->email = $input['email'];
        $email->primary = true;
        $member->emails()->save($email);

        // If input for phone is empty
        $phone = new Phone();
        $phone->phone = $input['phone_private'];
        $phone->phoneType()->associate(PhoneType::find(PhoneType::PRIVATE_PHONE));
        $member->phones()->save($phone);

        $phone = new Phone();
        $phone->phone = $input['phone_mobile'];
        $phone->phoneType()->associate(PhoneType::find(PhoneType::MOBILE_PHONE));
        $member->phones()->save($phone);

        $address = new Address();
        $address->street = $input['address_street'];
        $address->postcode = $input['address_postcode'];
        $address->city = $input['address_city'];
        $address->country = $input['address_country'];
        $address->member()->associate($member);
        $address->save();

        DB::connection()->getPdo()->commit();
    }catch (\PDOException $e) {
        DB::connection()->getPdo()->rollBack();
        return Response::json("Error while writing to database.", 500);
    }

    $member->load('emails');

    $data = array("flash" => 'Member created successfully.');
    return Response::json($data, 200);
}

这是我控制器的一个例子。

  1. 以这种方式获取所有参数是否正常。这需要很大的空间。
  2. 我可以将数据库事务移到别处而不是存储在控制器中吗?
  3. 一般在哪里存储管理逻辑的代码?在控制器?在里面 模特?

1 个答案:

答案 0 :(得分:2)

您的控制器操作只是一种中间件,在某种意义上,您不应该放置任何业务逻辑。我可以提供一些指示:

  1. 您可以使用Input::json()->all()获取所有json输入,该输入返回一个数组,以便您可以操作它。
  2. 验证规则是另一项责任,因此它应该在您从控制器调用的另一个类中进行抽象,它也可能非常适合您的模型(或实体)。
  3. 为了帮助您了解如何在控制器中使用其他类,您应该在laravel文档中查找依赖项注入。
  4. 如果您可以通过Laravel的创建者访问本书https://leanpub.com/laravel,那么即使在laravel之外,它也有助于您理解代码组织和班级责任