jeudi 20 juillet 2017

How to create our own function in Laravel

I'm trying to add some function into Laravel like encrypting or decrypting a value, formatting paragraph etc

Currently I have added my function into controller class like this

public static function encrypt_decrypt($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = 'secret';
    $secret_iv = 'secret_2';

    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if ($action == 'encrypt') {
        $outputs = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($outputs);
    } else if ($action == 'decrypt') {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

Now currently I'm calling my function like



The above method works Perfectly but doesn't seem nice and correct to me, because writing the whole path makes it difficult.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tjz4Pm
via IFTTT

Aucun commentaire:

Enregistrer un commentaire