Sage Roots $_POST and $_GET variables

Dear community,

I am looking for a best practise for passing $_POST and $_GET variables to template parts.

Do you create a function that holds these variables?

or do you just request them within your template files with $_POST and $_GET (I think this is kinda ugly)


PS: is there any code reference for questions like these? because I tend to create my own solutions. But I rather stick with best practices. I think everyone is developing on his own matter. Would be nice to have some guidelines :slight_smile:


Alright, I am here to answer my own question :slight_smile:

Step 1: Register your query variable function and hook it up :wink:
function add_query_vars( $qvars ) {
$qvars[] = ‘var_x’;
$qvars[] = ‘var_y’;
$qvars[] = ‘var_z’;
return $qvars;
add_filter( ‘query_vars’, NAMESPACE . ‘\add_query_vars’ );

Step 2: refresh permalinks
Step 3: Retrieve your variables
<?php $var_x = get_query_var('var_x ', -1 ); ?>
<?php $var_y = get_query_var('var_y ', -1 ); ?>
<?php $var_z = get_query_var('var_z ', -1 ); ?>

Let me know if this is the right way.



1 Like

Nice. I didn’t know WordPress had this functionality.

Here’s an updated filters.php function for Sage 9:

 * Add custom query vars
 * Works with $_POST and $_GET
add_filter( 'query_vars', function ( $vars ){
    $vars[] = "search_keywords";
    $vars[] = "search_location";
    return $vars;
} );

Then in my Controllers I would do something like this for each variable:

  * Pass jobs_keywords query_var down
  * @return String
  public function keywords()
      return get_query_var('jobs_keywords');

Then in my template I can access them like so:

{{ $keywords }}