CodeIgniter最好的方法?

发布于 2024-10-03 14:17:01 字数 252 浏览 4 评论 0原文

我是代码点火器的新手。

我希望重写我们当前的系统以使用代码点火器。

我目前的问题是。

我正在传输登录表单。 目前,我们有一个页面接收 ajax 请求并验证数据并在提交表单时返回状态。

我应该如何在 Code Igniter 中解决这个问题? 我的想法是创建一个接收请求的控制器。

但是,

控制器不应该发出任何响应。

那么这是否意味着我应该创建一个视图只是为了吐出几行 json?

I am new to Code Igniter.

I am looking to re-write our current system to use code igniter.

My current question is.

I am transferring the login form.
Currently we have a page with receives an ajax request and validates the data and returns the status when the form is submitted.

How should I go about this in Code Igniter?
My thoughts are to create a controller which receives the request.

But,

A controller should not spit out any response.

So does that mean I should create a view just to spit out a couple of lines of json?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲在坟头点根烟 2024-10-10 14:17:01

当然。您甚至可以将其设置为通用视图,可以输出您想要的任何 JSON,然后将对象传递为 json_encoded 到视图。这样您就可以为其他处理 AJAX 请求的控制器重用相同的视图。

Sure. You could even make it a generic view that can spit out any JSON you want, and then pass in the object to be json_encoded to the view. That way you could reuse the same view for other controllers that handled AJAX requests as well.

方圜几里 2024-10-10 14:17:01

Code Igniter 有一个输入类和一个表单验证类。它们都有有助于处理表单输入的方法:

我会创建一个带有登录方法的帐户控制器。登录方法将使用前面提到的类进行表单处理,然后调用您的模型来执行数据库查询。

我认为拥有 ajax 控制器对您来说没有多大价值。您可能最终会在所有控制器中使用 ajax。如果您想考虑针对 ajax 优化控制器,我会将所有“移动部分”分解为松散耦合的小任务。由于 ajax 请求不会重新加载整个页面,因此可以更轻松地仅运行所需的逻辑。

在前端,使用基于模板的布局很有帮助。这样,您发送到输出的唯一内容就是正在更新的部分。 Code Igniter 解析器类也有助于模板化。

Code Igniter has an input class and a form validation class. They both have methods that are helpful for handling form input:

I would create an account controller with a login method. The login method would use the previously mentioned classes for form handling and then call your model to execute the DB query.

I don't think having an ajax controller would be of much value to you. You'll likely end up using ajax in all of your controllers. If you want to think about optimizing your controllers for ajax, I would break up all of the "moving parts" into small loosely coupled tasks. Since ajax requests don't reload the whole page it makes it easier to run only the required logic.

On the front end, it's helpful to use template-based layouts. This way the only thing you send to the output is portion being updated. The Code Igniter parser class helps with templating too.

玩心态 2024-10-10 14:17:01

以下是您如何进行登录的示例。这是来自我自己的网站之一。如果您看到我缺少的最佳实践,也希望得到其他人的反馈

VIEW

if (isset($message)) {echo $message;} //error message

echo form_open('login/validate_credentials', 'class="form-container"');
echo form_input('username', 'Username',  'class="form-field"');
echo form_password('password', 'Password',  'class="form-field"');
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Need an account? Enroll now!');
echo form_close();

CONTROLLER

function validate_credentials()
    {       
    $this->load->model('usermodel');
    $query = $this->usermodel->validateUser();

    if($query) // if the user's credentials validated...
    {
        $data = array(
            'username' => $this->input->post('username'),
            'is_logged_in' => true,
            'bt_link' => $this->usermodel->getBTlink($this->input->post('username')), 
        );

        $this->session->set_userdata($data); //store in session

        $this->load->view('membership'); //logged in
    }
    else // incorrect username or password
    {
        $data['message'] = "Invalid credentials";
        $data['header_type'] = 'header';
        $data['main_content'] = 'login_form';
        $this->load->view('template', $data);
    }
}   

这是一个很棒的 CI 教程

http://net.tutsplus.com/sessions/codeigniter-from-scratch

Here is an example of how you might be able to do the login. This is from one of my own sites. Would also love feedback from others if you see best practices that I am missing

VIEW

if (isset($message)) {echo $message;} //error message

echo form_open('login/validate_credentials', 'class="form-container"');
echo form_input('username', 'Username',  'class="form-field"');
echo form_password('password', 'Password',  'class="form-field"');
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Need an account? Enroll now!');
echo form_close();

CONTROLLER

function validate_credentials()
    {       
    $this->load->model('usermodel');
    $query = $this->usermodel->validateUser();

    if($query) // if the user's credentials validated...
    {
        $data = array(
            'username' => $this->input->post('username'),
            'is_logged_in' => true,
            'bt_link' => $this->usermodel->getBTlink($this->input->post('username')), 
        );

        $this->session->set_userdata($data); //store in session

        $this->load->view('membership'); //logged in
    }
    else // incorrect username or password
    {
        $data['message'] = "Invalid credentials";
        $data['header_type'] = 'header';
        $data['main_content'] = 'login_form';
        $this->load->view('template', $data);
    }
}   

Here is a great CI tutorial

http://net.tutsplus.com/sessions/codeigniter-from-scratch

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文