Request is always null


#1

I am using postman to send request to my localhost.

http://localhost/slim/users?name=Mahesh
NULL --> Why is it null?

Routes.php
<?php

use App\Controllers\HomeController;
use App\Controllers\UserController;


$app->get('/', HomeController::class.':index');
$app->post('/users', UserController::class.':addUsers');

index.php

require "vendor/autoload.php";


$app = new Slim\App([

    'settings' => [
        'displayErrorDetails' => true,
        'db' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'database' => 'db',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
        ]
    ]
]);

$container = $app->getContainer();

use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;

$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$container['db'] = function($container) use ($capsule) {
    return $capsule;
};

require __DIR__ . "/app/routes.php";

$app->run();

usercontroller

<?php

namespace App\Controllers;
use App\Models\User;

class UserController extends Controller {

    public function index( $response, $request ) {

        
    }

    public function addUsers( $request, $response) {
        
        var_dump($request->getBody());
        var_dump($request->getParsedBody());
    }
}

controller

<?php

namespace App\Controllers;

use Interop\Container\ContainerInterface;

class Controller {

    protected $container;
    public function __construct(ContainerInterface $c) {

        $this->container = $c;
    }
}

What is wrong in the coding?
Does it follow good practices?

Please tell me. I am learning slim.


#2

The request body is NULL, because the GET request you issued (via a browser ?) does not contain any body.

If you want to access the “name” query parameter of the URI, then you need to call

$request->getUri()->getQuery()

or

$request->getQueryParam(‘name’)