0

I'm trying to learn slim framework and I ran into a, in my eyes, strange issue. In Slim framework 3 it works fine. I've already done a lot of research before posting this question but to be honest it get even more complicated with all the answers I find.

I manage to GET all customers, GET a single customer and delete a customer. But POST (new customer) or PUT (updating a new customer) isn't working.

I'll try to give a much info as possible.

Public index.php
    <?php
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    use Slim\Factory\AppFactory;

    require __DIR__ . '/../vendor/autoload.php';
    require './src/config/db.php';

    $app = AppFactory::create();

    require './src/routes/customers.php';

    $app->run();

Config/db.php

    <?php
    /**
     * Connect MySQL with PDO class
     */
    class db {

      private $dbhost = 'localhost';
      private $dbuser = 'root';
      private $dbpass = 'root';
      private $dbname = 'api';

      public function connect() {

        // https://www.php.net/manual/en/pdo.connections.php
        $prepare_conn_str = "mysql:host=$this->dbhost;dbname=$this->dbname";

        $dbConn = new PDO( $prepare_conn_str, $this->dbuser, $this->dbpass );

        // https://www.php.net/manual/en/pdo.setattribute.php
        $dbConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        // return the database connection back
        return $dbConn;
      }
    }

And finally the customers.php...I've only provide the POST and PUT as these two aren't working.

$app->post('/api/customer/add', function( Request $request, Response $response){

  $name = $request->getParam('name');
  $telephone = $request->getParam('telephone');
  $email = $request->getParam('email');
  $website = $request->getParam('website');
  $location = $request->getParam('location');

  $sql = "INSERT INTO CUSTOMERS (NAME, TELEPHONE, EMAIL, WEBSITE, LOCATION) 
          VALUES(:name,:telephone,:email,:website, :location)";

  try {
    $db = new db();
    $db = $db->connect();
    $stmt = $db->prepare($sql);

    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':telephone', $telephone);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':website', $website);
    $stmt->bindParam(':location', $location);
    $stmt->execute();
    echo '{"notice" : {"msg" : "New customers Added."}';
  } catch( PDOException $e ) {
    echo '{"error": {"msg": ' . $e->getMessage() . '}';
  }
});


$app->put('/api/update/{id}', function( Request $request, Response $response){

  $id = $request->getAttribute('id');

  $name = $request->getParam('name');
  $telephone = $request->getParam('telephone');
  $email = $request->getParam('email');
  $website = $request->getParam('website');
  $location = $request->getParam('location');

  $sql = "UPDATE CUSTOMERS SET 
          name = :name,
          telephone = :telephone,
          email = :email,
          website = :website,
          location = :location,
          WHERE id = $id";

  try {
    $db = new db();
    $db = $db->connect();

    $stmt = $db->prepare( $sql );

    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':telephone', $telephone);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':website', $website);
    $stmt->bindParam(':location', $location);

    $stmt->execute();

    echo '{"notice" : {"msg" : "Customer is Updated."}';
  } catch( PDOException $e ) {
    echo '{"error": {"msg": ' . $e->getMessage() . '}';
  }
});

Error message

[16-May-2020 14:28:50 UTC] PHP Fatal error:  Uncaught Error: Call to undefined method Slim\Psr7\Request::getParam() in /Applications/MAMP/htdocs/MyApi/public/src/routes/customers.php:114
Stack trace:
#0 /Applications/MAMP/htdocs/MyApi/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(43): {closure}(Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)
#1 /Applications/MAMP/htdocs/MyApi/vendor/slim/slim/Slim/Routing/Route.php(381): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)
#2 /Applications/MAMP/htdocs/MyApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route->handle(Object(Slim\Psr7\Request))
#3 /Applications/MAMP/htdocs/MyApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#4 /Applications/MAMP/htdocs/MyApi/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#5 /Applications/MAMP/htdocs/MyApi/vendor/slim/slim/S in /Applications/MAMP/htdocs/MyApi/public/src/routes/customers.php on line 114
Sonia
  • 195
  • 1
  • 3
  • 12
  • What problem do you have? Are there any errors? – Nigel Ren May 15 '20 at 13:39
  • @NigelRen, just 500Internal Server Error – Sonia May 15 '20 at 13:44
  • Tempted to think it's https://stackoverflow.com/questions/57402634/request-parameters-are-empty-using-slim-v4-1 – Nigel Ren May 15 '20 at 13:45
  • @NigelRen Could be...but a bit confused with that solution – Sonia May 15 '20 at 13:57
  • 1
    Think the main thing is `$app->addBodyParsingMiddleware();` after `$app = AppFactory::create();` – Nigel Ren May 15 '20 at 13:59
  • @NigelRen I did that but still the same :( – Sonia May 15 '20 at 14:02
  • Error 500 just means "check the logs". Can you please check the actual error message or configure PHP to display it? – Álvaro González May 15 '20 at 16:36
  • @ÁlvaroGonzález, sorry for the late reply. I've update the question with the error I'm getting. – Sonia May 16 '20 at 14:30
  • 1
    That means that your PSR-7 implementation (the library you use to process requests and responses) doesn't provide a method with that name. To get post variables you need `$request->getParsedBody()` as Nigel suggested. – Álvaro González May 16 '20 at 16:17
  • Yes, but how do I implement this in my solution? Sorry, learner – Sonia May 16 '20 at 21:43
  • 1
    Follow the instructions [here](https://stackoverflow.com/a/57524022/13508) and [here](http://www.slimframework.com/docs/v4/objects/request.html). If you have problems, ask about the precise step you need help with (don't ask people to copy and paste those documents into an answer). – Álvaro González May 17 '20 at 11:59

0 Answers0