代码点火器我的表单助手


Codeigniter my form 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);

问题:

如何修改form_helper以将其更改为默认值:

echo form_open('contact');

但具有我在前面的代码中定义的功能。

我假设,我可以制作自己的表单助手../application/helpers/MY_form_helper.php )那里知道如何修改它以及如何使用我自己的助手?

如果我在帮助程序前面加上"my_",这意味着我覆盖了默认form_helper?我需要延长默认form_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;
    }
}

CI中的Helpers不是class(es)。它们只是函数。制作自己的助手并将它们保存在application/helpers中。像往常一样加载帮助程序

$this->load->helper('helperName'); #your file name should be helperName_helper.php

如果要扩展默认帮助程序,只需在MY_附加默认帮助程序名称并按照您所说的保存在application/helpers中即可。这将add / override默认函数。

相关文章: