Symfony Basic

  • Gets the login user information

    • The controller

      $this->getUser(); $this->get('security.token_storage') ->getToken() ->getUser();
    • The Twig is:

      {% if app. The user %} {{app. The user. The username}}} else {% % tourists {% endif %}
  • User password encryption

       /**
        *@var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
        */
       $encoder = $this->get('security.password_encoder');
    

    Verify password:

    $encoder - > isPasswordValid ($user and $userForm - > getPassword ());

    Password encryption:

       $password = $encoder->encodePassword($user, $userForm->getPlainPassword());
    
  • File Upload Related

    $file = $request->files->get('file'); $fileName = $this->get('kit.file_uploader')->upload($file, 'file'); $file_uploader = $this- BBB get('kit.file_uploader')->upload($file, 'file'); }else{// Not uploaded}
  • Get the root directory and host

    $this->getContainer()->get('kernel')->getRootDir() $root = $this->container->get('kernel')->getRootDir(); $root = $this->get('kernel')->getRootDir(); $root = $this->get('kernel')->getProjectDir() %kernel.project_dir% use %kernel.project_dir%/web/ instead of %kernel.root_dir%/.. /web/.

    get HOST

       
       $request()->getHost()
  • Gets the current route name

       // controller
       $routeName = $request->get('_route');
       //twig
       app.request.get('_route')