I have a web site running on drupalDrupal 8 whithwith a login module. I I want a $variable with the uid ofto get the current user ID for the currently logged-in user to later execute a database query later.
So i have write thisThis is the code I am using.
$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid; //return '220'
it work $uid = '220'
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$uid = $user->get('uid')->value;
echo $uid;
but inIn the file controller of mya custom module, this code workworks only between all using and mythe class controller like this :
<?php
namespace Drupal\CoopMatha_adh_apport\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;
$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid;
class RapportsController extends ControllerBase
{
//I want $uid here
public function filtrer($firstLetter='', $nbr_ligne='')
{
... some code ...
}
}
My problem is that i want to got the value of $uid (for example '220') ofuses the current user in my controller class for execute my query conditions but if i put just myfollowing code to get the current user in my class or trying to get $uid, the $uid return nothing.
namespace Drupal\CoopMatha_adh_apport\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;
$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid;
class RapportsController extends ControllerBase {
// I want the user ID here.
public function filtrer($firstLetter='', $nbr_ligne='') {
// …
}
}
So i want just a solution forHow do I get the $uid of the currentcurrently logged-in user insideID in the default controller class.?