3

i have a bunch of controllers ie, users,admin and guest i have tons of functions on the admin controller that i want to use on the users. Since accessing a controller would mean to redirect it, i would like that behaviour not to happen, so the question is

  • Can i use a controller like a helper/model and load it? loading it like a helper/model would be easier for me since there are a tons of controllers and i do not want to add a new file.

im still new in C.I please guide me.

hyperman
  • 33
  • 3
  • Ideally, you should make a common parent controller that would include all the reusable code that the other controllers would inherit. – Shomz Oct 15 '14 at 15:46
  • https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/ Take a read – Eujinks Oct 15 '14 at 15:49

2 Answers2

1

You can create helper classes which will perform some logic and you can call them either in the controllers or on the views. I do not know if it is that that you want. For example, i have created a folder called Repository (repository pattern) where i implement for example the logic to render a menu:

 public class reposAccount
 {
    public static List<Sp_ShowMenuList_Result> ShowMenuList(int RoleID)
    {
            myEntities db = new myEntities();
            List<Sp_ShowMenuList_Result> ListShowMenu = db.Sp_ShowMenuList(RoleID).ToList();
            return ListShowMenu;

    }
 }

And the on the View i just call that "Helper":

 if (HttpContext.Current.User.Identity.IsAuthenticated)
{
   //do some preparatory activities, then i call the helper:
    foreach (var obj in reposAccount.ShowMenuList(RoleId).Where(z => z.ParentMenu == null).OrderBy(z => z.MenuOrder).ToList())
    {
        @: <li>@Html.ActionLink(obj.MenuName, "Index", obj.Controler_Name)</li>
    }
}

The same behaviour or the methods, you could call them on each controller.

Turay Melo
  • 114
  • 5
0

You can load it as a normal model or library using

$this->load->library()

here is a SIMILAR QUESTION you might want to look at, the answer you are looking for is the third one.

NOTE:

if you are using CI3 this will not work. its helpful if you also read the comments.

Community
  • 1
  • 1
tomexsans
  • 4,454
  • 4
  • 33
  • 49