0

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?

digitalXmage
  • 23
  • 1
  • 7
  • 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 Oct 11 '19 at 14:37
  • I am using version Slim 3.12 – digitalXmage Oct 11 '19 at 14:46
  • What is the content of `.htaccess` file? Does this file exist at all? – Nima Oct 11 '19 at 14:56
  • Yes, i've updated my question and included it in the question description. – digitalXmage Oct 11 '19 at 15:05
  • @Entilore I have 3.12 and followed the code example for slim version 3, which is exactly the same implementation style as the website: https://www.slimframework.com/docs/v3/tutorial/first-app.html – digitalXmage Oct 12 '19 at 16:18
  • Oh yes understood. It's probably an apache issue, if you try to launch your code using the php webserver (the tutorial you're following explains how) it should work. Just be sure to try it in the folder where you index.php is located – Entilore Oct 12 '19 at 16:30
  • @Entilore I used the in built php server like you suggested, and now it works. I'm not sure why i guess it is an Apache2 issue? Regardless Thank you! my script works now. – digitalXmage Oct 12 '19 at 16:41
  • Using Apache (or nginx) is nice when you want to test your project in real conditions. But for developing your app, I'd stay on built in php server right now. – Entilore Oct 12 '19 at 16:59

2 Answers2

1

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]
Mardagg
  • 72
  • 5
0

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.

ambrous
  • 194
  • 2
  • 11
  • How would i go about doing that? – digitalXmage Oct 11 '19 at 14:47
  • I'm not very well at apache, but you can try this: ``` AllowOverride None Order Allow,Deny Allow from All FallbackResource /index.php ``` It is like in apache config for symfony https://symfony.com/doc/current/setup/web_server_configuration – ambrous Oct 11 '19 at 14:51