0

I have a web site on drupal 8 whith a login module. I want a $variable with the uid of the current user for execute a query later.

So i have write this code

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $uid= $user->get('uid')->value;
  echo $uid; //return '220'

it work $uid = '220'

but in the file controller of my custom module this code work only between all using and my 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') of the current user in my controller class for execute my query conditions but if i put just my code to get the current user in my class or trying to get $uid, the $uid return nothing.

So i want just a solution for get the $uid of the current user inside the default controller class.

  • 2
    Ignoring the invalid syntax, what exactly are you expecting to happen when you echo something in (I'm assuming) a class method? – Clive Feb 16 '18 at 11:42
  • i'm trying to get "16008001" in $column_name. It's correct when it's out of my controller class. A classic "echo" work in my class (example $test = '32' -> echo $test;) – psilonnix Feb 16 '18 at 13:07
  • 1
    Put yourself in the position of someone who hasn’t built your site (ie everyone who answers questions here). If you were given the code and description you’ve given here, would you be able or inclined to try to help? If you were to set up a simple environment to reproduce the problem, again given just the code and description in this question, how would you do it? What files would you create and what would thy contain? You can’t throw vague stuff at the internet and hope people will work it out, you need to be clear about the problem you’re facing – Clive Feb 16 '18 at 13:26
2

So i want just a solution for get the $uid of the current user inside the default controller class.

Controllers have a currentUser method:

public function filtrer($firstLetter='', $nbr_ligne='') {
  $current_user_id = $this->currentUser()->id();
}
  • YEAH, thanks it works. sry for my terrible problem description and my english. Good week :D – psilonnix Feb 16 '18 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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