Bold Skinny Controller model in CodeIgniter

I have been using CodeIgniter for a while and have a decent knowledge of MVC, PHP, etc.

However, I find it difficult to adhere to the ideals of Fat Model Skinny Controller.

I have seen a lot about this; including that pseudo-code to include in each file, but no real examples. (Please link to some articles if I missed any obvious ones!)

I find it difficult to transfer form logic to a model. For example, I use a custom library for my auth system, which has its own model. Should I then create a site user model for login? Or should I just create a site model for this? Or a shape model?

To help me, can anyone advise me on how to drop this controller? I understand that there is a lot of code, but simple pointers would be great. (Note that I just wrote this code, so it has not been reorganized much, but it should give a good example of how some of my methods get out of hand.)

public function register() { session_start(); if ($this->tf_login->logged_in()) { redirect('profile'); } if ($_GET['oauth'] == 'true') { $type = $_GET['type']; try { $token = $this->tf_login->oauth($type, '', 'email'); } catch (TFLoginCSRFMismatchException $e) { $this->tf_assets->add_data('error_message', $e->getMessage()); } catch (TFLoginOAuthErrorException $e) { $this->tf_assets->add_data('error_message', $e->getMessage()); } if ($token) { $user_details = $this->tf_login->call('https://graph.facebook.com/me?fields=email,first_name,last_name,username&access_token=' . $token); $user_details_decoded = json_decode($user_details); if ($user_details_decoded->email) { try { $id = $this->tf_login->create_user($user_details_decoded->username, md5($user_details_decoded->username . time()), $user_details_decoded->email, '', TRUE, TRUE); } catch (TFLoginUserExistsException $e) { try { if ($this->tf_login->oauth_login($type, $user_details_decoded->email, $token)) { $this->session->set_flashdata('success_message', 'You have successfully logged in.'); redirect('profile'); } else { $this->session->set_flashdata('error_message', 'An account with these details exists, but currently isn\'t synced with ' . $type . '. Please log in to sync the account.'); } } catch (Exception $e) { $this->session->set_flashdata('error_message', $e->getMessage()); } } catch (TFLoginUserNotCreated $e) { $this->tf_assets->add_data('error_message', 'You could not be registered, please try again.'); } if ($id) { $this->tf_login->add_user_meta($id, 'first_name', $user_details_decoded->first_name); $this->tf_login->add_user_meta($id, 'surname', $user_details_decoded->last_name); $this->tf_login->sync_accounts($id, $type, $token); $this->session->set_flashdata('success_message', 'Welcome ' . $this->input->post('first_name', TRUE) . ' ' . $this->input->post('surname', TRUE) . '. Your account has been sucessfully created. You will shortly receive an email with a verification link in.'); redirect('login'); } } else { $this->session->set_flash_data('error_message', 'You could not be logged in, please try again.'); } } // Redirect to clear URL redirect(current_url()); } if ($this->form_validation->run() !== FALSE) { try { $id = $this->tf_login->create_user($_POST['username'], $_POST['password'], $_POST['email'], '', FALSE); } catch (Exception $e) { $this->tf_assets->add_data('error_message', $e->getMessage()); } if ($id) { $this->tf_login->add_user_meta($id, 'first_name', $_POST['first_name']); $this->tf_login->add_user_meta($id, 'surname', $_POST['surname']); if ($this->tf_login->register_verification_email()) { $this->session->set_flashdata('success_message', 'Welcome ' . $this->input->post('first_name', TRUE) . ' ' . $this->input->post('surname', TRUE) . '. Your account has been sucessfully created. You will shortly receive an email with a verification link in.'); redirect('login'); } else { $this->tf_login->login_user($id); $this->session->set_flashdata('success_message','Your account has been sucessfully created.'); redirect('profile'); } } else { $this->tf_assets->add_data('error_message', $this->tf_login->get_errors()); } } if (validation_errors()) { $this->tf_assets->add_data('error_message', validation_errors()); } $this->tf_assets->set_content('public/register'); $this->tf_assets->add_data('page_title', "Register"); $this->tf_assets->render_layout(); } 

Thanks in advance!

+6
source share
1 answer

From what I can tell, most or all of this code belongs to the controller or component, so I don’t think your problem is related to the Model / Controller confusion.

The code is difficult to read, however, due to deep nested structures and the inability to derive specific tasks into their own methods. The main refactoring that comes in handy is creating new private methods to highlight the discrete subtasks that you perform. This has the additional important benefit of clarifying the top level structure of your current method. This way you would get something similar (just to give an example):

 public function register() { session_start(); if ($this->tf_login->logged_in()) { redirect('profile'); } if ($_GET['oauth'] == 'true') { $this->oauthRegister(); } $this->normalRegister(); } 

Similarly, the oatuhRegister and normalRegister will be broken down into smaller methods, so when you are done, each method will adhere to SRP and will probably be less than 10 lines of code. This will greatly improve the readability and usability of your code. I also recommend checking for β€œClean Code,” which gives a strong argument for shortening your method.

+3
source

Source: https://habr.com/ru/post/898020/


All Articles