Welcome Guest, Not a member yet? Register   Sign In
Post your controller!
#25

[eluser]Nick Husher[/eluser]
@stuffradio:

Suggestion: rather than have a lot of if-else blocks and stringy content data in your controller, you could push the content into a config file and access them using the third url segment as an index. For example:

Code:
// controller.php

// get the segment string once, so we don't need to retrieve it many times
$action = $this->uri->segment(3);

// the segment function returns false if the segment doesn't exist
// set a default value in that case.
if($action === false) { $action = 'default'; }

// load the messages config file into the messages namespace in our controller
$this->config->load('messages', true);
$data['success'] = $this->config->item($action + '_success', 'messages');
$data['error'] = $this->config->item($action + '_error', 'messages');


// messages.php
$config['default_success'] = '';
$config['default_error'] = '';

$config['userexist_success'] = '';
$config['userexist_error'] = 'Username or password doesn't match or username doesn't exist.'

$config['verified_success'] = 'You have successfully verified your account! You may now login.';
$config['verified_error'] = '';

It's generally considered good style to minimize extensive if-else blocks and case statements if you can. Long if-else blocks can make it more difficult to read your program, and it also affects extensibility: if ever you wanted to add more messages based on that URL fragment, you'd have to add another block to the end. Your controller could quickly become huge and unwieldy, when the above method will always have the same number of lines in your controller. At the very least, bind the segment string to a variable at the top of your code (i.e. $action = $this->uri->segment(3)), so if you ever need to change which segment it is, you only have to change it in one place.


Messages In This Thread
Post your controller! - by El Forum - 08-30-2009, 12:19 AM
Post your controller! - by El Forum - 08-30-2009, 01:36 AM
Post your controller! - by El Forum - 08-30-2009, 07:19 AM
Post your controller! - by El Forum - 08-30-2009, 12:31 PM
Post your controller! - by El Forum - 08-30-2009, 12:44 PM
Post your controller! - by El Forum - 08-30-2009, 08:59 PM
Post your controller! - by El Forum - 08-30-2009, 10:07 PM
Post your controller! - by El Forum - 08-31-2009, 02:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:19 AM
Post your controller! - by El Forum - 08-31-2009, 04:43 AM
Post your controller! - by El Forum - 08-31-2009, 04:49 AM
Post your controller! - by El Forum - 09-01-2009, 01:05 AM
Post your controller! - by El Forum - 09-01-2009, 09:30 PM
Post your controller! - by El Forum - 09-02-2009, 02:42 AM
Post your controller! - by El Forum - 09-02-2009, 05:34 AM
Post your controller! - by El Forum - 09-02-2009, 09:02 AM
Post your controller! - by El Forum - 09-02-2009, 09:43 AM
Post your controller! - by El Forum - 09-02-2009, 10:13 AM
Post your controller! - by El Forum - 09-02-2009, 01:13 PM
Post your controller! - by El Forum - 09-03-2009, 06:40 AM
Post your controller! - by El Forum - 11-03-2009, 10:20 PM
Post your controller! - by El Forum - 11-03-2009, 11:02 PM
Post your controller! - by El Forum - 11-03-2009, 11:33 PM
Post your controller! - by El Forum - 11-04-2009, 12:09 AM
Post your controller! - by El Forum - 11-04-2009, 03:36 AM
Post your controller! - by El Forum - 11-04-2009, 07:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB