Call controller within another controller - CodeIgniter - codeigniter

I need to call a controller say 'faq_view' inside admin controller as the URL structure admin/faq_view like this how I can do this?
e.g:
site.com/maincontroller/function
site.com/maincontroller/othercontroller/function

Then, just redirect the page. Else if you want to just call the function, call it via AJAX.
It depends what you exactly want to do. If you want to just invoke the function, its not the right way. Controller as it defines itself controls the flow of the pages that comes on sequence. Controller is responsible to send commands to its associated view to change the view's presentation of the model.
So, if you are saying you want to call controller within another controller, that should mean you are about to redirect to another page.
Updated answer:
Just assume you have new_function on maincontroller that calls the function from othercontroller. The function does not need to be defined on othercontroller.
Add the following line on routes.php.
$routes['maincontroller/new_function'] = 'othercontroller/new_function';
Now, you can call the function of othercontroller as maincontroller/new_function.

You can always call a controller inside another controller, but this only works for calling one controller as far as I have tried. Let's say you are trying to load a controller inside a controller. You can try this:
$this->load->library('../controllers/myothercontroller');
Then do this:
$this->myothercontroller->function_name();
That's it! You can now access any function inside myothercontroller (controller) in your current controller. I hope this helps too.

Your controllers are part of the presentation layer and should not contain application logic. That means you should never need to call a controller from another controller, instead refactor your application and move the domain logic to the model layer.
Now if you have a method that you need in multiple controllers, say for example you need a template method that automatically adds your header and footer views.
If that is the case, create a base class that your controllers extend.
If you are talking about just a routing issue, then just use the routes file for that. I don't like the CI automatic routing and it should be avoided as it will result in duplicate URLs for the same resource.

Related

Laravel do i need two controllers to handel two store functions?

Basically what i want to do is that 1 Controller handels two store functions called like this:
public function singleupload(){
..some code
}
and
public function multiupload(){
..some code too
}
as i continued to define the routes (get/post)
Route::get('/MultiUpload', 'controller#MultiUpload');
Route::get('/SingleUpload', 'controller#SingleUpload');
Route::post('/MultiUpload', 'controller#storeMulti');
Route::post('/SingleUpload', 'controller#storeSingle');
and as i tried to post some data to my database it tells me that there is no 'store' function. So i opened the docs and found this:
POST /photos store photos.store
So my question is can i create two store functions in one controller or do i have to create a different one so every route has its own controller with its own store function?
You are doing some things wrong.
First of all follow Repository Pattern.
You should always write all common functions in Repository which can be accessible in entire Project.
You should use controller only to fetch the request from the Route and pass all the logic to the Repository.
These Process will help you reduce all you coding lines.
Hope this helps !!!
cheers!!
NO you don't need to create new Controller. You can add new action for this.
But it also, depends on the how is your application functionality.
Normally, i personally recommended to create generic function or traits or add the functionality in base controller.
you can handle multiple store functions in one controller there is no need to create 2 different controllers.

How can I find which view from which controller calling an action?

For example I have an "action-one" in "controller-one", if it called from views of that controller, return is different when it called from views of another controllers; So I should know which view from which controller calling an action.
It is easier to just have a flag, as an optional method parameter that you would use to specify and check in your controller.
But it is better to keep shared code in a different method if possible and have different actions for different purposes.
If what you needed was something that would not necessarily need a different action, then just pass the action name or any other identifier you wish to use and then check and do the different stuff you need to do based on their identifier.

Front Controller implementation in Laravel

I have a front controller which receives all of the requests. Request has an action parameter which tells the controller which command to initiate.
Apparently Laravel commands are not supposed to return responses like views and redirects upon completion. Should my main controller do it? Different actions require different responses.
Perhaps I should implement command objects as tiny controllers with a single method like process()? If for example an action is to load a login screen, process() would simply return view('login'). Is that reasonable?
An explanation would be appreciated.
Look into Job classes.
https://laravel.com/docs/5.2/queues#writing-job-classes
Your controller could dispatch a Job classs like this (like a tiny controller that you mentioned)...
https://laravel.com/docs/5.2/queues#pushing-jobs-onto-the-queue
The handle method on the Job class could return whatever you want to your controller.

yii writing a resuable code for cascade dropDownMenu

I wrote a three cascade dropDownLists that its listData are generated from the database models.
The lists are generated with an Ajax call to action in the controller based.
I want to reuse this code and to share it with more pages.
I tried to do the following:
Write it as a Custom Widget.
currently i use 'createurl' function that calls a function in the matching controller.
I cant write JavaScript since i want to use the existing db models.
In this case i need to write the action functions in an independent file - so should i write a controller? where should i place it?
Write it as a part of a module - but it seems overkill.
any suggestions, i am sure that there is a right and simple way to do it.
You could create it as a helper. A helper is just a class in the components which has no direct action in the M->C->V action flow but can be used in any controller, model, view, component, module, etc...
I would write a helper method to call it from the controller.
Another suggestion could be to extend CController to your own base controller and have your actual controllers, extend from your custom base controller. That way you can make it easily available in every controller, and then you just set some members that contain the models to use which you set in the actual controller.
If you need more help on this, find me on freenode #yii

MVC - Calling Controller Methods

My application is following the MVC design pattern. The problem I keep running into is needing to call methods inside a Controller class from outside that Controller class (ex. A View class wants to call a Controller method, or a Manager class wants to call a Controller method). Is calling Controller methods in this way allowed in MVC? If it's allowed, what's the proper way to do it?
According to the version of MVC that I am following (there seems to be so many different versions out there), the View knows of the Model, and the Controller knows of the View. Doing it this way, I can't access the controller. Here's the best site I've found and the one describing the version of MVC I'm following: http://leepoint.net/notes-java/GUI/structure/40mvc.html. The Main Program code block really shows how this works.
Thanks for any answers.
Take a closer look at this paragraph from the article you linked to:
View
This View doesn't know about the Controller, except that it provides methods for registering a Controller's listeners. Other organizations are possible (eg, the Controller's listeners are non-private variables that can be referenced by the View, the View calls the Controller to get listeners, the View calls methods in the Controller to process actions, ...).
You have the observer pattern here between the View and the Controller. MVC is not a single pattern per se but at least two combined.
One way to get your head around managing the View/Controller communication is to use events. The View fires events on certain user actions (without knowing necessarily who might handle them.) The Controller processes these events and acts accordingly.

Resources