vlauciani's avatar

Use $request->input params, globally

Hi all

Starting from a simple URL like this:

http://<host>/?var1=value1&var2=value2

I'm retrieving GET input:

class myController extends Controller
{
    public function index(Request $request)
    { 
       . . . 
       $var1 = $request->input('var1', 'default_value1');
       $var2 = $request->input('var2', 'default_value2');
       . . . 
    }
}

I would like to use $var1 and $var2 as a global variable to use it in the entire app.

I tried something like this:

class myController extends Controller
{
    public function index(Request $request)
    { 
       . . . 
       $var1 = $request->input('var1', 'default_value1');
       $var2 = $request->input('var2', 'default_value2');

       define('var1',$var1);
       define('var2',$var2);
       . . . 
    }
}

than I can use it, for example, into the Handler class:

class Handler extends ExceptionHandler
{
. . .
    public function render($request, Exception $e)
    {
        . . .             
        dd(var1);
        . . .
    }
}

but I do not like this and i don't think this is the best way.

Thank you, Valentino

0 likes
8 replies
martinbean's avatar

@vlauciani I’m not sure what input you want to use globally, but if a user is specifying parameters (i.e. settings) then you could put them in the session, and access them from there.

vlauciani's avatar

Hi @martinbean

One param that I would like to use globally, is the "output format" for example. The user can specify format=json or format=text and I need to return all possible response (from "data" to "Exception") in the json or text format.

Session could be a solution, but is this the best practice?

Thank you.

pmall's avatar

For your use case you can make a response macro (check the doc) and format your reponse the way specified in the response macro. You dont need to access this info globally.

vlauciani's avatar

Hi @pmall

I know the response macro... but the problem is not the output and how to format It.
The problem is to GET the variable and use it into all CLASSes (Controllers and Methods).

d3xt3r's avatar

How about a singleton context class to set the variables (if you don't want to persist it after the request completes) or session (as already mentioned) if the data requires persistence.

vlauciani's avatar

Hi @premsaurav

Could you give me a simple singleton example? I search it on Lumen Documentation and Google but I can't find it.

Thank you.

d3xt3r's avatar

Depends,

  1. If you want a very basic implementation
class Context
{
    protected static $instance = null;

    protected $data = array();

    /**
     * @return Context
     */
    public static function getInstance() {

        if(static::$instance === null) {
            static::$instance = new Context;
        }

        return static::$instance;
    }

    public static function set($name,$value)
    {
        static::getInstance()->data[$name] = $value;
    }

    public static function get($name, $default = null)
    {
        $data = static::getInstance()->data;

        return isset($data[$name]) ? $data[$name] : $default;
    }
}

// and then wherever you want use

Context::set('var1','value1');

// and

Context::get('var1');

// Please note that value will be available only through the running script.
  1. Similar results could also be achieved through binding your context to IoC and using facades.

Please or to participate in this conversation.