UndefinedFunctionException: Attempted to call function from namespace Controller

13,862

Solution 1

Inside getTokenAction you have the line:

$token .= $codeAlphabet[randSecureAction(0,strlen($codeAlphabet))];

Here is your problem, you have to use $this->randSecureAction(...). So try

$token .= $codeAlphabet[$this->randSecureAction(0,strlen($codeAlphabet))];

Solution 2

service is a php object. so you need to change it to

$this->randSecureAction(0,strlen($codeAlphabet))

also use Response objects to response, not instead of return.

and personally prefer to use Action word just at the end of action functions.

so final code should be like this

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class TokenController extends Controller {

    public function randSecure($min, $max) {
        $range = $max - $min;
        if ($range < 0)
            return $min; // not so random...
        $log = log($range, 2);
        $bytes = (int) ($log / 8) + 1; // length in bytes
        $bits = (int) $log + 1; // length in bits
        $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
        do {
            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
            $rnd = $rnd & $filter; // discard irrelevant bits
        } while ($rnd >= $range);
        return $min + $rnd;
    }

    public function getToken($length = 32) {
        $token = "";
        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
        $codeAlphabet.= "0123456789";
        for ($i = 0; $i < $length; $i++) {            
             $token .= $codeAlphabet[$this->randSecure(0, strlen($codeAlphabet))];
        }
        return $token;
    }

}

and definition as service:

    <parameters>
        <parameter key="acme.controller.token.class">Acme\DemoBundle\Controller\TokenController</parameter>
    </parameters>

    <services>
        <service id="acme.token.controller" class="%acme.controller.token.class%" />        
    </services>

and usage:

public function helloAction($name) {

    $token = $this->get('acme.token.controller')->getToken();

}
Share:
13,862
Cedric
Author by

Cedric

Let's code and fly!

Updated on June 04, 2022

Comments

  • Cedric
    Cedric almost 2 years

    I am trying to apply a security token using this code snipp from scott but I can't seem to work it out in symfony2, here is my code:

    <?php
    
    namespace Acme\UserManagementBundle\Controller;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;
    
    class TokenController extends Controller
    {
    
        public function randSecureAction($min, $max) {
            $range = $max - $min;
            if ($range < 0) return $min; // not so random...
            $log = log($range, 2);
            $bytes = (int) ($log / 8) + 1; // length in bytes
            $bits = (int) $log + 1; // length in bits
            $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
    
            do {
                $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
                $rnd = $rnd & $filter; // discard irrelevant bits
            } while ($rnd >= $range);
    
            return new Response ($min + $rnd);
        }
    
        public function getTokenAction($length=32) {
            $token = "";
            $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
            $codeAlphabet.= "0123456789";
    
            for($i=0;$i<$length;$i++) {
                $token .= $codeAlphabet[randSecureAction(0,strlen($codeAlphabet))];
            }
            return new Response ($token);
        }
    
    }
    

    I create this TokenController as a service like this so I can call it to my DefaultController, now the service can't call the other functions inside this controller, am I doing it wrong or there is a problem within my code because the function (getTokenAction) inside can't seem to use other function(randSecureAction) in the TokenController class.