Publish REST API in heroku

0

I need to post a API REST in heroku . I have uploaded the project structure to the hosting with the required git commands. In addition, I created a database in heroku through phpmyadmin . The file .env of the project contains the necessary configuration to connect to the database:

DATABASE_HOST    = 'hostingmysql329.nominalia.com'
DATABASE_NAME    = 'comparador'
DATABASE_PORT    = 3306
DATABASE_USER    = 'user1'
DATABASE_PASSWD  = 'password'
DATABASE_DRIVER  = 'pdo_mysql'
DATABASE_CHARSET = 'UTF8'

The structure of the project is as follows:

In the url: app I can see the specification of the API that I defined with swagger and slim but when I send requests to the server I get the following error:

I always get the error 404 in all requests. To update every change I make in local I use the following commands:

  • git add .
  • git commit -am "message"
  • git push heroku master

Could it be that the changes are not being updated? Should I write the configuration of the database in a file other than the .env ?

    
asked by Lara 29.05.2018 в 13:18
source

1 answer

2

Ok, first, I do not know what library you are using to read your file .env , but assuming it is PHPDotenv or Symfony / Dotenv, what you do in your local is to export the contents of that file to environment variables, and then access them in the life cycle of the application with getenv or $_ENV .

In Heroku the environment variables are handled in the application panel. The best practices indicate that your file .env should not be subject to version control but explicitly ignored, to avoid that the keys remain in github or wherever. Probably, as Kleith said, Heroku is simply ignoring that file.

For the project to read the environment variables, go to your Heroku project, to the configuration section of your app (it can be in link ) and click REVEAL CONFIG VARS .

Enter the content of your .env here (here I put two variables as an example, you must put them all. Without quotes .In a file .env everything that comes after = it is assumed to be text so you do not need them.

With that your app should be able to connect to the database. But there is another point. In your local the swagger definition of the models is in the root of the project and the routes in /api/v1/<endpoint> , while in Heroku they are under public . (For example, /public/api/v1/<endpoint> ).

To modify this behavior, you have to add (or modify if you already have it) a Procfile file to the root of the project . Note that it is Procfile with a capital letter.

The content of your Procfile , considering that you are using Apache, should be:

web: vendor/bin/heroku-php-apache2 public/

With those two steps:

  • Enter the environment variables
  • Create or modify the Procfile using public as document root
  • You should be able to use the app as you use it in localhost.

    Optional step

    I would recommend removing the .env file from version control using git rm --cached .env and then adding .env to your .gitignore file.

    However, it is possible that in your application you are hard-calling the file .env without verifying that it exists, something like:

    $dotenv->load(__DIR__ './../.env');
    

    If you remove it from version control that's going to throw you an error. You should wrap that call in a check that the file exists:

    if(is_readable(__DIR__ './../.env')) {
        $dotenv->load(__DIR__ './../.env');
    }
    
        
    answered by 29.05.2018 / 20:32
    source