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.'); } }
Thanks in advance!