2016-07-22 95 views
0

在Symfony的,我想創建一個loginpage,但我得到了以下錯誤:Symfony的錯誤登錄

Variable "error" does not exist in QuizBundle:Default:welcome.html.twig at line 37

有人可以幫助我? 這裏是我的代碼:

類:

class SecurityController extends Controller 
{ 
    public function indexAction($name) 
    { 
     return $this->render('', array('name' => $name)); 
    } 

    /** 
    * @Route("/welcome", name="welcome") 
    */ 
    public function welcomeAction(Request $request) 
    { 
     $authenticationUtils = $this->get('security.authentication_utils'); 

     // get the login error if there is one 
     $error = $authenticationUtils->getLastAuthenticationError(); 

     // last username entered by the user 
     $lastUsername = $authenticationUtils->getLastUsername(); 



     return $this->render('@Quiz/Default/welcome.html.twig', array(
       // last username entered by the user 
       'last_username' => $lastUsername, 
       'error'   => $error, 
      ) 
     ); 
    } 
} 

嫩枝文件:

<div id="loginfields" style="display: none"> 
       {% if error %} 
        <div>{{error.messageKey|trans(error.messageData, 'security')}}</div> 
       {% endif %} 

       <form class="form-signin" action="{{path('welcome')}}" method="post"> 
        <h2 class="form-signin-heading">Please sign in</h2> 
        <label class="sr-only" for="username">Username:</label> 
        <input type="text" id="username" placeholder="Username" class="form-control" name="_username" value="{{ last_username }}"/> 

        <label class="sr-only" for="password">Password:</label> 
        <input type="password" id="password" class="form-control" placeholder="Password" name="_password" /> 
        <input type="hidden" name="_target_path" value="/admin" /> 

        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
       </form> 
      </div> 

Security.yml

security: 
    encoders: 
      QuizBundle\Entity\User: plaintext 

    providers: 
      our_db_provider: 
       entity: 
        class: QuizBundle:User 

    firewalls: 
      main: 
       anonymous: ~ 
       form_login: 
        login_path: welcome 
        check_path: welcome 

回答

1

要使用error變量(或避免異常/錯誤,當您嘗試使用也許不會存在的變量),你需要檢查它是否存在這樣的:在define Twig documentation

{% if error is defined %} 
    <div>{{error.messageKey|trans(error.messageData, 'security')}}</div> 
{% endif %} 

更多的相關信息。

+0

對不起,延遲迴復。問題是我有一個login.html.twig文件,它與我的問題中發佈的文件完全相同,但在這個文件中我有'

'。然後在SecurityController類中,我有:'@Route(「/ login」,name =「login」)',並且在我的security.yml中有:'login_path:login check_path:login'並且一切正常 – Otonel

+0

不要緊,從我的角度來看,這是一個愚蠢的錯誤,因爲我在另一個控制器的其他地方有/歡迎路徑。 – Otonel