mevdschee / php-crud-api

Single file PHP script that adds a REST API to a SQL database
MIT License
3.59k stars 1.01k forks source link

Postgresql database integration with slim4 php using php-crud-api query #719

Closed haider0324 closed 3 years ago

haider0324 commented 3 years ago

Hi Experts,

Need to build an app server application using slim4 php and postgresql database. Could you please help me out generate api's from postgresql database using php-crud-api. I follow this post https://tqdev.com/2019-automatic-api-slimphp-4 but from this point Install PHP-CRUD-API, i am not able to get what i need to do Where should i provide postgresql database connection details etc Generate api from postgresql database Not able to understand app/routes.php code

Thanks in advance

mevdschee commented 3 years ago

Thank you for creating this issue. What exactly did you execute? What are the error messages? The post assumes the server is mysql and the server is on localhost, but you can specify other config parameters (driver, address, port) next to the existing (username, password, database) to solve your problem. The least you need to add is:

'driver' => 'pgsql',

See: https://github.com/mevdschee/php-crud-api#configuration

I hope this helps.

NB: The environment variables (such as "PHP_CRUD_API_PASSWORD") take precedence over the PHP configuration, so you may specify the password there if that suits your security model better.

haider0324 commented 3 years ago

Thanks for your quick response

Basically i am stuck in Installation section of php-crud-api . I am not able to overcome this line PHP 7.0 or higher with PDO drivers for MySQL, PgSQL, SqlSrv or SQLite enabled

I am confused how to use php-crud-api with my postgresql local database. How to install php-crud-api and use with slim4 php.

Thanks

mevdschee commented 3 years ago

Did you download or install composer (see: https://getcomposer.org/)? You can also do:

curl https://getcomposer.org/composer-stable.phar -o composer.phar

Did you manage to run the composer command?

php composer.phar require mevdschee/php-crud-api

Does that help you? Are you on Windows? If so, follow the composer installation manual here:

https://getcomposer.org/doc/00-intro.md#installation-windows

After that run the composer command as:

composer require mevdschee/php-crud-api

you need to type that in the command prompt.

haider0324 commented 3 years ago

I apologize for taking your time. Great Thanks for looking into this query

Please have a look what i have done so far. Currently i am using MAC OS Catalina

Composer SetUp and Install Command php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('sha384', 'composer-setup.php') === 'c31c1e292ad7be5f49291169c0ac8f683499edddcfd4e42232982d0fd193004208a58ff6f353fde0012d35fdd72bc394') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"

Install Slim SlimPHP 4 for Automatic REST API Commands php composer.phar create-project slim/slim-skeleton slimrestpgapi cd slimrestpgapi mv ../composer.phar . php composer.phar start

Install PHP-CRUD-API but it doesn't work php composer.phar require mevdschee/php-crud-api and done changes in app/routes.php file as well

but in project folder config directory not found and when i put following database details it doesn't works $config = new Config([ 'username' => 'php-crud-api', 'password' => 'php-crud-api', 'database' => 'php-crud-api', 'basePath' => '/api', ]);

mevdschee commented 3 years ago

Change:

$config = new Config([
  'username' => 'php-crud-api',
  'password' => 'php-crud-api',
  'database' => 'php-crud-api',
  'basePath' => '/api',
]);

Into:

$config = new Config([
  'driver' => 'pgsql',
  'username' => 'php-crud-api',
  'password' => 'php-crud-api',
  'database' => 'php-crud-api',
  'basePath' => '/api',
]);

To make a PostgreSQL instead of a MySQL connection (notice the 'driver' config parameter).

NB: Mac OS is not an environment that I test the software on. I only test on Linux. Nevertheless I don't know any reason the software should not work on Windows or Mac OS.

mevdschee commented 3 years ago

Also try to point your browser to your slim application on the localhost on the "/api" path. What is the result? Are there errors in the error log?

haider0324 commented 3 years ago

I got this following error while including driver

Screenshot 2020-10-22 at 11 23 11 AM

I know i am doing mistake config folder is not present in my directory. Even i run the command php composer.phar require mevdschee/php-crud-api

Project directory structure

Screenshot 2020-10-22 at 11 26 30 AM

And code in routes.php

<?php

declare(strict_types=1);

use Slim\App;
use Slim\Psr7\Request;
use Slim\Psr7\Response;

// Note these extra use statements:
use Tqdev\PhpCrudApi\Api;
use Tqdev\PhpCrudApi\Config;

return function (App $app) {
    $container = $app->getContainer();

    // Add this handler for PHP-CRUD-API:
    $app->any('/api[/{params:.*}]', function (
        Request $request,
        Response $response,
        array $args
    ) use ($container) {

        $config = new Config([
            'driver' => 'pgsql',
            'username' => '',
            'password' => '',
            'database' => 'testinternet_local',
            'basePath' => '/api',
        ]);
        $api = new Api($config);
        $response = $api->handle($request);
        return $response;
    });
};
mevdschee commented 3 years ago

Can you check the following path? Does this file exist?

vendor/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi/Config.php

Can you ensure composer.json has the following (in the require section)?

  "mevdschee/php-crud-api": "*",

I hope this clears things up.

haider0324 commented 3 years ago

No this path and file not exits in my source code folder.

Screenshot 2020-10-22 at 1 44 40 PM

and also it didn't find in composer.json as well

Screenshot 2020-10-22 at 1 45 10 PM

I am using following versions Php Version 7.4 SlimPhp 4.5 PostgreSQL 13.0

haider0324 commented 3 years ago

Thank You @mevdschee , It's working now

Screenshot 2020-10-22 at 1 55 43 PM

I just want to know how we generate api classes in our code. Suppose i have existing database and based on that they will generate crud api services for each table. Create models, services etc based on database tables in postgresql database.

mevdschee commented 3 years ago

I just want to know how we generate api classes in our code.

You don't have to, the code uses reflection. You can adjust it's behavior using configuration parameters.

Create models, services etc based on database tables in postgresql database.

If that is what you want, maybe uninstall this library and try slimp-api by @gabriel403:

https://github.com/slimphp-api/slim-api

haider0324 commented 3 years ago

Great Thanks @mevdschee for helping and guiding me

mevdschee commented 3 years ago

You are welcome, I'm happy to help you with any further issues you encounter!