symfony筆記之--基礎經常使用知識點

Symfony Basic

  • 獲取登陸用戶信息web

    • 控制器中app

      $this->getUser();
          //等價於
          $this->get('security.token_storage')
          ->getToken()
          ->getUser();
    • Twig中:this

      {% if app.user %}{{ app.user.username }}{% else %}遊客{% endif %}
  • 用戶密碼加密加密

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

    校驗密碼:code

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

    密碼加密:orm

    $password = $encoder->encodePassword($user, $userForm->getPlainPassword());
  • 文件上傳相關token

    $file = $request->files->get('file'); //獲取上傳的文件
       if($file instanceof UploadedFile){
           //不爲空
           $filename = $this->get('kit.file_uploader')->upload($file, 'file');
       }else{
              //未上傳
       }
  • 獲取根目錄和host路由

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

    get HOSTget

    $request()->getHost()
  • 獲取當前路由名稱文件上傳

    // controller
       $routeName = $request->get('_route');
       //twig
       app.request.get('_route')
相關文章
相關標籤/搜索