Answer the question
In order to leave comments, you need to log in
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 )
{
...
...
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
]);
}
}
...
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 );
...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question