Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

get data from other controller

hi, i have one question

to my controller ex: IndexController i have query:

        $categorycount = $this->modelsManager->executeQuery("SELECT count(id) AS velueviews FROM Videos    WHERE category = :category:", 

        array('category' => '1'));

        $this->view->setVars(array('categorycount' => $categorycount));

i want to get this data from other controller if it possible, ex: ProfileController, i don't need to write similar query to ProfileController. What can I do?


if the ohter controller extends this one you can use

$this->videosQuery = $this->modelsManager....

but i think the best way is to have Model and create a public static function there so you can call it from where ever you need it


Controllers shouldn't need to talk to each other. Create a model/class that you can access through the controller, if needed. keep controllers as lean as possible. My basic controller function structure is:

  1. Take user input
  2. Get data from proper model
  3. Return data to user (or return view to user, if applicable)

All of the data retrieval/formatting/etc is done in the model.

edited Jan '16

I have some questions for you:

  1. You have a function to get data from DB in Profile controller and now, you wanna call it in other controller that no need redefine it?
  2. You have a function return DATA and reuse it (DATA) in other controller? If 1: You can define this function in the BaseController that other extends it. If 2: You can use caching: redis or memcache.


Don't get the data in a controller. Get the data in a model. It has nothing to do with extending a base controller or caching.

Controller 1:

    public function getModelData() {
        $model = new MyModel();
        $data = $model->getData();
        return $data;

Controller 2:

    public function getModelData() {
        $model = new MyModel();
        $data = $model->getData();
        return $data;


    Class MyModel extends Model {
        public function getData() {
            //Call your database to get the data here.

thanks to all

You are Save my time And Teach, How to write the code correctly