S
S
Stanislav Shendakov2020-05-12 04:35:36
symfony
Stanislav Shendakov, 2020-05-12 04:35:36

How to automatically start the service?

Good afternoon.

When you enter the site, or when you go to another page, you need to automatically perform some actions. For example, I now have a rc/Service/MenuPuller class like this:

class MenuPuller extends AbstractController
{
    const TOP_MENU = 1;
    const DOWN_MENU = 2;
    const DOWN_SIDEBAR_MENU = 3;
...
 public function getMenu( $category )
{
...

In the class method getMenu returns an array of links from which the desired menu is built. I use like this:
...
use App\Service\MenuPuller;

class DefaultController extends AbstractController
{
    /**
     * @Route("/", name="app_homepage")
     */
    public function index( MenuPuller $menuPuller  )
    {
  	return $this->render('default/index.html.twig', [
            'main_nav' => $menuPuller->getTopMenu(),
            'footer_nav' => $menuPuller->getDownMenu(),
            'footer_sidebar_nav' => $menuPuller->getDownSidebarMenu(),
            'post' => $_GET
        ]);
    }
}


There is also the Autologin class designed as a service for automatic authorization:
...
class Autologin extends AbstractController
{

    public static function autologin( String $email, Request $request, AbstractController $ac )
    {
        $dispatcher = new EventDispatcher();
        $user = $ac->getDoctrine()->getManager()->getRepository("App\Entity\User")->findOneBy(['email' => $email ]);
        $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
        $ac->get('security.token_storage')->setToken($token);
        $ac->get('session')->set('_security_main', serialize($token));
        $event = new InteractiveLoginEvent($request, $token);
        $dispatcher->dispatch($event);
    }
}
...
// Где-то в коде вызывается так:
// вместо "[email protected]" будет глобальная переменная, здесь просто для примера.

public function dashboardAction()
    {
        Autologin::autologin( "[email protected]", $request, $this );
...
 public function indexAction(Request $request)
    {
        Autologin::autologin( "[email protected]", $request, $this );
...


Those. verification must be carried out in several places, since it is not known from which page the user will go.

If there are few pages, then, IMHO, this approach has the right to exist, but there will be many pages, in addition to these services there will be more x.z. how many similar ones and using a sequence of similar calls in each controller that needs this data starts to resemble "crutches". Tell me the "correct" approaches: the use of events, the inheritance of classes of some system services, well, or something else. Those. ideally, the controller (pages, for example) should have the data it needs, or by accessing a single resource. I hope I explained clearly. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question