1

I am trying to build Slim Framework Version 4 api, for my angular application.

On every request i make, i get

Type: Slim\Exception\HttpMethodNotAllowedException
Code: 405
Message: Method not allowed. Must be one of: POST
File: G:\xampp\htdocs\slim\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php
Line: 96
Trace: #0 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\Routing\RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(98): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request))
#2 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\MiddlewareDispatcher.php(140): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#3 G:\xampp\htdocs\slim\app\Middlewares\RequestUser.php(19): class@anonymous->handle(Object(Slim\Psr7\Request))
#4 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\MiddlewareDispatcher.php(283): App\Middlewares\RequestUser->__invoke(Object(Slim\Psr7\Request), Object(class@anonymous))
#5 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Psr7\Request))
#6 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\App.php(211): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#7 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\App.php(195): Slim\App->handle(Object(Slim\Psr7\Request))
#8 G:\xampp\htdocs\slim\public\index.php(42): Slim\App->run()
#9 {main}
[Tue Feb 18 11:38:13 2020] ::1:57190 [200]: /login
[Tue Feb 18 11:38:14 2020] ::1:57191 [200]: /login

The good part is my api is working fine as expected. But I get to see this error on every call.

How can i overcome this.

If i try a get call from my browser, then there is no error.

enter image description here

If i make an ajax call, the Network tab shows no error

enter image description here

But my command prompt throws this error

enter image description here

Edit 1

Adding route definitions

$app->get('/',  '\App\Controllers\HomeController:home');
$app->post('/login',  '\App\Controllers\UserController:login');

Edit 2

Adding angular code

login(data){
    const url = environment.api+'login';
    return this.http.post(url, data);
  }

Answer i need to add

$app->options('/{routes:.+}', function ($request, $response, $args) {
    return $response;
});

in my index.php to accept all Options request

http://www.slimframework.com/docs/v4/cookbook/enable-cors.html

Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
  • Please include route definitions in your question. – Nima Feb 18 '20 at 06:38
  • Please also include your front-end(Angular JS) code on how you call your API's. – kwingkwingko Feb 18 '20 at 06:43
  • 405 Not Found Error is a method name not found. Check your front-end side maybe you are calling a POST API and you used GET. Just double-check it. – kwingkwingko Feb 18 '20 at 06:46
  • Or double check the path in your routes maybe there are some similar path that you are calling. – kwingkwingko Feb 18 '20 at 06:48
  • You are defining `/login` route to accept `POST` requests only, and `/` to accept `GET` requests. Trying to send a GET request to `/login` (i.e trying to reach `localhost:8888/login` by typing the address in a browsers address bar) produces a `405`, as well as trying to send a POST request to `/`. So it is important to send a proper request to each route. If you need a clean solution, please consider the part of code that is responsible for sending requests to these URLs and describe the desired behavior of the code. – Nima Feb 18 '20 at 07:10

2 Answers2

1

I had the same error for POST request ( not for GET ) caused by CORS.

I added route ( config/routes.php ):

// Post that cause an error
$app->post('/notes', NoteCreateAction::class);
// Allow preflight requests for /notes
$app->options('/notes', function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
    return $response;
});

Solution was founded here: https://odan.github.io/2019/11/24/slim4-cors.html

0

There are two ways for deploy PHP-SLIM FRAMEWORK V4:

1. Run composer command:

>composer start 

2. Deploy with apache server

a. In the file ‪C:\xampp\apache\conf\extra\httpd-vhosts.conf uncomment this config

NameVirtualHost *:80

b. In the file C:\xampp\apache\conf\extra\httpd-vhosts.conf put this config at finish

<VirtualHost *:80>
DocumentRoot C:\xampp\htdocs\myBackendServer
<Directory "C:\xampp\htdocs\myBackendServer">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

c. Finally, in the browser can you open: http://localhost/