Welcome Guest, Not a member yet? Register   Sign In
Can I call my controller form my view and get its data
#1

[eluser]bleu[/eluser]
Can I call my controller form my view and get its data such as my controller is
Code:
function get1($id1){.......  return $id;}

in my view can I call

Code:
$show_id= $this->get1('64');
#2

[eluser]boltsabre[/eluser]
Dont know... can you... it would have taken you less time to actually try it that it has for me to read, login, and reply to this.

But seriously....this is the complete back-to-front, wrong way to be approaching a MVC framework, it just simply goes against the whole principle of it in the first place, doesn't it!?!
#3

[eluser]PhilTem[/eluser]
With @wiredesignz HMVC you can. Please refer to the wiki to learn how you can do this.

But with default CI install you can't Wink
#4

[eluser]oliur[/eluser]
This is wrong as far as MVC is concerend. You should be doing the other way around.

You do all processing in your controller and load your view from the controller.

But if you need to do any processing in the view use a helper instead.

Helpers are procedural functions just save them in your application/helper folder and call any function from view as and when required.

For example, I have a helper called application/helper/project_helper.php and a function called decode_id() within the helper.

If I need to use that function to decode an encrypted id from my view file, I do

Code:
// read encrypted id
$encrypted_id = $this->input->post('id');
// decode it
$decoded_id = decode_id($encrypted_id);

Notice you do not have to use $this-> as decode_id() is a procedural function.

If you need to interact with the database or access core classess from your helper you can't just use $this->db-.....

You need to create an object first from the helper.

Code:
$ci = & get_instance();
// you can use this object to access database or config/session etc
$ci->db-query('..');
or $ci->session->set_session('..','..')

Hope it helps.

#5

[eluser]bleu[/eluser]
[quote author="oliur" date="1331917401"]This is wrong as far as MVC is concerend. You should be doing the other way around.

You do all processing in your controller and load your view from the controller.

But if you need to do any processing in the view use a helper instead.

Helpers are procedural functions just save them in your application/helper folder and call any function from view as and when required.

For example, I have a helper called application/helper/project_helper.php and a function called decode_id() within the helper.

If I need to use that function to decode an encrypted id from my view file, I do

Code:
// read encrypted id
$encrypted_id = $this->input->post('id');
// decode it
$decoded_id = decode_id($encrypted_id);

Notice you do not have to use $this-> as decode_id() is a procedural function.

If you need to interact with the database or access core classess from your helper you can't just use $this->db-.....

You need to create an object first from the helper.

Code:
$ci = & get_instance();
// you can use this object to access database or config/session etc
$ci->db-query('..');
or $ci->session->set_session('..','..')

Hope it helps.

[/quote]
Thanks that is helpful, but is this method proper , is it okay to pass a parameter to a helper from a view and get its data from the view?
#6

[eluser]oliur[/eluser]
yes perfectly normal to pass parameter and use it in view




Theme © iAndrew 2016 - Forum software by © MyBB