I have this code using the slim framework and php with apache2 on linux:
<?php
use PsrHttpMessageServerRequestInterface as Request;
use PsrHttpMessageResponseInterface as Response;
require '../../vendor/autoload.php';
$app = new SlimApp;
#doesn't work results in URl not found in browser
$app->get('/hello','sayhi');
#works, when i just type in 'localhost/' into my webbrowser
$app->get('/',function()
{
return 'testing';
});
$app->run();
#functions
function sayhi()
{
echo 'hello world again';
}
.htaccess (same directory as my index.php file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
000-default.conf has the documentroot set to the directory of where my php file and .htaccess file.
When i type in ‘localhost/’ into my web browser the anonymous function is executed. As that’s what it will do if the server recieves ‘/’
However when i type in ‘localhost/hello’ it results in URL not found, when it should execute the ‘sayhi’ function when the server receives this type of url.
Why is it doing this? is there something wrong in my code, as i don’t understand how to go about sorting this.
I also tried setting the option to the directory of my php file
AllowOverride All
However this results in a 500 Internal error, when i have this option set. I tried following this guide: 500 internal error guide to no avail.
I have no idea how to sort this, help?
2
Answers
I think you need to configure apache so that all requests would go into index.php in which your code are located. It seems that when you request
localhost/hello
your web server tries to find file hello.You can tell Apache to redirect all routes to index. Try something like in your vhost or in a .htaccess file :