moses toh
moses toh

Reputation: 13192

Native php call function in Laravel Framework

I use Native PHP or regular PHP. I want to call function bcrypt in Laravel

My code is like this :

<?php
    $password = '12345678';
    echo bcrypt($password);
?>

It's not working

Its error is Fatal error: Call to undefined function bcrypt() in...

How to call function bcrypt in Laravel?

Does it can be done?

Thank you

Upvotes: 2

Views: 1562

Answers (2)

Tim
Tim

Reputation: 5943

You said you already have a file with helper functions in place. To be able to call the function directly you need to autoload that file.

Add a files array to your composer.json like this:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Utilities/helpers.php"
    ]
},

Next run composer dump-autoload the reload the autoload-files and you should be good to go.

Upvotes: 0

Mark Davidson
Mark Davidson

Reputation: 5513

To start off with this isn't a very good idea. If you wanting to use Laravel use Laravel if not just make use of the appropriate libraries to do the job your wanting.

However this is one way to use the BcryptHasher from Laravel ( Not the best way just a way ).

<?php
require __DIR__ . '/vendor/autoload.php';

use Illuminate\Hashing\BcryptHasher;

$hasher = new BcryptHasher();

var_dump($hasher->make('test'));

You can't just use the bcrypt method as stuff needs initializing which will take more code than the above.

Also its worth noting that at the end of the day the bcrypt method just does

password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);

so if your not making use of other Laravel stuff just use password_hash the $cost by default in Laravel is 10.

Upvotes: 3

Related Questions