Ard*_*ram 14 coding-style codeigniter
我已经使用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);
}
}
class User extends Model
{
.
.
.
public function login($username, password)
{
....
}
}
Run Code Online (Sandbox Code Playgroud)
另一个是直接从Model访问输入参数,如下所示:
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');
}
}
Run Code Online (Sandbox Code Playgroud)
您认为更好的做法是什么?我倾向于第一个,因为它给了模型更多的独立性,但我在示例代码和其他开发人员中看到了第二种方法.不可否认,第二种方法更短,特别是当您接受5-10个输入字段时.
Rep*_*pox 12
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!'
| 归档时间: |
|
| 查看次数: |
11813 次 |
| 最近记录: |