开发者

Please suggestion a better design of this controller class

开发者 https://www.devze.com 2023-03-31 02:56 出处:网络
Here is the code using CodeIgniter: The problem I encounter: The controller will have some functions call view, and it

Here is the code using CodeIgniter:

The problem I encounter:

  1. The controller will have some functions call view, and it separated, but it is still very close with the logic itself, if the controller change to return in JSON or XML to display result, it seems very trouble.

  2. Seems many method, but each one is depends another.

  3. I think it is difficult to track the code.

Please give some suggestions thank you. *Please reminded that, it is only the controller class. the load view is actually prepare the data for the view, won't render the page. also the doXXX function call model is only use the model method, it won't have any SQL statement. The MVC is separated, but the controller also have the functions related to the view or model, make it quite messy.

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 va开发者_运维知识库lid_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
}

}


1. The controller will have some functions call view, and it separated, but it is still very close with the logic itself, if the controller change to return in JSON or XML to display result, it seems very trouble.

Depends on how you organized your code and what you actually pass into the view (template). If that's well structured, you can have one view for HTML, one for XML and one for json, where-as json normally just encodes the view variable's (see json_encodeDocs).

2. Seems many method, but each one is depends another.

Well, just don't do it :) The names look like you wanted to "code that into". Keep it apart. Make those function actually actions that a user performs:

register - that action handles the registration process

Make a login controller out of it that handles anything you need:

login - the login action
lost_password - the lost password action
register - the registration action
activate - the registration activation action

Everything else does not belong in there. There is no need for an action to display some page - the controller itself can decide which view to pick.

Next to that you don't need to display database errors. CI takes care of that. Just put only in what's needed and keep things simple. That should help you to reduce the number of methods and the code therein as well.

3. I think it is difficult to track the code.

Sure. Too many functions with not really speaking names. Keep things simple. It's not easy, but give naming and reducing the overall logic some love.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号