简体   繁体   中英

Slim Framework v3 running on built-in php server

I'm working on an application that uses Slim Framework v3 and I'm trying to use the built-in php server to test the code. The problem is no matter what I try I still get the stock 404 error message.

I'm running the web server with this command:

php -S localhost:8080 src/routes/api.php

And this is my code in api.php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: "GET, POST, PUT, PATCH, DELETE, OPTIONS"');
header('Access-Control-Allow-Headers: Authorization, X-Auth-Token, Content-Type');

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

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$settings = [
    'settings' => [
        'determineRouteBeforeAppMiddleware' => true,
        'displayErrorDetails' => true
    ],
];
$app = new Slim\App($settings);

// Dependency injection
$di_container = $app->getContainer();

$app->get('/help', function (Request $request, Response $response, $args) {
    return $response->withJson(['test']);
});

When I change the /help route in api.php to simply / and then visit any route in the browser I can then see the results of that / route. Any ideas?

You must rename the file api.php to index.php .

Then add: $app->run(); to the last line in index.php .

Start the app in the src/routes directory: php -S localhost:8080 index.php .

Edit: A better place for all the public files would be <project>/public/index.php

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