If I die() at top of router.php, the post-method from the form seems to arrive there.
But as the code continues to router’s post method (seen above), the error is popped: 405 Method not allowed: must be one of: (GET)
Any idea how to fix this or have a work-around?
Added: My Router GET-requests do work good, as well as .htaccess -file is configured just as Slim recommends. Also if I do a traditional _form _POST method_ to another .php page (without the router), things work fine, however then I don't know how I could use _form action_ to direct the _POSTed values to a specific Class and Method. That’s why I needed the Router to work.
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class CartController
{
public static function update(ServerRequestInterface $request, ResponseInterface $response, $args){
die("OK, got this slug: " . $args['slug'] );
}
}
Yeah, I forgot that on when doing my tests as supposed by marcelbonnet, normally I don’t have them.
It seems to go OK if I POST anything directly from the form, say to Controller, using absolute path at in form action: /test/App/Controllers/CartController.php. The question now is, how do I target the request to a specific function in there if I do it this way?
Everything works fine as long as I don’t have to pass through third parameter via my http request. In this case it’s $slug (=$args kind of thing to identify a product). The slug is needed in my Cart Controller in order to identify product in upcoming SQLs.
So, when I add that $slug -thing to the POST method of my form {slug: item.slug}, and I try to route it forth in my Router: post(‘cart/{slug}’, … I get the Error 405.
Question: What may cause an error with POST methods “$args” -section? (in this case the $slug)
And there’s is also the same issue remaining: How do I pass the $slug -variable within the POST method, because the update2 -method actually requires (Request $request, Response $response, $slug). For sake of clarity I’ve taken it out in the above example cos I cannot get the normal route post work still…
In that case I’m sorry to say that don’t know why you’re getting 405 error. I’m guessing it’s something simple/stupid, but I’m just not seeing it.
For a post form, there’s no reason to pass a slug (since it’s not visible to the user you can just pass an object-id) but you can define a route like this:
in your form action tag. Your controller of course needs to know whether it’ll be receiving an ID or a slug, but the processing logic is the same for both versions.
Alternatively you can just leave the route without the “/{id}” part and pass a hidden form variable containing the objectID/slug (as you prefer) to your form submission handler in your controller.