title: Get Group Name in Slim Framework
body: <p>I have the following group of routes in my application:</p>
  
  <pre><code>$app-&gt;group('/admin', function () use ($app){
      $app-&gt;get("", function(){
          $page = new PageAdmin();
          $page-&gt;setTpl("main/main");
      });
  });
  $app-&gt;group('/user', function () use ($app){
      $app-&gt;get("", function(){
          $page = new PageUser();
          $page-&gt;setTpl("main/main");
      });
  });
  </code></pre>
  
  <p>I want to define the value of a variable based on the group of these routes.</p>
  
  <p>For example: if the group is “admin”, my variable will be equal to 1. If the group is “user”, my variable will be equal to 2.</p>
  
  <p>How do I do this? I am using version 3 of the Slim Framework.</p>
  
  <p>I’ve tried using middleware using $route-> getGroups () but my page gives an error and the result returned is much more complex than I need.</p>
  
  <p><br>
  <strong>EDIT</strong></p>
  
  <p>I tried to assign the value to the variable after defining the group, but this approach did not work, because this value will always be that of the last group (because of the code reading flow). For example:</p>
  
  <pre><code>$app-&gt;group('/admin', function () use ($app){
      $dir = "root";
  
      $app-&gt;get("", function(){
          $page = new PageAdmin();
          $page-&gt;setTpl("main/main");
      });
  });
  $app-&gt;group('/user', function () use ($app){
      $dir = "all";
  
      $app-&gt;get("", function(){
          $page = new PageUser();
          $page-&gt;setTpl("main/main");
      });
  });
  </code></pre>
  
  <p>In the above case, even if I am on a route in the "admin" group, the $dir variable will have the value "all". If I comment out the line $dir = "all"; my application works normally, because the variable has not been replaced.</p>
  
  <p>I tried to create middleware using the Slim Framework documentation itself as follows:</p>
  
  <pre><code>use Psr\Http\Message\ServerRequestInterface;
  use Psr\Http\Message\ResponseInterface;
  use \Rain\Tpl;
  
  $slimSettings = array('determineRouteBeforeAppMiddleware' =&gt; true);
  
  $slimConfig = array('settings' =&gt; $slimSettings);
  $app = new \Slim\App($slimConfig);
  
  $myMiddleware = function ($request, $response, $next) {
  
      $route = $request-&gt;getAttribute('route');
      $routeName = $route-&gt;getName();
      $groups = $route-&gt;getGroups();
      $methods = $route-&gt;getMethods();
      $arguments = $route-&gt;getArguments();
  
      print "Route Info: " . print_r($route, true);
      print "Route Name: " . print_r($routeName, true);
      print "Route Groups: " . print_r($groups, true);
      print "Route Methods: " . print_r($methods, true);
      print "Route Arguments: " . print_r($arguments, true);
  };
  
  
  $app-&gt;add($myMiddleware);
  </code></pre>
  
  <p>Using the code above, all information about groups, methods and arguments is displayed. As I said, this answer is much more complex than I believe it needs.</p>
  
  <p>In addition, after commenting on the lines that print this information, my page simply stops working. So, even if I wanted to use them, I can't, because from then on the following message starts to appear:</p>
  
  <blockquote>
    <p>Slim Application Error</p>
    
    <p>A website error has occurred. Sorry for the temporary inconvenience.</p>
  </blockquote>
  
  <p>I believe that my problem is simple to be solved, but I admit I don't have the necessary knowledge for that. I think I will be able to do what I need through middleware, but I just can't seem to get it to work.</p>
  
  <p><br>
  <strong>EDIT 2</strong></p>
  
  <p>I will use this variable in a class constructor to define which templates will be used. These templates will be defined according to the route group.</p>
  
  <p>Today the application is working with constants that are defined in a project configuration file.</p>
  
  <pre><code>namespace Admin;
  
  use ACM\PageACM;
  
  class PageAdmin extends PageACM{
      public function __construct($opts = array(), $project = ACMDef_PROJECT, $workspace = "admin", $theme = ACMDef_THEME_ADMIN){
          parent::__construct($opts, $project, $workspace, $theme);
      }
  }
  </code></pre>
  
  <p>The point is that I need to exchange these constants for global variables, because I can have two users with different permissions using the same template or several different templates.</p>
  
  <p>The constant that I need to transform into a global variable is "ACMDef_THEME_ADMIN", and it concerns the directory in which my template is located. It can assume different values, because a user can work with different themes, or users with different permissions can consume these resources as well.</p>