slimphp / Slim-Skeleton

Slim Framework 4 Skeleton Application
http://www.slimframework.com
MIT License
1.59k stars 478 forks source link

Status code 405 #233

Closed jagadishnallappa closed 3 years ago

jagadishnallappa commented 3 years ago

When this project is run from netbeans and wamp server, I get the following error. { "statusCode": 405, "error": { "type": "NOT_ALLOWED", "description": "Method not allowed. Must be one of: OPTIONS" } }

Please help in solving this. Thank you very much.

l0gicgate commented 3 years ago

Can you give more context? A simple index.php sample with a minimum amount of code to replicate the issue, request information, etc.

Method not allowed is typically when a route exists but the method you’re trying to request is not mapped.

jagadishnallappa commented 3 years ago

Can you give more context? A simple index.php sample with a minimum amount of code to replicate the issue, request information, etc.

Method not allowed is typically when a route exists but the method you’re trying to request is not mapped.

I've used composer to get this project into my system into www folder of wamp. Then when I try to run it, I get the above mentioned error. Perhaps I'm doing something wrong...

eugene-borovov commented 3 years ago

Please check #221. Sometimes this error correspond with 404. I find useful this #214

jagadishnallappa commented 3 years ago

It runs correctly with the hello world message if run on php in built server. But the problem arises when run on wamp server ... :(

$app->setBasePath('/myapp'); Doing this also returns the same

{ "statusCode": 405, "error": { "type": "NOT_ALLOWED", "description": "Method not allowed. Must be one of: OPTIONS" } }

Sorry. Perhaps I am doing something wrong. Please help. I could post whatever information you would need to help me.

jagadishnallappa commented 3 years ago

I still can't seem to get it working... :( Someone, please help me. I am willing to share all of my configuration files. Been on it since the last 1 week... Tried everything but still can't seem to get it working...

jagadishnallappa commented 3 years ago

This is my index.php files for your reference.

<?php
declare(strict_types=1);

use App\Application\Handlers\HttpErrorHandler;
use App\Application\Handlers\ShutdownHandler;
use App\Application\ResponseEmitter\ResponseEmitter;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;

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

// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();

if (false) { // Should be set to true in production
    $containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}

// Set up settings
$settings = require __DIR__ . '/../app/settings.php';
$settings($containerBuilder);

// Set up dependencies
$dependencies = require __DIR__ . '/../app/dependencies.php';
$dependencies($containerBuilder);

// Set up repositories
$repositories = require __DIR__ . '/../app/repositories.php';
$repositories($containerBuilder);

// Build PHP-DI Container instance
$container = $containerBuilder->build();

// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
$callableResolver = $app->getCallableResolver();

// Register middleware
$middleware = require __DIR__ . '/../app/middleware.php';
$middleware($app);

// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);

/** @var SettingsInterface $settings */
$settings = $container->get(SettingsInterface::class);

$displayErrorDetails = $settings->get('displayErrorDetails');
$logError = $settings->get('logError');
$logErrorDetails = $settings->get('logErrorDetails');

// Create Request object from globals
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();

// Create Error Handler
$responseFactory = $app->getResponseFactory();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);

// Create Shutdown Handler
$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails);
register_shutdown_function($shutdownHandler);

// Add Routing Middleware
$app->addRoutingMiddleware();

// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logError, $logErrorDetails);
$errorMiddleware->setDefaultErrorHandler($errorHandler);

// Run App & Emit Response
$response = $app->handle($request);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
eugene-borovov commented 3 years ago

Let`s see your app/routes.php file and the request information.

jagadishnallappa commented 3 years ago

Lets see yourapp/routes.php` file and the request information.

<?php

declare(strict_types=1);

use App\Application\Actions\User\ListUsersAction;
use App\Application\Actions\User\ViewUserAction;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;

return function (App $app) {
    $app->options('/{routes:.*}', function (Request $request, Response $response) {
        // CORS Pre-Flight OPTIONS Request Handler
        return $response;
    });

    $app->get('/', function (Request $request, Response $response) {
        $response->getBody()->write('Hello world!');
        return $response;
    });

    $app->group('/users', function (Group $group) {
        $group->get('', ListUsersAction::class);
        $group->get('/{id}', ViewUserAction::class);
    });
};
jagadishnallappa commented 3 years ago

No change at all in the skeleton. Serving this from a php built in server gives me no problem. It works fine. But this issue arises when I run from the wamp server... Now, please don't abandon me. Please continue to help. I am willing to share phpInfo()

eugene-borovov commented 3 years ago

Webroot must point to public directory of the skeleton. If deploy skeleton under webroot then setup base path to public.

jagadishnallappa commented 3 years ago

Webroot must point to public directory of the skeleton. If deploy skeleton under webroot then setup base path to public.

Sorry. I don't understand...

jagadishnallappa commented 3 years ago

Webroot must point to public directory of the skeleton. If deploy skeleton under webroot then setup base path to public.

It would help if you gave me an example.

eugene-borovov commented 3 years ago

This starts built-in web server

$ php -S 0.0.0.0:8080 -t public

This means:

 -S <addr>:<port> Run with built-in web server.
 -t <docroot>     Specify document root <docroot> for built-in web server.

I called <docroot> webroot. So your WAMP document root must point to public.

I think your problem is not in the application, but in the configuration of the WAMP web server.

jagadishnallappa commented 3 years ago

This starts built-in web server

$ php -S 0.0.0.0:8080 -t public

This means:

 -S <addr>:<port> Run with built-in web server.
 -t <docroot>     Specify document root <docroot> for built-in web server.

I called <docroot> webroot. So your WAMP document root must point to public.

I think your problem is not in the application, but in the configuration of the WAMP web server.

I am sorry to trouble you. But how and where do I configure the wamp server to specify the docroot/webroot? Please help. I am not a backend person. I am now forced to do the backend as well for a project.

eugene-borovov commented 3 years ago

I've used composer to get this project into my system into www folder of wamp. Then when I try to run it, I get the above mentioned error. Perhaps I'm doing something wrong...

I assume your www folder is the docroot of http://localhost. So you should set up base path to public ( $app->setBasePath('/public'); and request http://localhost/public/. In case WAMP adds some subdirectory you must configure it in application base path. Assuming WAMP www points to http://localhost/myapp then you should $app->setBasePath('/myapp/public'); and request http://localhost/myapp/public/.

I've run out of ideas on how to help you.

jagadishnallappa commented 3 years ago

I've used composer to get this project into my system into www folder of wamp. Then when I try to run it, I get the above mentioned error. Perhaps I'm doing something wrong...

I assume your www folder is the docroot of http://localhost. So you should set up base path to public ( $app->setBasePath('/public'); and request http://localhost/public/. In case WAMP adds some subdirectory you must configure it in application base path. Assuming WAMP www points to http://localhost/myapp then you should $app->setBasePath('/myapp/public'); and request http://localhost/myapp/public/.

I've run out of ideas on how to help you.

I still get the same error after adding the following line right after

// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
$app->setBasePath('/slim/public');
l0gicgate commented 3 years ago

Closing this as stale.

Payn commented 2 years ago

Had the same issue.

If app is for example in 'slim' subdirectory of www root, correct fix is adding only $app->setBasePath('/slim');

and accessing it through http://localhost/slim.