Codeigniter最佳实践访问$ this-> input-> post() [英] Codeigniter best practice in accessing $this->input->post()

查看:177
本文介绍了Codeigniter最佳实践访问$ this-> input-> post()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用CodeIgniter编码了一段时间,当我编程或读取其他程序员的代码,我通常遇到两种方式从输入获取数据。一个是从控制器获取输入,然后作为参数传递给模型,像这样:

  class MyController extends Controller 
{



public function login()
{
$ username = $ this-> input-> post('username');
$ password = $ this-> input-> post('password');
$ this-> load-> model('User');
$ this-> User-> login($ username,$ password);
}
}

类用户扩展模型
{



public function login($ username,password)
{
....
}
}
 <$> c $ c> class MyController extends Controller 
{



public function login()
{
$ this-> load-> model('User');
$ this-> User-> login();
}
}

类用户扩展模型
{



public function login()
{
$ username = $ this-> input-> post('username');
$ password = $ this-> input-> post('password');
}
}

你认为更好的做法是什么?我倾向于第一个,因为它使模型更独立,但我看到示例代码中的第二种方法,在其他开发人员。

解决方案

通过使用第一个示例,您允许其他开发人员更改视图和修改控制器,但仍然保持模型按原样。这必须是良好重构的一个例子。



通过选择第二个示例,您在视图中锁定开发,因为输入名称被模型锁定,



换句话说...

第一个样本:灵活

秒sample:'d * mnit,not again!'


I've been coding with CodeIgniter for a while now, and when I'm programming or reading up other programmer's code, I usually encounter two ways of obtaining data from input. One would be getting the input from the controller, then passing to the model as a parameter, like so:

class MyController extends Controller
{
    .
    .
    .
    public function login()
    {
       $username = $this->input->post('username');
       $password = $this->input->post('password');
       $this->load->model('User');
       $this->User->login($username, $password);
    }
}

class User extends Model
{
    .
    .
    .
    public function login($username, password)
    {
      ....
    }
}

Another would be accessing directly from the Model the input parameters, like so:

class MyController extends Controller
{
    .
    .
    .
    public function login()
    {
       $this->load->model('User');
       $this->User->login();
    }
}

class User extends Model
{
    .
    .
    .
    public function login()
    {
       $username = $this->input->post('username');
       $password = $this->input->post('password');
    }
}

What do you think is the better practice? I lean towards the first, as it gives the Model more independence, but I see the second method in sample codes and in other developers. Admittedly, the second method is shorter, especially when you are accepting 5-10 input fields.

解决方案

By using the first sample, you allow other developers to change views and modify controllers, but still maintaining the Model 'as is'. That must be an example of good refactoring.

By choosing the second sample, you lock your development in the views, since the input names are locked by the model, and you would preferably change the model as little as possible.

In other words...
first sample: flexible
second sample: 'd*mnit, not again!'

这篇关于Codeigniter最佳实践访问$ this-&gt; input-&gt; post()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆