Magento 2 with Yii2

Hello Everyone,

We need to Login and signup from magento 2 (signup and login form) to yii2 in website. Yii2 is installed in root directory and magento 2 is installed in shop directory.
Please let us know in case anyone has any suggestions

Signup won’t be a problem since you can authenticate against arbitrary database tables. So once the user signed up through magento 2, you can use the created record in the database to authenticate against that.

A login on the other hand is a bit trickier. There are multiple solutions I can think of. The most viable for me seems to integrate Yii 2 into the login process of magento 2. See here: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-yii-integration#using-yii-in-others

In essence it should be enough to include that code into the login() procedure:

// require __DIR__ . '/../vendor/autoload.php'; // Probably not required
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../config/web.php';
$app = (new yii\web\Application($config));

// ...

if ($loginInMagentoWasSuccessful) {
    $user = \app\models\User::findOne($userId); // The User entity should implement IdentityInterface and authenticate against the magento user table.
    $app->user->login($user);
}

You probably have to deal with autoloader issues, cookie and/or file path issues etc. But in general this should work and is relatively straight forward.

Hi @Coksnuss

Thanks for the reply,

I had tried this method in login function of magento 2, But it was not logged in to the yii site, when i refresh the web page
And also one more issue coming is magento generating cookie in yii site, due to this not able to register on yii site. Please let us know in case anyone has any suggestions.

Out of interest, I tried this by myself. Unfortunately, magento 2 does not seem to be installable without a (paid?) account. So I used a forum (https://mybb.com/) as test case.

Setup:
Yii basic application is served under http://localhost:8080 (root directory is /home/user/htdocs)
Forum is served under http://localhost:8080/forum (root directory is /home/user/htdocs/forum)

Applications are served with php -S localhost:8080 -t . while I created a symbolic link /home/user/htdocs/index.php -> home/user/htdocs/web/index.php.

After setting up the forum, I used gii to auto generate a user model, based on the forum user table, and implemented a the IdentityInterface interface in the model. Then I tried to integrate Yii which worked exactly as I’ve written above.

This is the diff (containing all code changes after inital forum setup and yii project creation)
https://pastebin.com/dcqqec0B

It works exactly as intended. If I login in the Forum, I will also be logged in in Yii (both in the Yii application and the Forum). The session cookie path is set to ‘/’ automatically by Yii, so thats also fine. Everything works like a charm.

I could imagine that magento does not properly scope its session cookie to the ‘/shop’ path and thats probably why it fails?!