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:
$app->group('/admin', function () use ($app){
$dir = "root";
$app->get("", function(){
$page = new PageAdmin();
$page->setTpl("main/main");
});
});
$app->group('/user', function () use ($app){
$dir = "all";
$app->get("", function(){
$page = new PageUser();
$page->setTpl("main/main");
});
});
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.
I tried to create middleware using the Slim Framework documentation itself as follows:
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use \Rain\Tpl;
$slimSettings = array('determineRouteBeforeAppMiddleware' => true);
$slimConfig = array('settings' => $slimSettings);
$app = new \Slim\App($slimConfig);
$myMiddleware = function ($request, $response, $next) {
$route = $request->getAttribute('route');
$routeName = $route->getName();
$groups = $route->getGroups();
$methods = $route->getMethods();
$arguments = $route->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->add($myMiddleware);
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.
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:
Slim Application Error
A website error has occurred. Sorry for the temporary inconvenience.
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.