Bom dia a todos,
Sou iniciante em Slim Framework, e atualmente uso a versão 3.12 .
Esse é meu index.php com as rotas:
<?php
require 'vendor/autoload.php';
use \App\Controllers\MyController;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\App;
$container = new Slim\Container();
$container->get("settings")->replace([
'displayErrorDetails' => true,
'determineRouteBeforeAppMiddleware' => true,
'debug' => true
]);
$container["MyController"] = function(Slim\Container $container){
return new \App\Controllers\MyController($container);
};
$app = new App($container);
$app->group("/admin",function(App $app){
Summary
This text will be hidden
$app->get('/home',"MyController:index")->setName("home");
});
$app->run();
?>
This is my Abstract Class Controller:
<?php
namespace App\Controllers;
abstract class Controller {
protected $values = [];
function __construct(\Slim\Container $c) {
$this->values["request"] = $c->request;
$this->values["response"] = $c->response;
$this->values["router"] = $c->router;
$this->values["container"] = $c;
}
public function __get( $key )
{
return $this->values[ $key ];
}
public function __set( $key, $value )
{
$this->values[ $key ] = $value;
}
}
?>
This is my Concrete Class MyController:
<?php
namespace App\Controllers;
use App\Controllers\Controller;
class MyController extends Controller{
public function index(){
var_dump($this->values["router"]);
}
}
?>
I’m trying to get the name of the current route inside the index method in MyController, testing this way:
var_dump ($ this-> values [“router”]);
But the return I get is always this:
I’ve tried to do this:
var_dump($this->values[“request”]->getAttribute(“route”)->getName());
But I get this error:
Is there any way to explain to me what’s going on?
I will be very grateful.
NOTE: Forgive me if I am not being clear because I am using Google Translate.