Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Routes cannot be both GET and POST #34

Open
Gabelbombe opened this issue Dec 24, 2014 · 6 comments
Open

Routes cannot be both GET and POST #34

Gabelbombe opened this issue Dec 24, 2014 · 6 comments

Comments

@Gabelbombe
Copy link

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...';
});
@cravelight
Copy link

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

@Gabelbombe
Copy link
Author

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
#34 (comment)
.

@yaworsw
Copy link
Contributor

yaworsw commented Jan 27, 2015

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

@chrisWhyTea-zz
Copy link

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

@yaworsw
Copy link
Contributor

yaworsw commented Mar 10, 2015

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

@chrisWhyTea-zz
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants