2

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

4
  • 1
    You can only call functions that exist. Commented Dec 21, 2015 at 14:53
  • @jedrzej.kurylo, I want to call function bcrypt in laravel. The functions exist in helper laravel. But I find it difficult to call the function Commented Dec 21, 2015 at 15:03
  • @jedrzej.kurylo, If use php native or php regular, Whether it can call a function in laravel? Commented Dec 21, 2015 at 15:07
  • bcrypt isn't installed on your system. It's an extension for PHP and not included by default (at least in some versions). Look up how to install/enable it and you should be fine to use it. Commented Dec 21, 2015 at 15:24

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

the above code can be run in php native or php regular? I tried but it did not work
What error did you get? This should work in a regular php file you will need to update the require path to be pointed to your Laravel directory my file was in the Laravel directory it's self but just regular PHP.
I would like to ask once again. In helpers (Illuminate\Foundation\helpers) exist function bcrypt. The function like this : function bcrypt($value, $options = array()) { return app('hash')->make($value, $options); }. Whether it can call the function of the native php? Thank you
0

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.

1 Comment

I am still confused. I do not use laravel. I use php native. I just wanted to use the hash laravel. So, my point, whether it can use the hash laravel in native php?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.