Welcome Guest, Not a member yet? Register   Sign In
CI4: Pass Data From Filter to Controller
#1

Is there anyway to pass authenticated user data from filter to controller?
Reply
#2

You can add anything to the Request and it will be available in your controller at $this->request. I think it would be more typical however to have some middleware library that handles the authentication state, and is accessed by both your Filter and Controller. So for example your Filter uses App\Libraries\Authentication::login() and then Controller uses App\Libraries\Authentication::getUser()
Reply
#3

In my case, it's a rest api with just jwt token with no username or password. In the filter, the user id will be decoded from the token and need to pass to controller. How to modify $this->request ?
Reply
#4

(04-10-2021, 09:10 AM)h4n Wrote: In my case, it's a rest api with just jwt token with no username or password. In the filter, the user id will be decoded from the token and need to pass to controller. How to modify $this->request ?

 I used as follows


in the filter
-------------
public function before(RequestInterface $request, $arguments = null){
.....

              $user= new \StdClass();
            $user->user_id =123;  //GOT from JWT
            $user->user_name="Name test"; //GOT from JWT

            $request->user =$user;
            return $request;

in targeted controller
-------------
$user_id = $this->request->user->user_id;
$user_name = $this->request->user->user_nam;
Reply
#5

I don't agree with accessing your database from your controller -- that is what your model is for. To pass a value as a $_GET parameter, just add a slash, then the integer after the controller name in your url property.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB