Welcome Guest, Not a member yet? Register   Sign In
Where do I stick it? (be nice now)
#1

[eluser]Unknown[/eluser]
I'm recreating a functioning site in CI and finding it challenging to know where to put some code: main controller, helper, directly in the view, or other place. I need to create an anchor tag that will include some dynamic mouseover JavaScript code and a dynamic link as well as. They will vary depending on a provided query parameter, or in CI's case, the method's second URL section. So, if the second parameter is a 1, the anchor tags target controller must change from the default to, say, the admin controller, which will load the data into an admin page so the viewer can edit that.

It would be a simple task of checking a value in the data[] array, like $IsAdmin, but I want to dynamically vary the anchors and javascript methods based on some other data in the $query, so I thought I should encapsulate the more complex logic in a separate function. However, where should I store it (controller itself, plugin, library), and how can I include it for the view to be able to call the function which will do the actual dynamic anchor creation.

Thanks in advance.

Jme
#2

[eluser]John Pantoja[/eluser]
Well, everything should go to your controller( or should I say does). You two have options (or more), process some of the logic directly inside your controller or hand some of that off to routes.php . You can use regular expression to do some of your processing and then have your controller verify before it does anything else. Check the CI docs for URL routing to help you get started.
#3

[eluser]John Pantoja[/eluser]
I should throw you more information. If you're not familiar with MVC, I do believe CI wiki has a good prep. In a nutshell: (and if I understand CI correctly .. not the MVC part)

Model - your database interaction code, CRUD
View - your html and code to display your dynamic or static data
Controller - your application logic to interact between your model and view and other code.

Library - Your code that extends CI or application specific code (OOP)
Helper - highly specific functions, collection of functions, good old fashion procedural code.
Plugins - basically just one function and procedural as well
#4

[eluser]Unknown[/eluser]
To follow up and provide some guidance to others, here's what I've done so far that has worked pretty well:
1. included the new library in my controller like this: $this->load->library('my_library');
2. call functions from the controller as usual: $this->my_library->formatURL($params)
3. call back to the model from the library as follow:
Code:
//this is a class level variable, declared just under the class declaration
•  var $CI;
//called from the constructor
•  $this->CI = get_instance();  
//called from the constructor
•  $this->CI->load->model('myinfo_model');
//called from any function
•  $myinfo=$this->CI->myinfo_model->get_myinfo($cat_id);

Now, I can reuse this library's functions in various controllers instead of duplicating the functions. By separating my functions into libraries like this, my controllers can have actions that contain little code themselves, but rather call to the libraries. Not necessarily the simplest approach, but cleaner and more flexible, which is what I understand to be the power of the MVC approach.

Any feedback or suggestions?




Theme © iAndrew 2016 - Forum software by © MyBB