I'm trying to migrate our website from a linux vm (also on google) to google App Engine Standard enviroment.
When i deploy the app and test it the main page (index.php) works fine but when i try to go to other files, for example /somefolder/somefile.php it doesnt. It just shows the index.php but without the pictures etc.
I searched the internet and i found that this is probably due to not having a front end controller(?)
My app.yaml file is as followed:
service: nameoftheapp
runtime: php83
handlers:
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
static_files: \1
upload: .+\.(gif|png|jpg)$
- url: /(.+\.php)$
script: auto
- url: /.*
script: auto
my index.php is:
<?php
// android store
if (preg_match('#android#i', $_SERVER ['HTTP_USER_AGENT'])) {
header('Location: market://details?id=nl.myapp');
exit;
}
// ios
if (preg_match('#(iPad|iPhone|iPod)#i', $_SERVER ['HTTP_USER_AGENT'])) {
header('Location: https://apps.apple.com/us/app/myapp/id973246494');
exit;
}
echo "<html> <head> </head> <body>";
echo '<center><p><br><p><br>Sometext</center> <p>';
echo '<center> some more text.</center> <p>';
echo "<center> <img width='800' src='images/logo_GW_forweb.jpg'</center> <p>";
echo "<center> yet some more text</center> <p>";
echo "</body>";
?>
the index.php serves as a simple landing page for users to redirect them to the appstores for the app. As far as this goes, this works well. also the logo, which resides in a subfolder is shown.
But i myself want to go to https://mywebsite.nl/somefolder/somefile.php
This part doesnt work. Can this be resolved by just setting the right app.yaml (i do have like 10 subfolders with some having their own subfolders and a total of 100+ .php files)
Do i need something else? I was hoping there would be a settings for the app.yaml that routes all reguests to the right place.
I made a test app to see how to get it working. This one works, but i doubt this is the way to go.
The app.yaml file states:
runtime: php83
service: test
handlers:
- url: /.*
script: index.php
And the index states:
<?php
$requestUri = $_SERVER['REQUEST_URI'];
// Hier kun je logica toevoegen om het verzoek te routeren naar de juiste actie/controller
if ($requestUri === '/test/test.php') {
require 'test/test.php';
} elseif ($requestUri === '/root.php') {
require 'root.php';
} else {
// Standaard HTML-tekst als de URI niet overeenkomt met specifieke routes
echo "<!DOCTYPE html>";
echo "<html lang=\"en\">";
echo "<head>";
echo " <meta charset=\"UTF-8\">";
echo " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">";
echo " <title>Test App</title>";
echo "</head>";
echo "<body>";
echo " <h1>Hallo dit is een test</h1>";
echo " <p>Welkom bij de PHP-testapplicatie op Google App Engine!</p>";
echo " <p><a href=\"test/test.php\">Ga naar test.php</a></p>";
echo " <p><a href=\"root.php\">Ga naar root.php</a></p>";
echo "</body>";
echo "</html>";
}
?>
This one works. i can access the root.php as well as the test.php which is located in the subfolder test. But i doubt this would be the way to go for my own website.
You're right that you need a front controller (ref). Google recommends using a framework like Laravel, Symfony, or Slim.
If you would rather use your index.php
file (instead of a framework) and you also have a lot of sub-paths (sub folders), you should write your front controller code to use regex to detect the routes.
This is an example from Google and the use case is similar to yours - user lands on the home page (index.php
) which has links that take user to other parts of the site