下面是使用 CodeIgniter 的代码:
我遇到的问题:
控制器会有一些函数调用view,虽然是分开的,但是和逻辑本身还是很接近的,如果控制器改成JSON或者XML返回显示结果,就显得很麻烦了。
似乎有很多方法,但每个方法都取决于另一个。
我认为很难跟踪代码。
请给些建议谢谢。*请注意,它只是控制器类。加载视图实际上是为视图准备数据,不会渲染页面。doXXX函数调用模型也只是使用模型方法,它不会有任何SQL语句。MVC 是分离的,但是控制器也有与视图或模型相关的功能,比较杂乱。
class User extends CI_Controller
{
public function register()
{
//check is logged in or not
//if not logged in , show the register page
}
public function show_register_page()
{
//generate the UI needed data , and call the view to render, and will the user will post back a valid_register function
}
public function valid_register()
{
//do all the valid logic, if success,
//do the do_register
//if fail, valid_register_fail
}
public function valid_register_fail()
{
//check is logged in or not
//show the valid register fail page
}
public function show_valid_register_fail_page()
{
//generate the UI needed data , and call the view to render
}
public function do_register()
{
//insert data in the db, the Model will be called
//if something go wrong in db, show the error page
//if everything is success, show the register success
}
public function show_db_error_page()
{
//generate the UI needed data , and call the view to render
}
public function show_register_success()
{
//generate the UI needed data , and call the view to render
}
}