Welcome Guest, Not a member yet? Register   Sign In
Get and Post methods
#1

[eluser]jiffier[/eluser]
Hi, other than CI, I use Spring framework in Java. Spring has a nice feature when using annotations so you can map a method to a GET or a POST request, for example

Code:
@RequestMapping(value="/new", method = RequestMethod.GET)
    public AppointmentForm getNewForm() {
        return new AppointmentForm();
    }

So getNewForm method will be invoked when the request is /new and the request is a GET. Wouldn't it be cool to have this in CI?
Otherwise we have do things like

Code:
function new(){

    if($this->input->post('user_id')==""){
      // GET
    }else{
      // POST
    }
}
#2

[eluser]n0xie[/eluser]
Since you usually use POST for forms (aka user input) and GET for read requests I don't see how this ever would be useful.
Code:
// GET
function product($id) {}

// POST
function order()
{
  $this->input->post('order_id');
}
#3

[eluser]jiffier[/eluser]
Yes, I think the approach you're suggesting is to use, for example, /users/edit/{user_id} to process the GET, that will display the form, and then /users/save to process the form POST. I normally use the same URL for both, which forces me to do the check for form parameters in the controller method. So yes, I think it's a pretty good approach.
Another thing.. Smile

Say we have a from with a lot of input fields. When processing it, I do the following:

Code:
function save(){

   $this->users->saveUser($user_id, $_POST);
}

And user_model is responsible of creating the data to insert, i.e:

Code:
$data['phone']=$_POST['phone'];
  $this->db->insert('user', $data);

Wouldn't it be cool to be available to "bind" request parameters to domain model objects?, i.e:

Code:
<input type="text" name="user[phone]" />


So that we get a $user array already populated by CI, and that can be automatically stored in the db with something like:

Code:
$this->db->update('user', $user);


Thanks in advance,

--
Dani
#4

[eluser]danmontgomery[/eluser]
Code:
echo $_SERVER['REQUEST_METHOD'];
// Outputs 'GET', 'HEAD', 'POST', or 'PUT'

What you're talking about is object relational mapping. There are plenty of plugins that do this, DMZ, Doctrine, etc.
#5

[eluser]jiffier[/eluser]
Not really. I mean, ORM tools have nothing to do with the web layer. What I am proposing only involves the web layer and posting form data. I'd like to map form parameters to, say, an array that represents an object in my model.
At the moment we have to do this manually, populating arrays with post data, and then (typically) this arrays are sent to the Database Library to populate a database table. This is a repetitive task that could be performed by the framework.

I think of a feature like this:

Code:
<input type="text" name="user[name]" />
<input type="text" name="user[email]" />
<input type="text" name="user[phone]" />

And then, in the controller, after the post is submitted:

Code:
$user =  $this->input->post('user');

The $user variable would contain an array with all fields populated by CI, i.e $user['phone']
#6

[eluser]danmontgomery[/eluser]
Have you tried it? I don't see any reason that wouldn't work, $_POST['user'] is an array with those 3 fields, $this->input->post() is just a wrapper for $_POST...
#7

[eluser]jiffier[/eluser]
True, that worked fine, great!




Theme © iAndrew 2016 - Forum software by © MyBB