slimcontroller icon indicating copy to clipboard operation
slimcontroller copied to clipboard

Routes cannot be both GET and POST

Open Gabelbombe opened this issue 10 years ago • 6 comments

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...';
});

Gabelbombe avatar Dec 24 '14 01:12 Gabelbombe

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 {
        ...
        }
    }
}

cravelight avatar Dec 24 '14 04:12 cravelight

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 .

Gabelbombe avatar Dec 24 '14 04:12 Gabelbombe

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
           ]                                  
]);

yaworsw avatar Jan 27 '15 15:01 yaworsw

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...

chrisWhyTea-zz avatar Mar 02 '15 18:03 chrisWhyTea-zz

@cs-koneko Can you post the configuration you're using?

yaworsw avatar Mar 10 '15 16:03 yaworsw

$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...

chrisWhyTea-zz avatar Mar 10 '15 20:03 chrisWhyTea-zz