简体   繁体   English

Codeigniter:助手功能未加载

[英]Codeigniter:Helper functions are not loaded

I have created a form and without entering the data in fields when i submit it,it redirect on blank page instead of the page which open with form_open() after loading the form helper in my controller. 我已经创建了一个表单,并且在提交表单时没有在字段中输入数据,而是在将表单助手加载到控制器中后,它重定向到空白页面,而不是使用form_open()打开的页面。 In fact i am unable to use the functionality of the url helper.I have post this issue in another way but didn't get any solution. 实际上我无法使用url helper的功能。我已经以另一种方式发布了此问题,但是没有任何解决方案。 i have tried the below code from codeigniter documentation but it's not working for me. 我已经尝试了来自codeigniter文档的以下代码,但对我而言不起作用。

Why this is happening . 为什么会这样。 Please help me to solve it. 请帮我解决。

Controller: 控制器:

class Login extends CI_Controller{
    function index()
    {
    $this->load->helper(array('form', 'url'));

    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');
    $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('login_form');
    }
    else
    {
        $this->load->view('success');
    }
}
}   

login_form view : login_form视图:

<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('login'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" value="Submit" /></div>

</form>

</body>
</html>

success view: 成功视图:

<html>
<head>
<title>My Form</title>
</head>
<body>

<h3>Your form was successfully submitted!</h3>

<p><?php echo anchor('form', 'Try it again!'); ?></p>

</body>
</html>

Thanks in advance... 提前致谢...

$this->load->helper('form', 'url'); $ this-> load-> helper('form','url'); instead of $this->load->helper(array('form', 'url')); 而不是 $this->load->helper(array('form', 'url')); should work ok. 应该可以。

Edit: Not sure what the problem is, but did you try loading the helpers inside /application/config/autoload.php ? 编辑:不确定是什么问题,但是您是否尝试在/application/config/autoload.php加载帮助程序?

$autoload['helper'] = array('form', 'url');

Your code seems correct did you configure your .htaccess file. 如果您配置了.htaccess文件,则您的代码似乎正确。 Because i tried your code it all working fine.When i submit the blank form its give validation error. 因为我尝试了您的代码,所以一切正常。当我提交空白表格时,给出了验证错误。

code for .htaccess file .htaccess文件的代码

    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

do you add parent::__construct() in your __construct() ? __construct()添加parent::__construct() __construct()吗?

    class Login extends CI_Controller{
        public function __construct()
        {   
            parent::__construct();
            $this->load->helper(array('form', 'url'));
        }  
        ....

In your login controller : 在您的login控制器中:

<?php
class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function index() {
        $data = array();
        $data['title'] = 'Login Form';
        $this->load->view('login_form', $data);
    }

    public function save() {     
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');

        if ($this->form_validation->run() != FALSE)
        {
            redirect('login/success');
        }
        else
        {
            $this->index();
        }
    }

    public function success() {
        $data = array();
        $data['title'] = 'Successfully Submitted';
        $this->load->view('success_view', $data);
    }
}
?>

Change form action in your login view page 在您的登录视图页面中更改表单操作

<?php echo form_open('login/save'); ?>

or 要么

<form method="post" action="<?php echo base_url(); ?>login/save">

Note : You can run your login form in your browser like http://localhost/project_folder/login/ 注意:您可以在浏览器中运行登录表单,例如http://localhost/project_folder/login/

Could try creating a construct class 可以尝试创建一个构造类

Make sure file name is Login.php first letter only is all ways upper case codeigniter ucfirst sometimes can cause issues if not done like here 确保文件名是login.php中第一个字母不仅是所有方式大写笨ucfirst有时会引起问题,如果不喜欢做这里

class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
    }

    function index() {

        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('login_form');
        }
        else
        {
            $this->load->view('success');
        }
    }
}   

Try add this 尝试添加这个

parent::__construct();

to your constructor, so 给你的构造函数

function __construct(){
    parent::__construct();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM