在Symfony控制器中加载PHP文件时出错


Error loading PHP file in Symfony controller

我正试图在我的Symfony控制器中加载一个身份验证PHP文件。

我必须将它与require_once一起使用(无法复制)。它在我的网站上包含一个用于识别的类。

这就是我迄今为止所尝试的:

/**
 * @Route("/login")
 * @Template()
 */
public function loginAction()
{
    require_once("/usr/share/php/ariseid/client/OAuthAriseClient.php");
    require_once("./config.inc.php");
    $consumer = OAuthAriseClient::getInstance($consumer_key, $consumer_secret,$consumer_private_key);
    $consumer->authenticate();
    if ($consumer->has_just_authenticated()) {
            session_regenerate_id();
            $consumer->session_id_changed();
    }
    if ($consumer->is_authenticated()) {
            $results = $consumer->api()->begin()
                    ->get_identifiant()  
                    ->done();
            try { 
                    $_SESSION['AriseID'] = $results[0]();
            }
            catch(OAuthAPIException $e) {
                    echo "Erreur : ".$e->getMessage();
            }
    }
    return $this->render('SlothBundle:Default:index.html.twig', array(
            'islogged' => $consumer->is_authenticated(),
        ));
}

这是我得到的错误:

CRITICAL-致命错误:找不到Class'SlothBundle''Controller''OAuthAriseClient'上下文:{"type":1,"file":"/home/users/sassociaties/sseparatisite/html/src/SlothBundle/Controller/DefaultController.php","line":39,"level":-1,"stack":[{,{"function":"call_user_func_array","file":"/home/users/somparationite/html/app/bootstrap.php.cache","line":3109,"args":[]},{"function":"handleRaw","type":"->","class":"Symfony''Component''HttpKernel''HttpKernel",{"function":"handle","type":"->","class":"Symfony''Component''HttpKernel''HttpKernel","file":"/home/users/associations/sseparatisite/html/app/bootstrap.php.cache","line":3222,"args":[]},{"function":"handle","type":"-->","class":"Symbony''Component ''HttpKerne''DependencyInjection''ContainerwareHttpKernel","file.cache","行":2444,"args":[]},

我尝试使用composer,但它返回相同的错误。

使用composer加载文件怎么样?

你只需要确保这些文件是可访问的,并且在你的项目文件夹中(如果你想保持简单)。

"autoload": {
    "psr-0": { "": "src/", "SymfonyStandard": "app/" },
    "files": [
        "ariseid/client/OAuthAriseClient.php",
        "config.inc.php"
    ]
}

来源:https://getcomposer.org/doc/04-schema.md#files

使用类时,您可以在文件顶部执行use OAuthAriseClient;,然后执行OAuthAriseClient::getInstance(),也可以在使用时在其名称'OAuthAriseClient::getInstance()前加一个反斜杠。

来源:http://php.net/manual/en/language.namespaces.importing.php

不管怎样,根据你得到的错误,我认为你应该这样做:

use SlothBundle'Controller'OAuthAriseClient;
// ...
$consumer = OAuthAriseClient::getInstance($consumer_key, $consumer_secret, $consumer_private_key);

注意:如果每次要在控制器中对用户进行身份验证,请考虑添加一个事件侦听器,在到达控制器之前对用户进行验证。

因为您没有从use语句导入类,
您的控制器正在查找属于同一名称空间(SlothBundle'Controller)的类。

你必须按照如下方式使用你的类:

$consumer = 'OAuthAriseClient::getInstance($consumer_key, $consumer_secret,$consumer_private_key);