H
H
hrvasiliy2015-11-11 10:23:46
PHP
hrvasiliy, 2015-11-11 10:23:46

How do you implement common functions/methods?

Depending on which page the user enters, the desired controller and model are connected. It often happens that in some controller a certain method is already implemented, which, for example, is received by the browser / OS and this method is suddenly required in another controller. Do not copy this method? I try to avoid even the slightest repetition of the code. I came to the conclusion that we need to add a new, common controller (there are, by the way, common methods). Would like to know how to properly implement the common part?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-11-11
@hrvasiliy

Implemented through a generic abstract controller class.

abstract class AbstractController
{
    public function commonMethod() { }
}

class ConcreteControllerA extends AbstractController { }
class ConcreteControllerB extends AbstractController { }

You can spice it all up with traits:
trait ControllerTrait
{
    public function traitMethod() { }
}

/**
 * В этом контроллере будут общие методы абстрактного 
 * класса и методы трейта.
 */
class ConcreteControllerA extends AbstractController 
{ 
    use ControllerTrait;
}

/**
 * А в этом только общие методы абстрактного класса.
 */
class ConcreteControllerB extends AbstractController
{
    
}

More complicated option - controller plugins:
https://github.com/zendframework/zend-mvc/tree/mas...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question