开发者

When using codeigniter, can I load content to a view from different controllers?

开发者 https://www.devze.com 2023-02-09 00:33 出处:网络
I have 2 controller files: login.php and tables.php. login.php loads the view I want: function locales(){

I have 2 controller files: login.php and tables.php.

login.php loads the view I want:

function locales(){
        $data['main_content'] = 'negocios';
        $this->load->view('includes/template', $data);
    }

however inside that view I am using:

<?php echo $this->table->generate($records); ?>
            <?php echo $this->pagination->create_links(); ?>

which makes reference to the tables.php controller:

class Tables extends Controller{

    function createTables(){
        $this->load->library开发者_如何学Go('pagination');

        $config['base_url'] = 'http://localhost/ci/index.php/tables/createTables';
        $config['total_rows'] = $this->db->get('usuarios')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;

        $this->pagination->initialize($config);

        $data['records'] = $this->db->get('usuarios', $config['per_page'], $this->uri->segment(3));
    }
} 

Can I accomplish what I'm trying to do? if so, how? Also, I know it's not recommended to access the DB directly in the controller but I'm doing this just for practice sake.


Are you trying to access to database directly from controller? In my opinion, this is not a good practice. The MVC patterns is designed to separate controll from views and data.

Quoting wikipedia:

The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react.

The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.

The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

I'll try to create a model called 'Tables' and inside of it put this:

class Tables extends Model{

    function createTables(){
        $this->load->library('pagination');

        $config['base_url'] = 'http://localhost/ci/index.php/tables/createTables';
        $config['total_rows'] = $this->db->get('usuarios')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;

        $this->pagination->initialize($config);

        return $this->db->get('usuarios', $config['per_page'], $this->uri->segment(3));
    }
} 

Then, your Login Controller could be:

function locales(){
   $data['main_content'] = 'negocios';
   $this->load->model('tables');
   $data['records'] = $this->tables->createTables();
   $this->load->view('includes/template', $data);
}

Now, you can modify the view:

// all your code...
<?php 
echo $records; 
echo $this->pagination->create_links();
?>
// more code...

If you don't need to call $this->tables->createTables() everytime, you should modify the controller:

function locales(){
   $data['main_content'] = 'negocios';
   $this->load->model('tables');
   if(yourcondition)
      $data['records'] = $this->tables->createTables();
   $this->load->view('includes/template', $data);
}

And your view:

// all your code...
<?php 
if(isset($records))
   echo $records; 
echo $this->pagination->create_links();
?>
// more code...


I'm not sure you're connecting to dots correctly in your question. You cannot directly reference all controllers within views.

To more fully understand what you are typing, the $this keyword you are using in both your controller and view files makes reference to (for all intensive purposes*) the controller you are using, so it would not make sense to call $this->{controller}->function() because you would be calling the controller of the controller.

I believe you are attempting to implement an HMVC structure. There has been some work done to implement this in CodeIgniter through Modular Extensions

But generally Vanilla codeigniter is 1 Controller at a time, but that controller can load many views and/or models.


*I know it is the CI instance, but the CI instance for all intensive purposes IS the controller

0

精彩评论

暂无评论...
验证码 换一张
取 消