简体   繁体   中英

PHP Composer throwing 404 URL not found

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.

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!

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