【问题标题】:Codeigniter my form helperCodeigniter 我的表单助手
【发布时间】:2013-11-08 12:10:35
【问题描述】:

我在 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) 如何修改它和如何使用我自己的助手? 如果我在 helper 前面加上“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;
    }

}

【问题讨论】:

    标签: php codeigniter helper


    【解决方案1】:

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

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

    如果您想扩展默认助手,只需将默认助手名称附加MY_ 并保存在application/helpers 中,如您所说。这将add / override 默认函数。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多