Using user plugin session in another plugin within OctoberCMS

1.3k Views Asked by At

I’m new to Laravel and OctoberCMS, there is a lot of documentation on both but because I don’t have much of an understanding of either I would really appreciate some help. It's also quite confusing trying to understand both at the same time, especially as there is way more documentation about laravel than October.

Currently, I wish to extend a plugin (Link to their extending plugin documentation). I need to use the session (username or email) from the rainlab user plugin (Link to their Git) and insert it into another plugin.

I'm upgrading the other plugin that currently relies on users entering their name via a form and replacing this with their username or email address once logged in.

My Layout contains the user plugin session:

{% component 'session' %}

The user plugin session works fine on the rest of my pages but I have gotten a 'value not found' error with my previous attempts using it with the plugin.

I know I need to either link the plugins or create a new one, and retrieve the username (or email) from the users database and insert it into another table, just can’t quite work out how to do this. To make matters worse I don’t have experience with MVC either!

Added this function (from an example I found) to the second plugin's Plugin.php page:

Plugin.php

public function boot()
 {
  Event::listen('rainlab.user.register', function($user) {
  // Code to register $user->email to mailing list
 });
}

But I got 'Undefined' error.

I've also tried adding a Routes.php file as I thought there may be an issue with middleware.

Routes.php

Route::get('/test', function () {
    dd(
        Auth::getUser(),
        BackendAuth::getUser()
    );
})->middleware('web');

Received this error:

"Call to undefined method RainLab\User\Facades\Auth::getUser()"

I’ve been banging my head against the wall for a week now and any tips or explanations would be greatly received. If you require any other bits of code please let me know. I haven't added any more bits of code as I know those pieces didn't work (being going around in circles and it would be a bit erratic and not much use) and I don't want to spam code on this page. I've shown the bits above to try and explain some previous thought processes. Also, it's the start of my project and not much code has changed from the generic code available to you from the OctoberCMS and User Plugin Gits.

I believe an explanation and some help here would benefit a lot of people, I can see I’m not the only one to have issues, partly down to the changes in the latest version of laravel.

Any thoughts and explanations would be much appreciated.

UPDATE:

To make it clearer where I need my session to appear I've included the code below. $name needs to be replaced by a new variable like email or username (from session).

components\Chat.php

        $name = 'anonymous';
   if ($n = Input::get('name')) {
        $name = strip_tags($n);
    }
    $message = new Message(['text'=>$text, 'user_id'=>$user_id, 'name'=>$name]);

    $message->save();

    return $this->getMessagesOutput($message->id, [$message]);
}
1

There are 1 best solutions below

3
On BEST ANSWER

It will be really Easy if you are adding session in layout {% component 'session' %}

now on entire page you can use {{ user.email }} directly in twig to get user email

if you need to use using PHP you can use Auth facade

this code must me in component which are included in page which is using that layout or in its life-cycle methods

// Returns the signed in user
$user = Auth::getUser();

echo $user->email;

you can not directly use route because route is separate from page execution so Auth will not work there.

"I need to use the session (username or email) from the rainlab user plugin (Link to their Git) and insert it into another plugin."

just create new component which have this code and include it in page.

can you share details what you need to do and how you are doing to get details and on which time you need to use that details so i can answer it precisely.

UPDATE

// by default name
$name = 'anonymous';
$user_id = 0; // if user is not logged in

// if user is logged in then use session
if($sessionUser = \Auth::getUser()) {
   // if user didn't set name then you can assign some default value here or email
   $name = $sessionUser->name != '' ? $sessionUser->name : $sessionUser->email;  
   $user_id = $sessionUser->id;
}   

// not needed but if user want to edit its name then we use that one
if ($n = Input::get('name')) {
    $name = strip_tags($n);
}

$message = new Message(['text'=>$text, 'user_id'=>$user_id, 'name'=>$name]);

$message->save();

return $this->getMessagesOutput($message->id, [$message]);