Welcome Guest, Not a member yet? Register   Sign In
Multiple controllers per page + Multiple instances of the same model
#12

You can define public/routable methods in one of the core controllers, but I generally wouldn't recommend it. (Similarly, you can use a "normal" controller as a base class, but I wouldn't recommend that, either.)

Putting multiple controllers into the MY_Controller.php file is basically just a hack to get CI to load the controllers before your child controller is defined (because you'll get an error when PHP comes across the parent class' name after the extends keyword if the controller hasn't been loaded and can't be autoloaded). You can set them up in separate files within your /application/core directory and load them in a slightly more intelligent fashion, but I don't recall what else is required to get this working properly.

Using a "normal" controller as a base class requires you to ensure that the "normal" controller is loaded before the child class is defined, just as would be the case with any base controller defined in the core directory. In most cases, it's slightly more difficult to load a "normal" controller than a core controller, especially if your core controllers are just loading out of MY_Controller.php.

However, when it comes down to it, there's nothing technical stopping you from putting a routable method in a base controller. Just remember that routable methods are accessible via the URL, regardless of whether they're inherited from a parent controller, so you should limit the number of public methods you're inheriting.

I happen to use a base controller in development for quick mock-ups of admin controllers which include public methods to demonstrate common functionality (index, create, edit, and it is mostly functional if a model is defined and the database table(s) setup), but I don't feel like I've spent enough time considering all of the potential problems with that code to use it in production, and it tends to be confusing to open up a controller and find that the method(s) you're looking for aren't defined.
Reply


Messages In This Thread
RE: Multiple controllers per page + Multiple instances of the same model - by mwhitney - 07-22-2016, 01:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB