0

I'm starting to learn PHP and I'm trying to create an endpoint.

I installed Apache 2 and I'm placing my files under: /var/www/html/, however when I try to access it through localhost/hello/foo I'm getting a 404 error page:

Not Found

The requested URL /hello/foo was not found on this server.

This is my composer.json

{
    "require": {
        "slim/slim": "^3.0"
    }
}

And my index.php:

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

require 'vendor/autoload.php';

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
  $name = $args['name'];
  $response->getBody()->write("Hello, $name");

  return $response;
});
$app->run();

As well as my .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

I have also tried adding those files along with some others into a folder inside /var/www/html/sample and trying to access it as localhost/sample/hello/foo but I'm having the same result.

Could someone provide any advice? As I said, I'm new to PHP and probably I'm missing something, but I don't know what it is.

Edit

In the linked question as a possible dup, they say they can access their endpoints through: localhost/index.php/hello/foo however in my case, the problem stands even with that path.

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Possible duplicate of [Slim Framework always return 404 Error](https://stackoverflow.com/questions/9747640/slim-framework-always-return-404-error) – user10089632 Apr 09 '18 at 17:02
  • @Frakcool. You may want to check the Apache configuration first. Just create a new folder (let's say projectFolder) with a simple index.html. See if you can call localhost/projectFolder and get a response. If it is, then we can debug further. – Santosh Achari Apr 09 '18 at 17:05
  • @SantoshAchari thanks for your tip, I can get a response when I created a sample HTML file inside `/var/www/html/sample/index.html" and when I access `localhost/sample` I get a response back from it. – Frakcool Apr 09 '18 at 17:08
  • @user10089632 please see my edit – Frakcool Apr 09 '18 at 17:10
  • If you don't want to run the PHP web server, the answer is likely this comment: https://stackoverflow.com/questions/9747640/slim-framework-always-return-404-error#comment50175185_30633620 – drew010 Apr 09 '18 at 18:03
  • I'm gonna try it just for the learning experience as well @drew010 thanks for your comment :) – Frakcool Apr 09 '18 at 18:12
  • You're welcome. Basically, the default `AllowOverride None` means that the server is ignoring the .htaccess file that has the rewrite rules which is why the routed URLs don't work and you can only pull up files/scripts that actually exist. – drew010 Apr 09 '18 at 23:57

1 Answers1

0

Instead of trying to add the files to /var/www/html/sample, I went to my project path:

cd /path/to/project

And then I ran:

php -S localhost:8000

Now, I can access my endpoint as:

localhost:8000/hello/foo

And that's it!

Frakcool
  • 10,915
  • 9
  • 50
  • 89