CleverStack / clever-controller

Lightning-fast flexible controller prototype
MIT License
9 stars 5 forks source link

get/post Action problem #20

Open likegun opened 9 years ago

likegun commented 9 years ago

I have two request GET /user/login for render a login page and POST /user/login for login. Is there any way to handle this except:

loginAction: function() {
  if (this.isGet()) {
    // render login page
  } else if (this.isPost()) {
    // process login request
  }
}
pilsy commented 9 years ago

Hi @likegun, there are three distinct patterns/methods that can be used to route to any Controller's Action.

  1. restfulRouting - This maps the GET|PUT|POST|DELETE /controllerName http methods to actions: (/controllerName is set by your controllers route: '')
    • listAction (Special Case: GET /controllerName will go to Controller.listAction() - If no id is present)
    • getAction
    • postAction
    • putAction
    • deleteAction

Also it is worthy to note that It is possible to custom map your own verb's or http methods to actions by taking advantage of the prototypical inheritance provided by the Controller Class.

  1. actionRouting - This is the method you are using, i would recommend either using two separate routes or using a middleware function to get rid of the if statement and funnel the requests into two distinctly separate controller actions. (if you want help with doing this i'll need you to paste in some code, just let me know)
  2. Specifically attaching a route (binding) to a specific controllers Action. (like loginAction) - This is done using the Controller.attach() method along with app.get (post/put/delete/all etc) method provided by express. (or restify, whatever your using - altho i'm guessing express).

Examples of routes can be found here - it is probably a good idea to set the controller's autoRouting flag to false and bind each of the controllers routes individually/specifically to ensure your controller responds to routes exactly as you wish - Altho to be honest with you i haven't ran into many problems with it set to true, however when i did run into it - it was causing a confusing routing issue... something to note.

Take a look at https://github.com/CleverStack/clever-controller#routing in the "Manual Routing" section as well :+1:

Cheers, Richard

likegun commented 9 years ago

I am sorry for replying so late.

I have read your suggestions carefully and it seems that I should to bind each of the controllers routes individually to solve my problem.

To be honest it`s a effective way but a bit troublesome.

I used to use laravel(a php framework) to develop project and I think it`s very comfortable to use the controller of the laravel.

For example:

ExampleController.php :

.....

function getHello() {

}

function postHello() {

}

.....

routes.php:

.......... Route::controller('example','ExampleController');

So that I can visit [GET] /example/hello and [POST] /example/hello.

I think it would be better if clever-controller could support this feature.

Just like :

getHelloAction: function() {

},

postHelloAction: function() {

}

If you think it`s a useless suggestion,just ignore it.

Ps: My english is very poor so I am sorry this reply would be difficult to understand.

^_^

2015-04-12 6:02 GMT+08:00 Richard notifications@github.com:

Hi @likegun https://github.com/likegun, there are three distinct patterns/methods that can be used to route to any Controller's Action.

  1. restfulRouting - This maps the GET|PUT|POST|DELETE /controllerName http methods to actions: (/controllerName is set by your controllers route: '')
    • listAction (Special Case: GET /controllerName will go to Controller.listAction() - If no id is present)
    • getAction
    • postAction
    • putAction
    • deleteAction

Also it is worthy to note that It is possible to custom map your own verb's or http methods to actions by taking advantage of the prototypical inheritance provided by the Controller Class.

1.

actionRouting - This is the method you are using, i would recommend either using two separate routes or using a middleware function to get rid of the if statement and funnel the requests into two distinctly separate controller actions. (if you want help with doing this i'll need you to paste in some code, just let me know) 2.

Specifically attaching a route (binding) to a specific controllers Action. (like loginAction) - This is done using the Controller.attach() method along with app.get (post/put/delete/all etc) method provided by express. (or restify, whatever your using - altho i'm guessing express).

Examples of routes can be found here https://github.com/CleverStack/backend-example-module/blob/master/routes.js#L3

  • it is probably a good idea to set the controller's autoRouting flag to false and bind each of the controllers routes individually/specifically to ensure your controller responds to routes exactly as you wish - Altho to be honest with you i haven't ran into many problems with it set to true, however when i did run into it - it was causing a confusing routing issue... something to note.

Cheers, Richard

— Reply to this email directly or view it on GitHub https://github.com/CleverStack/clever-controller/issues/20#issuecomment-91933797 .