I'm trying to build an API with PHP, I have the logic files stored in different folders, I followed some tutorial to make a PHP router and it works locally for example:
localhost:4000/products
But when I use the same URL when the app is hosted on heroku it doesn't work.
https://hostedapp.herokuapp.com/products
and gives this error:
Warning: include(view/products.php): Failed to open stream: No such file or directory in /app/index.php on line 12
Warning: include(): Failed opening 'view/products.php' for inclusion (include_path='.:') in /app/index.php on line 12
I've been trying for the past few days to change the way the paths are written and checked all StackOverflow questions related to it but nothing seems to work.
index.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('router.php');
route('/',function(){
echo "Index page";
});
route('/products',function(){
include('view/products.php');
});
route('/product-delete',function(){
include('view/product-delete.php');
});
route('/product-add',function(){
include 'view/product-add.php';
});
$endpoint = $_SERVER['REQUEST_URI'];
dispatch($endpoint);
?>
This is router.php
<?php
$routes = [];
function route($endpoint, $callback){
global $routes;
$endpoint = trim($endpoint, '/');
$routes[$endpoint]=$callback;
}
function dispatch($endpoint){
global $routes;
$endpoint = trim($endpoint,'/');
$callback=$routes[$endpoint];
return call_user_func($callback);
}
?>
and this is.htaccess (Copied it from a StackOverflow answer)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
#RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .* index.php/$0 [PT,L]
EDIT: I tried include(__DIR__) . '../subfolder/file.php' and include(__DIR__ . '../subfolder/file.php') and it gives the same error.
I also tried the that shows when I echo DIR or $_SERVER['DOCUMENT_ROOT'] on heroku which is just /app/ and it still gives the same error. /app/subfolder/file.php.