Server configuration:
- Ubuntu 18.04
- Apache 2.4.29
- PostgreSQL 10.6
- PHP 7.3 (I was using 7.2 but the system has decided to update it for me, I will rollback once I have resolved the current bugs)
- Composer
- Slim/Slim 3.0
- Slim/php-view
I have been building an API with the Slim PHP framework, using gothinkster's repository as a starting point. The code has been heavily adapted, though all code relating to rendering templates remains intact. When I access the website's homepage, the index.phtml
file is rendered properly, but following any hyperlinks from th homepage results in a 404
error, although the .phtml
files all reside within the same directory.
The following are excerpts of my code, excluding irrelevant configuration. Although I have only displayed one endpoint, the API defines 10 endpoints, all of which are get requests with no arguments, using .phtml
files which reside within the same folder.
Settings, routes and dependencies are called using require statements within the index.php
file.
/[api_root]/public/index.php:
if (PHP_SAPI == 'cli-server') {
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
The settings file returns an array containing a reference to the template path.
[api_root]/src/settings.php:
'settings' => [
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
]
]
Templates are assigned to the renderer within the dependencies.php
file.
[api_root]/src/dependencies.php:
$container = $app->getContainer();
// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new Slim\Views\PhpRenderer($settings['template_path']);
};
.phtml
templates are rendered within get
requests.
[api_root]/src/routes.php
$app->get('/getting-started',
function (Request $request, Response $response) {
return $this->renderer->render($response, 'getting-started.phtml');
});
The getting-started.phtml
file is located within [api_root]/templates/
.
The root /
is defined and successfully returns the index.phtml
file located within the templates directory, but when I attempt to follow any links from the homepage, I recieve a 404
error. The anchor which points to the getting-started page successfully redirects to mywebsiteurl.com/getting-started
, but the template file is not rendered and the browser responds with a 404
error.
All application files are stored privately on the server. I have created symlinks from the contents of the [api_root]/public
folder to my website's public_html
folder.
There is obviously something wrong with my definition of the path to the /templates
directory, but I can't work out how to rectify it.
EDIT:
When I created the symlinks to the public_html folder, I had forgotten to include hidden files (namely, the .htaccess
file). I have since created this symlink, though now when I attempt to access the homepage I see the default apache2 page which is displayed after apache is installed. I will note here that the website resides on a paid server. Apache is configured to use virtual hosts and I have edited the hosts file on my local machine properly. As stated, without an .htaccess
file I can view the homepage, but other routes are not being found. When the .htaccess
file exists in the public_html folder, I cannot view the homepage or any other routes.
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsiteurl.com
RewriteRule ^(.*) - [E=BASE:%1]
# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>