Problem using hashid.id library

Asked

Viewed 153 times

0

I’m trying to implement the library hashids in my project, I am not using the composer

My job is as simple as that:

function generate_hash_id($id){
    require("../libs/Hashids.php");
    $hashids = new Hashids\Hashids('teste');
    $hash = $hashids->encrypt($id);
    return $hash;
}

But it’s coming back 500 Internal Server Error

I call the function inside an iterator

for($i = 0; $i < 10, $i++){
    generate_hash_id($i);
}

The structure of my project:

inserir a descrição da imagem aqui

From what I understand you’re accusing that the library is not in this path, but that’s the way!


Following the @Uilherme tips, I removed the require from within the function.

require("../libs/Hashids.php");

function generate_hash_id($id){
    $hashids = new Hashids\Hashids('teste');
    $hash = $hashids->encrypt($id);
    return $hash;
}

for($i = 0; $i < 10, $i++){
    generate_hash_id($i);
}

Now the error that returns:

Fatal error: Uncaught Error: Call to Undefined method Hashids Hashids::Encrypt() in /usr/share/Nginx/www/api/Automatic/index.php:16 Stack trace: #0 /usr/share/Nginx/www/api/Automatic/index.php(110): generate_hash_id(1) #1 {main} thrown in /usr/share/Nginx/www/api/Automatic/index.php on line 16


1 answer

2


When you call more than once the require it will cause some exception error because the function has already been declared the first time it called the require '../libs/Hashids.php';, if you call again (in your case the loop) it will try to declare again and this is where the error occurs.

As you must be with the errors turned off appears 500 Internal error server, to fix trade require by require_once, something else the method $hashids->encrypt does not exist, the correct is $hashids->encode, see: https://github.com/ivanakimov/hashids.php/blob/master/lib/Hashids/Hashids.php#L114

The correct code would be:

function generate_hash_id($id){
    require_once "../libs/Hashids.php";
    $hashids = new Hashids\Hashids('teste');
    $hash = $hashids->encode($id);
    return $hash;
}

For details on how to detect errors and how to use in production and development mode read this question and answers:

  • Unnn actually I pulled the require("../libs/Hashids.php"); out of the function, but I’m having instability here on the server, so I haven’t been able to test all the recommendations

  • @Marcelobonifazio but require_once solved? Or error still occurs? If it’s like Ocaweb, it doesn’t have much to configure :/ but I say you should do everything on your machine, test well, use the error_reporting(E_ALL|E_STRICT); ini_set('display_errors', 1); at the top of the document and after everything is ok and send it to the production server following the tips of http://answall.com/a/106563/3635

  • the require_once solved the initial problem, but now the error that returns is another rs

  • @Marcelobonifazio and you will tell me what it is? :) so I can help

  • I already edited in question :)

  • @Marcelobonifazio exchange encrypt for encode and tell me what happens. I edited the answer ;)

  • I swear I’m not crazy and by the time I got this code as reference I was encrypt ahsuashausahs, now I went to see in the documentation of the guys and it is encode, worked @Guilhermenascimento thank you :)

  • 1

    @Marcelobonifazio why I always look at the official website and I go from the home page, had time I searched on Laravel through google and I would end up on the documentation page of Laravel4 hehehe, XD but I use the 5.2/5.3

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.