1

May be a very bad question. Not sure.

Just a brief

I am trying to add simplesamlphp in magento2.

1) I added it through composer and its added inside vendor directory. And i can access the classes like this $auth = new \SimpleSAML_Auth_Simple('default-sp'); And this works..and i can access functions also.

Now the issue is its not written keeping magento2's structure in mind. It will work only if i put the whole code in magento2's root.

EDIT

Now if i put the whole code in magento root and try to access like this(dont get me wrong..just try to check with object manager. Wont use it. Chill.)

$object_manager = \Magento\Framework\App\ObjectManager::getInstance();
        $dir = $object_manager->get('\Magento\Framework\Filesystem\DirectoryList');            
        $base = $dir->getRoot();
        $lib_file = $base.'/simplesamlphp/simplesamlphp/lib/_autoload.php';
        require_once($lib_file);
        $auth = new SimpleSAML_Auth_Simple('default-sp');

It gives me this

Fatal error: Class 'Magento\Customer\Controller\Account\SimpleSAML_Auth_Simple' not found. // BTW trying to call it from LoginPost action.

How to include classes which are at magento2's root? (Probably autoload wont work but still)

0

1 Answer 1

0

Try with backslash before

$auth = new \SimpleSAML_Auth_Simple('default-sp');
            ^

Multiple namespaces may also be declared in the same file. There are simple and bracketed allowed syntaxes.

Simple combination syntax

<?php
namespace MyProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }

namespace AnotherProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }

This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.

Bracketed syntax

<?php
namespace MyProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
}

namespace AnotherProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
}

It is strongly discouraged as a coding practice to combine multiple namespaces into the same file. The primary use case is to combine multiple PHP scripts into the same file.

To combine global non-namespaced code with namespaced code, only bracketed syntax is supported. Global code should be encased in a namespace statement with no namespace name as in:

Unnamespaced code

<?php
namespace MyProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
}

namespace { // global code
session_start();
$a = MyProject\connect();
echo MyProject\Connection::start();
}

No PHP code may exist outside of the namespace brackets except for an opening declare statement.

src: http://php.net/manual/en/language.namespaces.definitionmultiple.php

3
  • Probably you didnt read my post properly. I already tried that. That only works when you have it inside vendor folder. Nov 15, 2017 at 18:59
  • Put the code in magento root, it's a bad practices
    – Nolwennig
    Nov 17, 2017 at 12:01
  • Declare multiple namespaces?
    – Nolwennig
    Nov 17, 2017 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.