Codeigniter我的表单助手

时间:2013-11-08 12:10:36

标签: 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);

问题:

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

echo form_open('contact');

但我在之前的代码中定义了功能。

我假设,我可以制作自己的表格助手../application/helpers/MY_form_helper.php) 有如何修改以及如何使用我自己的助手? 如果我用“my_”为helper添加前缀,这意味着我覆盖默认的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;
    }

}

1 个答案:

答案 0 :(得分:0)

Helpers中的

CI不是class(es)。它们只是功能。制作自己的帮助器并将其保存在application/helpers中。像往常一样加载助手

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

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

相关问题