• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cleaning up functions in controller

#1
[eluser]Bramme[/eluser]
Hi all,

I'm still pretty new to the whole MVC approach, but with previous help from this community, I'm getting along pretty well porting a website from just Smarty to CI. Now I've stumbled on a semantics problem. Some of my functions are pretty lengthy, especially those that include some kind of form validation. Does the MVC approach dictate anything about that? Like putting stuff in a model or so?

For example, below is my email code. Could I shorten up that function by putting stuff elsewhere?

Code:
function contact () {

        $this->load->helper(array('form', 'url'));
        
        $this->load->library('validation');
        $rules['naam'] = "trim|required|htmlentities";
        $rules['email'] = "trim|required|valid_email";
        $rules['onderwerp'] = "trim|required|htmlentities";
        $rules['bericht'] = "trim|required|htmlentities";
        
        $fields['naam'] = 'naam';
        $fields['email'] = 'e-mail';
        $fields['onderwerp'] = 'onderwerp';
        $fields['bericht'] = 'bericht';

        $this->validation->set_fields($fields);
        $this->validation->set_rules($rules);
        $this->validation->set_message('required', 'Het %s veld is verplicht in te vullen.');
        $this->validation->set_message('valid_email', 'U moet een geldig e-mail adres opgeven.');
        
        $this->data['form'] = array("naam" => array("name" => "naam",
                                                    "id" => "naam"),
                                    "email" => array("name" => "email",
                                                    "id" => "email",
                                                    "size" => "35"),
                                    "onderwerp" => array("name" => "onderwerp",
                                                        "id" => "onderwerp",
                                                        "size" => "35"),
                                    "bericht" => array("name" => "bericht",
                                                        "id" => "bericht",
                                                        "cols" => "50",
                                                        "rows" => "10"),
                                    "zend" => array("name" => "submEmail",
                                                    "value" => "Verzend"));
                
        if ($this->validation->run() == FALSE)
            {
            $this->data['page'] = 'contact';
            $this->load->view('page_index', $this->data);
        }
        else
        {
            foreach($_POST as $key => $value) {
                $$key = $value;
            }
            $this->load->library('email');
            
            $config['mailtype'] = 'html';
            $this->email->initialize($config);
            
            $this->email->to('[email protected]');
            $this->email->from($email, $naam);
            $this->email->subject($onderwerp);
            $msg = "<html><head><title>$onderwerp</title></head><body>";
            $msg = "<p><u>Bericht verzonden van elsdufourmont.be:</u></p>\n";
            $msg .= $bericht;
            $msg .= "&lt;/body&gt;&lt;/html&gt;";
            

            $this->email->message($msg);
            
            if($this->email->send()) {
                $this->data['message'] = '<p>Uw bericht werd goed verzonden! Hieronder kan u het nog eens nalezen</p>';
                $this->data['message'] .= '<p><cite>'.$bericht.'</cite></p>';
            } else {
                $this->data['message'] = '<p>Uw bericht werd niet goed verzonden, gelieve rechtstreeks contact op te nemen met Els</p>';
                }
            $this->data['page'] = 'mailsent';
            $this->load->view('page_index', $this->data);
        }
    }
Don't mind the language, it's for a Dutch client.

If this is perfectly okay, just let me know Wink

#2
[eluser]wiredesignz[/eluser]
MVC doesn't dictate your content, only the duties of each component.

However, from experience, I would recommend moving the html into a view file, no matter how small it is and also move your messages into a language file.

This will allow you to add to or edit these without disturbing your controller code.

The controller's prime concern is managing the creation of your output (email and views in this case)

It would be better if it did not contain the data related to your client's application. Even the validation rules would be better moved to a config file you can load seperately as needed.

#3
[eluser]xwero[/eluser]
In this thread I changed the front controller and i've added a page to the wiki how to create your custom front controller. Piecing the information together you are able to make your controller method empty.

As wiredesingz wrote put strings in language files and put html in view files. This encourages reuse.

#4
[eluser]Bramme[/eluser]
So how would I go about sending that email then, using the HTML in a seperate file? I'm having some difficulties grasping it.
I guess I could make a email html file containing say <%message%>, read it into my controller and then replace <%message%> with my actual message... But are there any better ways?

And does anybody have an example of using a language file?

#5
[eluser]xwero[/eluser]
Code:
// language file
$lang['email_sendfrom'] = "Bericht verzonden van elsdufourmont.be";
// view
&lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;?php echo $onderwerp ?&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;
<p><u>&lt;?php echo $this->lang->line('email_sendform') ?&gt;:</u></p>&lt;?php echo $bericht ?&gt;
&lt;/body&gt;&lt;/html&gt;
// controller
$this->load->vars(array('onderwerp'=>$onderwerp,'bericht'=>$bericht));
$msg = $this->load->view('email','',TRUE);
examples say more than a thousand words Wink

#6
[eluser]Bramme[/eluser]
Why would I want to load my email message as a view? I didn't know you could declare $lang somewhere and then use $this->lang... Same principle as config file?

#7
[eluser]xwero[/eluser]
A view is nothing more than a marked-up file, you shouldn't see it as only controller method output.

The language library is loaded by default but if you have custom files you have to load then with the lang->load method. And after that yes it's similar to the config->item method.

#8
[eluser]Bramme[/eluser]
[quote author="xwero" date="1213578238"]
Code:
// language file
$lang['email_sendfrom'] = "Bericht verzonden van elsdufourmont.be";
// view
&lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;?php echo $onderwerp ?&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;
<p><u>&lt;?php echo $this->lang->line('email_sendform') ?&gt;:</u></p>&lt;?php echo $bericht ?&gt;
&lt;/body&gt;&lt;/html&gt;
// controller
$this->load->vars(array('onderwerp'=>$onderwerp,'bericht'=>$bericht));
$msg = $this->load->view('email','',TRUE);
examples say more than a thousand words Wink[/quote]

I must say, this works like a charm, but what's that third variable, the boolean, doing in $this->load->view? Couldn't find anything about it in the user guide...

#9
[eluser]Pascal Kriete[/eluser]
Check here Smile . It tells the loader to return the view as a string instead of adding it to the output buffer.

#10
[eluser]Bramme[/eluser]
[quote author="inparo" date="1213629738"]Check here Smile . It tells the loader to return the view as a string instead of adding it to the output buffer.[/quote]
Silly me, should've checked that one :p

Thanks inparo!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.