Find parameter with Slim Framework 3

Asked

Viewed 909 times

0

I need help to get the parameter id the moment the URI is called, passing the same to the container.

Follow the code below:

$this->get('/{id}', function($request, $response, $args) {
  return $response->withJson($this->get('singleSelect'));
});

$this->appContainer['singleSelect'] = function ($id) {
  return $this->singleSelect($id);
};

public function singleSelect($id) {
  return $id;
}

Thanks in advance.

1 answer

0

I think the problem with your code is to call the variable that receives the Slim App() object $this. That is, from what I saw you did: $this = new \Slim\App(); when you should have called the $app variable or any other but less a reserved word like $this, example:

$app = new \Slim\App();

I did the test with the code below and it worked normally:

<?php

use \Slim\App;

require '../vendor/autoload.php';

// Create app
$app = new \Slim\App();

// Get container
$container = $app->getContainer();


$app->get('/', function ($request, $response) {
echo "Slim Framework";
});

$app->get('/hello/{name}', function ($request, $response, $args) {
    return $response->withJson('Name: ' . $args['name']
    );
});

$app->get('/hello', function ($request, $response){
    echo "Ola";
});

$app->get('/{id}', function($request, $response, $args) {
  return $response->withJson("Id: " . $args['id']);
});



$app->run();

Adjust your code as above or simply rename the $this variable and see if it solves your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.