0
votes

I have this code using the slim framework and php with apache2 on linux:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../../vendor/autoload.php';

$app = new \Slim\App;

#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
Which version os Slim are you using? It looks like slim 3, but your instanciation of the App is weird (slim 3 requires a config to be given)Entilore
I am using version Slim 3.12digitalXmage
What is the content of .htaccess file? Does this file exist at all?Nima
Yes, i've updated my question and included it in the question description.digitalXmage
@Entilore I have 3.12 and followed the code example for slim version 3, which is exactly the same implementation style as the website: slimframework.com/docs/v3/tutorial/first-app.htmldigitalXmage

2 Answers

1
votes

You can tell Apache to redirect all routes to index. Try something like in your vhost or in a .htaccess file :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA, L]
0
votes

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.