slimcontroller
slimcontroller copied to clipboard
Routes cannot be both GET and POST
You cannot have a route that is GET and POST, it will default to the later unlike typical Slim which will support either of a type:
$app->addRoutes([
'/' => [ 'GET' => [ 'Home:index', function()
{
die('GET');
}]],
'/' => [ 'POST' => [ 'Home:index', function()
{
die('POST'); // curl -X POST localhost:80
}]],
]);
$app->notFound(function () USE ($app)
{
header('Content-type: text/plain');
echo 'Broken...';
});
I had this issue as well. I just worked around it by branching in the controller actions. Not awesome, but workable for now.
class AccountController extends BaseController
{
public function signupAction()
{
if ($this->app->request()->isPost()) {
...
} else {
...
}
}
}
Yeah I ended up doing the same unfortunately. Was hoping this was something on the fix chart. On Dec 23, 2014 8:08 PM, "Mark Gidman" [email protected] wrote:
I had this issue as well. I just worked around it by branching in the controller actions. Not awesome, but workable for now.
class AccountController extends BaseController { public function signupAction() { if ($this->app->request()->isPost()) { ... } else { ... } } }
— Reply to this email directly or view it on GitHub https://github.com/fortrabbit/slimcontroller/issues/34#issuecomment-68024489 .
Hey guys, sorry for such a late response.
The reason your routing config isn't working is you have duplicate keys in the config array
I removed the middleware and reformatted a little to illustrate:
$app->addRoutes([
'/' => [ 'GET' => [ 'Home:index' ]],
'/' => [ 'POST' => [ 'Home:index' ]]
]);
So you're trying to set the /
key twice in the same array.
The correct syntax for defining a route with multiple method types is something like this:
$app->addRoutes([
'/' => [ 'get' => [ 'Home:index', function() { /* middleware * } ], // with middleware
'post' => [ 'Home:index' ], // without
'put' => 'Home:index' // can also be a string
]
]);
uhm... the "without" middlewear part isn't working correctly... I get an ugly
Notice: Undefined offset: 1 in .../vendor/slimcontroller/slimcontroller/src/SlimController/Slim.php on line 99
without the middleware closures...
@cs-koneko Can you post the configuration you're using?
$app = new \SlimController\Slim([
'debug' => true,
'controller.class_prefix' => '\\project\\controllers',
'controller.method_suffix' => 'Action',
'view' => new \Slim\Views\Twig(),
'templates.path' => '../app/views/'
]);
$app->addRoutes(['/' => [
'get' => ['xyzController:index'],
'post' => ['xyzController:create']
]]);
$app->run();
it looked like something like this back then ... only if i put the controller string inside a array I get the message (only the string or inside a array with the closure will work without the Undefined offset notice...