DEV Community

Milan Felix Šulc for NetrixOne

Posted on • Edited on

Deploy Static Frontend + PHP Files Using ZEIT Now

I am quite a fan of ZEIT company and even more of their tool called Now.

I consider myself mainly as PHP developer, but I also like JavaScript. In these days static site generators are raising and I am totally into it.

So how to take advantage of static frontend but with dynamic backend written in PHP?

Since November 2019 it's simple as possible with ZEIT Now.

👀 https://imgur.com/V7CcInl


Minimal project structure looks like this, you gonna need only 3 files.

project/
├── api/
│   ├── index.php
├── now.json
└── index.html

File index.html contains static frontend.

<html>
<head></head>
<body>

#
# Fetch data from /api/index.php using Fetch API
#

</body>
</html>

File api/index.php contains dynamic data or expose API endpoint.

<?php

header('conten-type: application/json');
echo json_encode(['tech' => 'ZEIT Now']);

File now.json setup deployment.

{
  "functions": {
    "api/index.php": {
      "runtime": "now-php@0.0.7"
    }
  }
}

Finally you can call now command and see what happened.

View this example on Github.

Top comments (0)