I want to load a controller from a function in another controller because the library I integrated to my project I don't want to load it to the controller because I want to keep it clean and related.
I tried using modules but I still had to put controller in the url like
http://example.com/maincontroller/function
http://example.com/othercontroller/function
I have default controller so I can load http://example.com/function so how could I access the controller from a function from main so I don't have to put the controller in the url.
I'm still willing to use HMVC if I can load the controller function from the main controller function.
you cannot call a controller method from another controller directly
my solution is to use inheritances and extend your controller from the library controller
in your controller we call it
Mycontoller
it will extendsController1
and you can call methodA from mycontroller
http://example.com/mycontroller/methodA
http://example.com/mycontroller/methodB
this solution worked for me
I came here because I needed to create a
{{ render() }}
function in Twig, to simulate Symfony2's behaviour. Rendering controllers from view is really cool to display independant widgets or ajax-reloadable stuffs.Even if you're not a Twig user, you can still take this helper and use it as you want in your views to render a controller, using
<?php echo twig_render('welcome/index', $param1, $param2, $_); ?>
. This will echo everything your controller outputted.Here it is:
helpers/twig_helper.php
Specific for Twig users (adapt this code to your Twig implementation):
libraries/Twig.php
Usage
I had a similar problem. I wanted to have two controllers:
homepage.php - public facing homepage
home.php - home screen once a user was logged in
and I wanted them both to read from 'mydomain.com'
I was able to accomplish this by setting 'hompepage' as the default controller in my routes config and adding a remap function to homepage.php
While the methods above might work, here is a very good method.
Extend the core controller with a MY controller, then extend this MY controller for all your other controllers. For example, you could have:
Then your other controllers could then extend this as follows:
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers,
controller1
andcontroller2
.Controller1
has a methodmethod_1
- andcontroller2
has a methodmethod_2
.You can set up routes like this:
Then, you can call method 1 with a URL like
http://site.com/method_1
and method 2 withhttp://site.com/method_2
.Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
Create a helper using the code I created belows and name it controller_helper.php.
Autoload your helper in the
autoload.php
file underconfig
.From your method call
controller('name')
to load the controller.Note that
name
is the filename of the controller.This method will append
'_controller'
to your controller'name'
. To call a method in the controller just run$this->name_controller->method();
after you load the controller as described above.