Page 1 of 1
Call to a member function variable() on null - can you use $request with this event?
Posted: Sat Dec 30, 2017 9:16 pm
by aster59
Hi everyone, happy holidays.
My goal is to:
Grab a query string (&ref) from the users url after they click agree to the terms.
Url ex.)
https://forum.com/ucp.php?mode=register&ref=person_who_ref_me
I used the event:
core.ucp_register_data_before
My code was inside my function in the listener:
Code: Select all
$ref = $request->variable('ref', '', false,\phpbb\request\request_interface::GET);
But I get the error in my title. I declared
$request
like this in my listener file.
Code: Select all
public function __construct(\phpbb\template\template $template,
\phpbb\controller\helper $helper,
\phpbb\user $user,
\phpbb\db\driver\driver_interface $db,
\phpbb\request\request $request)
What did I do? Can I not do request with this event or am I going about this the wrong way? Basically I need this query string as a referral to be in the database after the user registers and I think this is the first step...
Re: Call to a member function variable() on null - can you use $request with this event?
Posted: Sat Dec 30, 2017 9:54 pm
by AbaddonOrmuz
Show the complete code, because you're not declaring
$request
, your code says that you're
expecting that
$request
will be a instance of
phpbb\request\request
, but you're not assingning that variable so you can't use it outside the
__construct()
method.
Read about dependency injection:
https://area51.phpbb.com/docs/dev/3.2.x ... -injection
Re: Call to a member function variable() on null - can you use $request with this event?
Posted: Sat Dec 30, 2017 10:01 pm
by aster59
AbaddonOrmuz wrote: ↑Sat Dec 30, 2017 9:54 pm
Show the complete code, because you're not declaring
$request
, your code says that you're
expecting that
$request
will be a instance of
phpbb\request\request
, but you're not assingning that variable so you can't use it outside the
__construct()
method.
Read about dependency injection:
https://area51.phpbb.com/docs/dev/3.2.x ... -injection
Oh no, I had forgotten
$this
but it didn't fully solve it. Here is the full code...
Now I get an error: Cannot access empty property. I have my namespace declared and using Symfony.
Code: Select all
class points_listener implements EventSubscriberInterface {
protected $template, $helper, $user, $db, $request;
public function __construct(\phpbb\template\template $template,
\phpbb\controller\helper $helper,
\phpbb\user $user,
\phpbb\db\driver\driver_interface $db,
\phpbb\request\request $request)
{
$this->template = $template;
$this->helper = $helper;
$this->user = $user;
$this->db = $db;
$this->request = $request;
}
static public function getSubscribedEvents()
{
return array(
'core.ucp_register_data_before' => 'ref_link_find',
);
}
public function ref_link_find($event) {
$ref = $this->$request->variable('ref', '', false,\phpbb\request\request_interface::GET);
die(print $ref);
}
And just using die() to see the value but I get that error instead.
I also tried using SERVER and HTTP_REFERER but that is empty too?
Re: Call to a member function variable() on null - can you use $request with this event?
Posted: Sat Dec 30, 2017 10:04 pm
by AbaddonOrmuz
You have a typo, it's not $this->$request()
but $this->request()
You might wan't to use var_dump()
or print_r()
for debuggin.
Re: Call to a member function variable() on null - can you use $request with this event?
Posted: Sat Dec 30, 2017 10:09 pm
by aster59
AbaddonOrmuz wrote: ↑Sat Dec 30, 2017 10:04 pm
You have a typo, it's not
$this->$request()
but
$this->request()
You might wan't to use
var_dump()
or
print_r()
for debuggin.
Oh, well how pathetic of me. Thanks. Sorry a little rusty.
Yeah, good idea.
Re: Call to a member function variable() on null - can you use $request with this event?
Posted: Sat Dec 30, 2017 10:12 pm
by AbaddonOrmuz
Don't worry, we've all had one of those days
