开发者

In Codeigniter: How to identify the model and controller of the views?

开发者 https://www.devze.com 2023-04-07 22:47 出处:网络
Is there a way to 开发者_C百科identify the model name and controller name of the views like print_r($model) or print_r($controller)?

Is there a way to 开发者_C百科identify the model name and controller name of the views like print_r($model) or print_r($controller)?

For example:

view.php

<div class="data">
<?php
echo "this is from".$controller;
echo "this is from".$model;
?>
</div>


You can get the name of the controller with this:

$this->router->fetch_class();

You have to pass controller name to view (as a variable).
But you cannot identify model in the view - there could be more than one model (in one controller). The best way is to create method in your model and assign it to variable passed to the view.


You shouldn't be passing stuff from the model straight to the view. http://codeigniter.com/user_guide/overview/mvc.html and http://codeigniter.com/user_guide/overview/appflow.html

If you want to pass information to the you can do this.

model_name.php

//Class Declaration

    function get_data()
    {
       return 'This is the Model Data';
    }

controller.php

 //Class Declaration
    function index()
    {
        $data['controller'] = 'Data From the Controller';
        $data['model'] = $this->Model_Name->get_data();
        $this->load->view('view.php', $data);
    }

The View remains the same as you have.


A controller can call from multiple models and the view is designated by the controller (or included in a view designated by the controller). You really shouldn't call data from the model directly into the view.

You could get the controller name from the appropriate URI segment if you're not using routes (or otherwise) to change the segment name. I'm not sure it's even possible to have two controllers at a time. Then again, I've never had the need to even try.

I suppose you could manually set variables in the controller under every function and echo them out on the page, but there isn't a built-in, automated way to do what you're looking for.

0

精彩评论

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