Welcome Guest, Not a member yet? Register   Sign In
Access Codeigniter rest services in model
#1

[eluser]smily[/eluser]
I had Implemented Webservices with codigniter using Rest library. I Can able to access get/post data in controller using
Code:
$this->get('email')
/
Code:
$this->post('email')
method. But when I'm trying to access the data in model using
Code:
$this->get('email')
/
Code:
$this->post('email')
it is not working.

Any ideas?

Thank in advance
#2

[eluser]PhilTem[/eluser]
Did you use @philsturgeon's REST server-library?

The problem is, the attribute for get and post variables is a protected variable of REST_Controller, hence it's only available in REST_Controller and all extending controllers. Therefore you can't access it from outside (like from any model) since they see these attributes as private e.g. actually don't see them.

The only workaround could be, to be define the variables as public by your controller that extends the REST_Controller. Just like the problem with HMVC and form-validation which up with a 2.x release.
Changed visibility should fix it.
#3

[eluser]smily[/eluser]
Thanks for your replay. Shell I user
Code:
$this->input->post('email');
in model to get post data of Web-services?
#4

[eluser]PhilTem[/eluser]
I never used @philsturgeon's REST class neither have I worked with RESTful services in any way. So I can't tell, how much REST-requests differ from ordinary requests made through your browser. But I think, they're the same. Just give it a try.

Otherwise you just have to make with standard programming practices and give attributes to your methods.
#5

[eluser]smily[/eluser]
I'm using philsturgeon’s REST server-library. So you said that "The only workaround could be, to be define the variables as public by your controller that extends the REST_Controller. Just like the problem with HMVC and form-validation which up with a 2.x release.
Changed visibility should fix it." Can you explain me how to do this?
#6

[eluser]tpetrone[/eluser]
The data passed to the REST_Controller via REST methods (PUT/POST/GET/DELETE) are only accessible at the controller level. REST just extends the CONTROLLER class, so you pass values/arrays to models are you normally would..

The most basic way to get the data from a $this->post() to a $this->model is to...

Code:
$this->load->model('my_cool_model', cool_model);

$this->cool_model->do_something_with_my_post_data($this->post());

or

Code:
$this->load->model('my_cool_model', cool_model);

$this->cool_model->do_something_with_my_post_data($this->post('email'));

I also highly recommend that you sanitize the data before you send any PUT/POST/DELETE data to the controller.

Use the "normal" form validation for this..

Example:
Code:
if($this->delete('email')){

    // why?  Form Validation only works on $_POST
    // Make sure the DELETE method values are form encoded

    $_POST = $this->delete();

     $this->form_validation->set_rules('email', 'Email', 'required');

     if ( $this->form_validation->run() == FALSE )
        {
          // do something here when this test fails
        } else {

            $this->cool_model->do_something_cool_with_this_email($_POST['email']);

        }
        

}


I am sure there are other ways to do this but simple is usually effective.









Theme © iAndrew 2016 - Forum software by © MyBB