Welcome Guest, Not a member yet? Register   Sign In
controller functions best practice
#1

[eluser]MikeL85[/eluser]
So, I have this URI;

CodeIgniter_2.1.3/index.php/branches/corporate

So it looks very obvious to the user where they are. Branches, and then they learn about our Corporate branch. But here’s the trouble, I’ve created a Branches.php controller. But that means I have to create a corporate function with the Braches controller, right?….. But I don’t wanna do that! I have about 12 branches in my department. I don’t want to make 12 different functions.

I'm very new to CI, Frameworks, MVC so really don't know what I'm doing.

Am I supposed to create an index function in Branches and then pass in the "Corporate" (or whatever they have selected) to it? How do I do that?
#2

[eluser]TheFuzzy0ne[/eluser]
I don't really understand the underlying structure. Does each branch serve up a different page? If so, then what's wrong with having 12 methods (one for each branch)?

You may want to look into the [url="http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping"]_remap()[/url] method or using [url="ellislab.com/codeigniter/user-guide/general/routing.html"]routes[/url]. Perhaps that's what you're after?
#3

[eluser]boltsabre[/eluser]
...
#4

[eluser]RaGe10940[/eluser]
Yea pretty much what Fuzzy said, my controller for my dash board is fat and ugly. In there I have function to check access rights, functions for all 7 of my pages that my app serves out. Logging out, I also have a function to redirect to another fat controller if need be. And going against what Fuzzy had already told me, in my case my controllers are fat usually. Even though the Model should be fatter since it does all the business logic, in my case my model does all the data gathering logic Tongue

So don't worry about have x amount of functions in a single controller Smile
#5

[eluser][email protected][/eluser]
yes, you can create one function(index) and pass the branch name as the parameter to that function and act accordingly.

Code:
controller
--------------
class Branches extends CI_Controller{

function __construct(){
     parent::construct();
}

function index($branch_name){

     //your logic

}

}

call as url
---------------
Code:
<a href="&lt;?php echo site_url('branches/index/'.$branch_name);?&gt;">&lt;?php echo $branch_name;?&gt;</a>
#6

[eluser]MikeL85[/eluser]

Thanks guys. I managed to pass the branch name in to my model from the controller. I now have one function with several if statements that will return the branch information. Woo!

I'm slowly getting used to MVC...




Theme © iAndrew 2016 - Forum software by © MyBB