1
votes

I'm trying to load an authentication PHP file in my Symfony controller.

I must use it with require_once (can't copy it). It contains a class for identification on my website.

This is what I tried so far:

/**
 * @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(),
        ));
}

This is the error I'm getting:

CRITICAL - Fatal Error: Class'SlothBundle\Controller\OAuthAriseClient' not found Context: {"type":1,"file":"/home/users/assoces/separatiiste/html/src/SlothBundle/Controller/DefaultController.php","line":39,"level":-1,"stack":[{"function":"loginAction","type":"->","class":"SlothBundle\Controller\DefaultController","file":"/home/users/assoces/separatiiste/html/app/bootstrap.php.cache","line":3109,"args":[]},{"function":"call_user_func_array","file":"/home/users/assoces/separatiiste/html/app/bootstrap.php.cache","line":3109,"args":[]},{"function":"handleRaw","type":"->","class":"Symfony\Component\HttpKernel\HttpKernel","file":"/home/users/assoces/separatiiste/html/app/bootstrap.php.cache","line":3071,"args":[]},{"function":"handle","type":"->","class":"Symfony\Component\HttpKernel\HttpKernel","file":"/home/users/assoces/separatiiste/html/app/bootstrap.php.cache","line":3222,"args":[]},{"function":"handle","type":"->","class":"Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel","file":"/home/users/assoces/separatiiste/html/app/bootstrap.php.cache","line":2444,"args":[]},{"function":"handle","type":"->","class":"Symfony\Component\HttpKernel\Kernel","file":"/home/users/assoces/separatiiste/html/web/app_dev.php","line":29,"args":[]},{"function":"{main}","file":"/home/users/assoces/separatiiste/html/web/app_dev.php","line":0,"args":[]}]}

I tried with composer but it returns the same error.

2
require_once ? in Symfony oO this is absurd.. How ever in your case you are in a namespace and if you want to call OAuthAriseClient you have to put slash before it \OAuthAriseClient ...Svetoslav
thanks it works can u explain me why ? and if it's absurd i'd like to know why and how correct it. thanksSky
You can read for PHP Namespacing at php.net documentation. The absurd thing in fact is not really wrong but I can't just explain you :) I am not good teacher..Svetoslav
Or just use: $consumer = \OAuthAriseClient:: Note the back slash. That is assuming that your OAuthAriseClient.php files does not have a namespace. And yes you should read about and understand namespaces. If OAuthAriseClient is from a third party then look at their website and see how to do a composer install. Then the includes go away.Cerad

2 Answers

1
votes

What about using composer to load your file?

You just have to make sure those files are accessible and within your project folder (if you want to keep things simple).

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

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

When using your classes you can either do use OAuthAriseClient; on the top of your file and then OAuthAriseClient::getInstance() or you can just prepend a backslash to its name \OAuthAriseClient::getInstance() when using it.

Source: http://php.net/manual/en/language.namespaces.importing.php

Anyway, according to the error you're getting this is what I think you should do:

use SlothBundle\Controller\OAuthAriseClient;

// ...

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

Note: If you're going to authenticate the user every time in your controllers consider adding an event listener that authenticates the user before reaching the controller.

0
votes

Because you are not importing the class from the use statements,
your controller is looking for a class which is part of the same namespace (SlothBundle\Controller).

You have to use your class as follows :

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