• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 $this->filter('var', 'int'); in widgets

AndrewSimm

Active member
#1
I have some variables that I am getting from the address bar. These works perfectly in the controller. On the same page I have some widgets that display some statistics based on the variables from the address bar. Looking at my code below I think the issue is I can't figure out how to get parameterbag into the method, but I am not sure.

PHP:
<?php

namespace CIS\Backend\Widgets;

use \XF\Widget\AbstractWidget;
use XF\Mvc\ParameterBag;

class Rating extends AbstractWidget
{
    public function render()
    {

        //Page title
        $title = "Rating";

        //Get Class
        $get_class = $this->filter('class', 'int');
Error

ErrorException: Fatal Error: Call to undefined method CIS\Backend\Widgets\Rating::filter() src/addons/CIS/Backend/Widgets/Rating.php:17
 

Mike

XenForo developer
Staff member
#2
Widgets won't have access to the request object by default. You would access this via $this->app->request().

Generally speaking, there isn't an expectation that a widget would require values from the query string. It's certainly quite dangerous in terms of inputs potentially meaning multiple things. If you want to do something like this, ideally it would be done by passing context variables through the widget position.
 

AndrewSimm

Active member
#3
Widgets won't have access to the request object by default. You would access this via $this->app->request().

Generally speaking, there isn't an expectation that a widget would require values from the query string. It's certainly quite dangerous in terms of inputs potentially meaning multiple things. If you want to do something like this, ideally it would be done by passing context variables through the widget position.
How is it any more dangerous than getting values from the query string and using them in the page page controller?
 

Mike

XenForo developer
Staff member
#4
Widgets are designed to sit on arbitrary pages, so you don't know the inputs they may expect and use. If you and the controller use the same input, unexpected things may happen.
 

AndrewSimm

Active member
#5
Widgets are designed to sit on arbitrary pages, so you don't know the inputs they may expect and use. If you and the controller use the same input, unexpected things may happen.
These particular widgets are only going to sit on the 2 pages related to them. Basically I have a list of players and a roster list and has you filter the list it gives information based off of your filter. For instance if I filter players to only show Quarterbacks the widgets will filter down as well. So let's say the list is 10 Quarterbacks. The rating widget might show that 4 were 3 star rating, 4 were 4 star rating and 2 were 5 star rating. The main controller and the widgets will have the exact same parameters entered into the queries, with the widgets showing grouped information.