Php Codeigniter我的表单助手
我在CodeIgniter中使用form_helper:Php Codeigniter我的表单助手,php,codeigniter,helper,Php,Codeigniter,Helper,我在CodeIgniter中使用form_helper: $current = $this->lang->mci_current(); $uri = 'contact'; $url = $this->lang->mci_make_uri($current, $uri); // output "en/contact" echo form_open($url); 问题: if (!function_exists('form_open')) { funct
$current = $this->lang->mci_current();
$uri = 'contact';
$url = $this->lang->mci_make_uri($current, $uri); // output "en/contact"
echo form_open($url);
问题:
if (!function_exists('form_open')) {
function form_open($action = '', $attributes = '', $hidden = array()) {
$CI = & get_instance();
$current = $CI->lang->mci_current();
$action = $CI->lang->mci_make_uri($current, $action);
if ($attributes == '') {
$attributes = 'method="post"';
}
// If an action is not a full URL then turn it into one
if ($action && strpos($action, '://') === FALSE) {
$action = $CI->config->site_url($action);
}
// If no action is provided then set to the current url
$action OR $action = $CI->config->site_url($CI->uri->uri_string());
$form = '<form action="' . $action . '"';
$form .= _attributes_to_string($attributes, TRUE);
$form .= '>';
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
if ($CI->config->item('csrf_protection') === TRUE AND !(strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"'))) {
$hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
}
if (is_array($hidden) AND count($hidden) > 0) {
$form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
}
return $form;
}
}
如何修改form_helper以将其更改为默认值:
echo form_open('contact');
但是使用我在前面的代码中定义的功能
我假设,我可以制作自己的表单助手
(。/application/helpers/MY_form_helper.php
)
有关于如何修改它和如何使用我自己的助手的信息吗?
如果我在helper前面加上“my_u”,这意味着我覆盖了默认的表单\u helper?我是否需要扩展默认的form\u helper
这就是我设法做到的:
注意:我是MVC和OOP新手,正在学习CodeIgniter
我的尝试:
if (!function_exists('form_open')) {
function form_open($action = '', $attributes = '', $hidden = array()) {
$CI = & get_instance();
$current = $CI->lang->mci_current();
$action = $CI->lang->mci_make_uri($current, $action);
if ($attributes == '') {
$attributes = 'method="post"';
}
// If an action is not a full URL then turn it into one
if ($action && strpos($action, '://') === FALSE) {
$action = $CI->config->site_url($action);
}
// If no action is provided then set to the current url
$action OR $action = $CI->config->site_url($CI->uri->uri_string());
$form = '<form action="' . $action . '"';
$form .= _attributes_to_string($attributes, TRUE);
$form .= '>';
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
if ($CI->config->item('csrf_protection') === TRUE AND !(strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"'))) {
$hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
}
if (is_array($hidden) AND count($hidden) > 0) {
$form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
}
return $form;
}
}
如果(!function_存在('form_open')){
函数form_open($action='',$attributes='',$hidden=array()){
$CI=&get_instance();
$current=$CI->lang->mci_current();
$action=$CI->lang->mci\u make\u uri($current,$action);
如果($attributes==''){
$attributes='method=“post”';
}
//如果某个操作不是完整的URL,则将其转换为完整的URL
if($action&&strpos($action,,://')==FALSE){
$action=$CI->config->site\u url($action);
}
//如果未提供任何操作,则设置为当前url
$action或$action=$CI->config->site\u url($CI->uri->uri\u string());
$form='';
//如果启用,则添加CSRF字段,但将其保留在GET请求和对外部网站的请求中
if($CI->config->item('csrf_protection')==TRUE和!(strpos($action,$CI->config->base_url())==FALSE或strpos($form,'method=“get”')){
$hidden[$CI->security->get_csrf_token_name()]=$CI->security->get_csrf_hash();
}
如果(是_数组($hidden)和计数($hidden)>0){
$form.=sprintf(“%s”,form_hidden($hidden));
}
返回$表格;
}
}
在CI
中的帮助程序
不是类
。它们只是函数。制作自己的助手并将其保存在应用程序/helpers
中。像往常一样加载助手
$this->load->helper('helperName'); #your file name should be helperName_helper.php
如果您想扩展默认帮助程序,只需使用默认帮助程序名称附加MY_quot
,并如您所说保存在应用程序/helpers
中。这将add/override
默认函数。因此我假设我的助手应该被命名为my\u form\u helper.php
位于application/helpers/
中。当我使用my
而不是my
时,是否存在差异?是的,根据文档,您应该使用my
。