php 如何从视图中调用 codeigniter 控制器功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9986520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call codeigniter controller function from view
提问by keerthi
How to call codeigniter controller function from view? When i call the function in a controller, get a 404 page.
如何从视图中调用 codeigniter 控制器功能?当我在控制器中调用该函数时,得到一个 404 页面。
采纳答案by envysea
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!
我希望这有帮助!
回答by saad
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();
回答by Logus
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 方法。
回答by Abhishek
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.
您可以在视图中调用控制器方法,但请注意,这在包括 codeigniter 在内的任何 MVC 中都不是一个好的做法。
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) ;
?>
回答by Mr?àm Tn
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();
回答by rechie
views cannot call controller functions.
视图不能调用控制器函数。
回答by ShivarajRH
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;
回答by Ahmad
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. :)
希望这有帮助。:)
回答by Shailesh Singh
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
回答by Fillz Adebayo
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..
非常简单,只需在控制器类中正确编写函数并使用标记来指定控制器类和方法名称,或任何其他必要的参数..
<?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.phpand 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>