如何从视图调用codeigniter控制器函数

时间:2023-01-24 11:24:05

How to call codeigniter controller function from view? When i call the function in a controller, get a 404 page.

如何从视图调用codeigniter控制器函数?当我调用控制器中的函数时,获得404页面。

11 个解决方案

#1


16  

Codeigniter is an MVC (Model - View - Controller) framework. It's really not a good idea to call a function from the view. The view should be used just for presentation, and all your logic should be happening before you get to the view in the controllers and models.

Codeigniter是一个MVC(模型-视图-控制器)框架。从视图中调用函数并不是一个好主意。视图应该仅用于表示,在您到达控制器和模型中的视图之前,您的所有逻辑都应该发生。

A good start for clarifying the best practice is to follow this tutorial:

明确最佳实践的一个好的开始是跟随本教程:

https://codeigniter.com/user_guide/tutorial/index.html

https://codeigniter.com/user_guide/tutorial/index.html

It's simple, but it really lays out an excellent how-to.

它很简单,但是它确实提供了一个很好的操作方法。

I hope this helps!

我希望这可以帮助!

#2


28  

You can call controller function from view in the following way:

您可以通过以下方式调用控制器函数:

Controller:

控制器:

public function read()
{

            $object['controller']=$this; 
            $this->load->view('read',$object);
}

View:

观点:

//to call controller function from view do

$controller->myOtherFunct();

#3


6  

You can call a controller function with AJAX on your view. In this case, I'm using the jQuery library to make the call.

可以在视图上调用带有AJAX的控制器函数。在本例中,我使用jQuery库进行调用。

<script type="text/javascript">
    $.ajax({
            url: "<?=site_url("controller/function")?>",
            type: "post", // To protect sensitive data
            data: {
               ajax:true,
               variableX: "string",
               variableY: 25
               //and any other variables you want to pass via POST
                   },
            success:function(response){
            // Handle the response object
            }
        });
</script>

This way you can create portions of code (modules) and reload them the AJAX method in a HTML container.

通过这种方式,您可以创建部分代码(模块),并在HTML容器中重新加载AJAX方法。

#4


4  

I would like to answer this question as this comes all times up in searches --

我想回答这个问题,因为这在搜索中经常出现

You can call a controller method in view, but please note that this is not a good practice in any MVC including codeigniter.

您可以在view中调用controller方法,但是请注意,这在任何MVC(包括codeigniter)中都不是一个好的实践。

Your controller may be like below class --

您的控制器可能类似于下面的类——。

<?php
    class VCI_Controller extends CI_Controller {
    ....
    ....
    function abc($id){
       return $id ;
    }

    }
?>

Now You can call this function in view files as below --
<?php
    $CI =& get_instance();
    $CI->abc($id) ;

?>

#5


3  

views cannot call controller functions.

视图不能调用控制器函数。

#6


2  

One idea i can give is,

我能给出的一个想法是,

Call that function in controller itself and return value to view file. Like,

在控制器中调用该函数并将值返回到视图文件。就像,

class Business extends CI_Controller {
    public function index() {
            $data['css'] = 'profile';

            $data['cur_url'] = $this->getCurrURL(); // the function called and store val
            $this->load->view("home_view",$data);
     }
     function getCurrURL() {
                $currURL='http://'.$_SERVER['HTTP_HOST'].'/'.ltrim($_SERVER['REQUEST_URI'],'/').'';
            return $currURL;
     }

}

in view(home_view.php) use that variable. Like,

在view(home_view.php)中使用该变量。就像,

echo $cur_url;

#7


2  

class MY_Controller extends CI_Controller {

    public $CI = NULL;

    public function __construct() {
        parent::__construct();
        $this->CI = & get_instance();
    }

    public function yourMethod() {

    }

}

// in view just call
$this->CI->yourMethod();

#8


1  

I know this is bad.. But I have been in hard situation where it is impossible to put this back to controller or model.

我知道这很糟糕。但我一直处于困难的境地,不可能把它放回控制器或模型。

My solution is to call a function on model. It can be do inside a view. But you have to make sure the model has been loaded to your controller first.

我的解决方案是在模型上调用一个函数。它可以在视图中执行。但是你必须先确保模型已经加载到你的控制器中。

Say your model main_model, you can call function on the model like this on your view : $this->main_model->your_function();

假设你的模型main_model,你可以在视图中调用模型上的函数:$this->main_model->your_function();

Hope this help. :)

希望这个有帮助。:)

#9


1  

We can also pass controller function as variable in the view page.

我们还可以在视图页面中将控制器函数作为变量传递。

class My_controller extends CI_Controller {
    public function index() {
           $data['val']=3;
           $data['square']=function($val){
                               return $val*$val;
                                   };
    $this->load->view('my-view',$data);
}
}

In the view page

在视图页面

<p>Square of <?=$val?>
   <?php 
     echo $square($val); 
   ?>
</p>

The output is 9

输出是9

#10


1  

it is quite simple just have the function correctly written in the controller class and use a tag to specify the controller class and method name, or any other neccessary parameter..

只要在controller类中正确地写入函数并使用标记来指定控制器类和方法名,或者任何其他必要的参数,就很简单。

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Iris extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->model('script');
        $this->load->model('alert');

    }public function pledge_ph(){
        $this->script->phpledge();
    }
}
?>

This is the controller class Iris.php and the model class with the function pointed to from the controller class.

这是控制器类Iris。php和模型类的函数指向来自控制器类。

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Script extends CI_Model {
    public function __construct() {
        parent::__construct();
        // Your own constructor code
    }public function ghpledge(){
        $gh_id = uniqid(rand(1,11));
        $date=date("y-m-d");
        $gh_member = $_SESSION['member_id'];
        $amount= 10000;
        $data = array(
            'gh_id'=> $gh_id,
            'gh_member'=> $gh_member,
            'amount'=> $amount,
            'date'=> $date
        );
        $this->db->insert('iris_gh',$data);
    } 
}
?>

On the view instead of a button just use the anchor link with the controller name and method name.

在视图中,不要使用按钮,只需使用带有控制器名和方法名的锚点链接。

<html>
    <head></head>
    <body>
        <a href="<?php echo base_url(); ?>index.php/iris/pledge_ph" class="btn btn-success">PLEDGE PH</a>
    </body>
</html>

#11


0  

if you need to call a controller from a view, maybe to load a partial view, you thinking as modular programming, and you should implement HMVC structure in lieu of plane MVC. CodeIgniter didnt implement HMVC natively, but you can use this useful library in order to implement HMVC. https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

如果需要从视图中调用控制器,可能需要加载部分视图,您可以将其视为模块化编程,并且应该实现HMVC结构来代替平面MVC。CodeIgniter并没有直接实现HMVC,但是您可以使用这个有用的库来实现HMVC。https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

after setup remember:that all your controllers should extends from MX_Controller in order to using this feature.

设置后请记住:所有的控制器都应该从MX_Controller扩展到使用这个特性。

#1


16  

Codeigniter is an MVC (Model - View - Controller) framework. It's really not a good idea to call a function from the view. The view should be used just for presentation, and all your logic should be happening before you get to the view in the controllers and models.

Codeigniter是一个MVC(模型-视图-控制器)框架。从视图中调用函数并不是一个好主意。视图应该仅用于表示,在您到达控制器和模型中的视图之前,您的所有逻辑都应该发生。

A good start for clarifying the best practice is to follow this tutorial:

明确最佳实践的一个好的开始是跟随本教程:

https://codeigniter.com/user_guide/tutorial/index.html

https://codeigniter.com/user_guide/tutorial/index.html

It's simple, but it really lays out an excellent how-to.

它很简单,但是它确实提供了一个很好的操作方法。

I hope this helps!

我希望这可以帮助!

#2


28  

You can call controller function from view in the following way:

您可以通过以下方式调用控制器函数:

Controller:

控制器:

public function read()
{

            $object['controller']=$this; 
            $this->load->view('read',$object);
}

View:

观点:

//to call controller function from view do

$controller->myOtherFunct();

#3


6  

You can call a controller function with AJAX on your view. In this case, I'm using the jQuery library to make the call.

可以在视图上调用带有AJAX的控制器函数。在本例中,我使用jQuery库进行调用。

<script type="text/javascript">
    $.ajax({
            url: "<?=site_url("controller/function")?>",
            type: "post", // To protect sensitive data
            data: {
               ajax:true,
               variableX: "string",
               variableY: 25
               //and any other variables you want to pass via POST
                   },
            success:function(response){
            // Handle the response object
            }
        });
</script>

This way you can create portions of code (modules) and reload them the AJAX method in a HTML container.

通过这种方式,您可以创建部分代码(模块),并在HTML容器中重新加载AJAX方法。

#4


4  

I would like to answer this question as this comes all times up in searches --

我想回答这个问题,因为这在搜索中经常出现

You can call a controller method in view, but please note that this is not a good practice in any MVC including codeigniter.

您可以在view中调用controller方法,但是请注意,这在任何MVC(包括codeigniter)中都不是一个好的实践。

Your controller may be like below class --

您的控制器可能类似于下面的类——。

<?php
    class VCI_Controller extends CI_Controller {
    ....
    ....
    function abc($id){
       return $id ;
    }

    }
?>

Now You can call this function in view files as below --
<?php
    $CI =& get_instance();
    $CI->abc($id) ;

?>

#5


3  

views cannot call controller functions.

视图不能调用控制器函数。

#6


2  

One idea i can give is,

我能给出的一个想法是,

Call that function in controller itself and return value to view file. Like,

在控制器中调用该函数并将值返回到视图文件。就像,

class Business extends CI_Controller {
    public function index() {
            $data['css'] = 'profile';

            $data['cur_url'] = $this->getCurrURL(); // the function called and store val
            $this->load->view("home_view",$data);
     }
     function getCurrURL() {
                $currURL='http://'.$_SERVER['HTTP_HOST'].'/'.ltrim($_SERVER['REQUEST_URI'],'/').'';
            return $currURL;
     }

}

in view(home_view.php) use that variable. Like,

在view(home_view.php)中使用该变量。就像,

echo $cur_url;

#7


2  

class MY_Controller extends CI_Controller {

    public $CI = NULL;

    public function __construct() {
        parent::__construct();
        $this->CI = & get_instance();
    }

    public function yourMethod() {

    }

}

// in view just call
$this->CI->yourMethod();

#8


1  

I know this is bad.. But I have been in hard situation where it is impossible to put this back to controller or model.

我知道这很糟糕。但我一直处于困难的境地,不可能把它放回控制器或模型。

My solution is to call a function on model. It can be do inside a view. But you have to make sure the model has been loaded to your controller first.

我的解决方案是在模型上调用一个函数。它可以在视图中执行。但是你必须先确保模型已经加载到你的控制器中。

Say your model main_model, you can call function on the model like this on your view : $this->main_model->your_function();

假设你的模型main_model,你可以在视图中调用模型上的函数:$this->main_model->your_function();

Hope this help. :)

希望这个有帮助。:)

#9


1  

We can also pass controller function as variable in the view page.

我们还可以在视图页面中将控制器函数作为变量传递。

class My_controller extends CI_Controller {
    public function index() {
           $data['val']=3;
           $data['square']=function($val){
                               return $val*$val;
                                   };
    $this->load->view('my-view',$data);
}
}

In the view page

在视图页面

<p>Square of <?=$val?>
   <?php 
     echo $square($val); 
   ?>
</p>

The output is 9

输出是9

#10


1  

it is quite simple just have the function correctly written in the controller class and use a tag to specify the controller class and method name, or any other neccessary parameter..

只要在controller类中正确地写入函数并使用标记来指定控制器类和方法名,或者任何其他必要的参数,就很简单。

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Iris extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->model('script');
        $this->load->model('alert');

    }public function pledge_ph(){
        $this->script->phpledge();
    }
}
?>

This is the controller class Iris.php and the model class with the function pointed to from the controller class.

这是控制器类Iris。php和模型类的函数指向来自控制器类。

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Script extends CI_Model {
    public function __construct() {
        parent::__construct();
        // Your own constructor code
    }public function ghpledge(){
        $gh_id = uniqid(rand(1,11));
        $date=date("y-m-d");
        $gh_member = $_SESSION['member_id'];
        $amount= 10000;
        $data = array(
            'gh_id'=> $gh_id,
            'gh_member'=> $gh_member,
            'amount'=> $amount,
            'date'=> $date
        );
        $this->db->insert('iris_gh',$data);
    } 
}
?>

On the view instead of a button just use the anchor link with the controller name and method name.

在视图中,不要使用按钮,只需使用带有控制器名和方法名的锚点链接。

<html>
    <head></head>
    <body>
        <a href="<?php echo base_url(); ?>index.php/iris/pledge_ph" class="btn btn-success">PLEDGE PH</a>
    </body>
</html>

#11


0  

if you need to call a controller from a view, maybe to load a partial view, you thinking as modular programming, and you should implement HMVC structure in lieu of plane MVC. CodeIgniter didnt implement HMVC natively, but you can use this useful library in order to implement HMVC. https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

如果需要从视图中调用控制器,可能需要加载部分视图,您可以将其视为模块化编程,并且应该实现HMVC结构来代替平面MVC。CodeIgniter并没有直接实现HMVC,但是您可以使用这个有用的库来实现HMVC。https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

after setup remember:that all your controllers should extends from MX_Controller in order to using this feature.

设置后请记住:所有的控制器都应该从MX_Controller扩展到使用这个特性。