简体   繁体   中英

URL not found when trying to use slim framework with PHP and Apache

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?

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM