【问题标题】:UndefinedFunctionException: Attempted to call function from namespace ControllerUndefinedFunctionException:试图从命名空间控制器调用函数
【发布时间】:2014-01-15 07:20:44
【问题描述】:

我正在尝试使用 this code snipp from scott 应用安全令牌,但我似乎无法在 symfony2 中解决它,这是我的代码:

<?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);
    }

}

我将此 TokenController 创建为像 this 这样的服务,因此我可以将其调用到我的 DefaultController,现在该服务无法调用此控制器内的其他函数,是我做错了还是我的代码中有问题因为里面的函数(getTokenAction)好像不能使用TokenController类中的其他函数(randSecureAction)。

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    getTokenAction 里面有一行:

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

    这是你的问题,你必须使用$this-&gt;randSecureAction(...)。所以试试

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

    【讨论】:

    • 怎么了?忘记了那个简单的指针,我真的很晕。还有一个问题,除了响应组件中的 forward 方法之外,是否还有另一个函数用于调用 symfony 中的另一个函数,因为它返回一个带有标题字符串的字符串,我只想从函数中获取返回的数据。 tnx!
    • 我不确定,如果我理解正确的话。但是你可以像往常一样调用函数和返回值。您不必将 Response 用于您的内部功能。
    【解决方案2】:

    service 是一个 php 对象。所以你需要把它改成

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

    也使用 Response 对象来响应,而不是代替返回。

    个人更喜欢在action函数的末尾使用Action word。

    所以最终的代码应该是这样的

    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;
        }
    
    }
    

    以及作为服务的定义:

        <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>
    

    及用法:

    public function helloAction($name) {
    
        $token = $this->get('acme.token.controller')->getToken();
    
    }
    

    【讨论】:

    • tnx 为指南!我是 symfony 的新手,所以我不习惯使用其中的命名约定,所以我非常感谢指南! tnx 再次!。
    猜你喜欢
    • 1970-01-01
    • 2017-09-12
    • 2020-06-14
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2016-11-07
    相关资源
    最近更新 更多