Welcome Guest, Not a member yet? Register   Sign In
Several questions concerning CI4 controller
#1

Here the (working) class:

PHP Code:
<?php namespace App\Controllers;
 
use 
CodeIgniter\Controller;
use 
App\Models\ContactModel;
 
class 
Contact extends Controller
{
    public function __construct() {
        helper('html''url''form');
    }

    public function index()
    {
        $data = [
            'title'  => 'Contact',
            'src'    => 'images/logo.jpg',
            'alt'    => 'site dot net site logo',
            'heading' => 'Contact',
            'success' => 'Thank you for contacting us - we will respond to you shortly'
        ];

        return view('contact/contact'$data);
    }
 
    public function dispatch() // invoked by the 'action-url' in contact.php
    {
        $data = [
            'title'  => 'Contact',
            'src'    => 'images/logo.jpg',
            'alt'    => 'site.net site logo',
            'heading' => 'Contact',
            'success' => 'Thank you for contacting us - we will respond to you shortly.',
            'mailerror' => 'Server error - please send an email direct to [email protected], thank you.'
        ];
         
        $val 
$this->validate([ // data checking 'convenience method'
            'name' => 'required|alpha_space',
            'email' => 'required|valid_email',
            'message'  => 'required|min_length[40]|max_length[750]',
        ]);
 
        $model = new ContactModel();
        $email = \Config\Services::email();
        $sender $this->request->getVar('email'FILTER_SANITIZE_EMAIL);
        $message $this->request->getVar('message');
 
        if (!$val)
        {
            echo view('contact/contact'$data);
 
        }
        else
        {  // $request = \Config\Services::request();
           // To use the request-object outside the Controller we need to copy
           // it through the Service class
            $model->save([
                'name' => $this->request->getVar('name'),
                'email'  => $this->request->getVar('email'FILTER_SANITIZE_EMAIL),
                'message'  => $this->request->getVar('message'),
            ]);

            $config['protocol'] = 'smtp';
            $config['mailPath'] = '';
            $config['charset']  'iso-8859-1';
            $config['SMTPHost'] = '';
            $config['SMTPUser'] = '';
            $config['SMTPPass'] = '';
            $config['SMTPPort'] = '';
            $config['SMTPTimeout'] = '';
            $config['SMTPKeepAlive'] = TRUE;
            $config['SMTPCrypto'] = 'tls';

            $config['mailType'] = 'text';
            $config['wordWrap'] = TRUE;
            $email->initialize($config);

            $email->setFrom($sender);
            $email->setTo('[email protected]');
            $email->setCC('');
            $email->setBCC('');
            $email->setSubject('Contact form');
            $email->setMessage($message);

            if ($email->send(false))
            {
                echo view('contact/mailerror'$data);
            }
 
            echo view('contact/success'$data);
        }
    }

.

1.) I want to put the $data array outside the whole class or outside the functions (both possible in C/C++) and pass them as a parameter to the function so there is less code. How can I accomplish that in Codeigniter/php?

2.) My email-server (postfix) is running with TLS encryption. I do not see any possibility to use certificates in CI(4). Please elaborate...

3.) The error message is never evaluated when used in another form of configuration (mail/sendmail) in case testing it on my localhost at home which obviously fails:
PHP Code:
echo view('contact/mailerror'$data);. 
.

4.) Is this still needed after checking in the validation process?
PHP Code:
FILTER_SANITIZE_EMAIL
.


Any help is appreciated.


Cheers

G.
Reply


Messages In This Thread
Several questions concerning CI4 controller - by bitshifter - 03-24-2020, 09:03 AM
RE: Several questions concerning CI4 controller - by bitshifter - 03-25-2020, 08:22 AM
RE: Several questions concerning CI4 controller - by bitshifter - 03-25-2020, 12:00 PM
RE: Several questions concerning CI4 controller - by bitshifter - 03-28-2020, 12:13 PM
RE: Several questions concerning CI4 controller - by bitshifter - 04-01-2020, 01:06 PM
RE: Several questions concerning CI4 controller - by bitshifter - 04-17-2020, 07:09 PM



Theme © iAndrew 2016 - Forum software by © MyBB