Any template for "one class in one action / page"?

MVC is a really good example, but sometimes it’s very boring to put everything into controller methods. The controller is constantly growing, and it takes time to get rid of thousands of lines of code. Some people strongly recommend placing as much as possible in Model, but I prefer to keep the model clean (I did not put controllers in the model).

The idea is to put each Controller action in its own class ...

class Post_Add {}
class Post_Remove {}
class Post_View {}

All code that is common to all action classes that we put in class Post_Parentand pass it to the instance in the action constructor.

So, the calling action will look like ...

$parent = new Post_Parent();
$action = new Post_Add($parent);
$action->run();

So what do we have?

  • , , vars, , .
  • (Post_Parent) . ACL ..

? ?

.

+3
3

Transaction Script PageController. script Domain Logic . PageController - . , , .

class PostAddController implements RequestHandler {
    public function handle($request) {
       $post = filter_input(INPUT_POST, 'post', FILTER_SANITIZE_SPECIAL_CHARS);
       $model = new PostAddTransactionScript;   
       $model->process($post);
       include 'postAddViewScript.php';
    }
}

PostAddTransactionScript $postData -, . - MVC, script .

. , .

, Post_Parent ,

$commander = new PostCommander;
$commander->setStrategy(new PostAddCommand);
$commander->handle($_POST);

, , , .

0

, , , . " ", , .

. , . . - , -, , , - -.

+1

I suggest making friends a bit and thinking about why controllers grow so much. Maybe you can do refactoring and extract some common components into separate modules? Maybe you have some code in your logic that should be in your model or view? Do you use a good template system?

Dividing the controllers into smaller pieces does not solve the main problem, but only sweeps it under the carpet.

0
source

Source: https://habr.com/ru/post/1765785/


All Articles