2017-08-29 53 views
-2

這是我的DefaultController.php文件。我無法解決注意:未定義的變量:在symfony中的184號控制器上的會話號碼爲

注意:未定義的變量:symfony的會話報道

釷錯誤就行號184.我想提出一個登錄和註銷頁面的儀表板。任何人都可以幫我解決這個問題嗎?

以下是完整的錯誤消息:

enter image description here

DefaultController.php

<?php 


namespace AppBundle\Controller; 
use AppBundle\Entity\users; 
use AppBundle\Entity\Login; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Session\Session; 




class DefaultController extends Controller 
{ 
/** 
    * @Route("/login", name="login") 
    */ 

    public function loginAction(Request $request) 
    { 

     $request->getSession(); 
     $em=$this->getDoctrine()->getEntityManager(); 
     $repository=$em->getRepository('AppBundle:users'); 


     if($request->getMethod()=='POST') 
     { 

     $session->clear(); 
     $username=$request->get('username'); 
     $password=$request->get('password'); 
     $remember=$request->get('remember'); 





     $user=$repository->findOneBy(array('userName'=>$username, 'password'=>$password)); 



     if($user) 
     { 
      if($remember=='remember-me') 
      { 
       $login=new Login(); 
       $login->setUserName($username); 
       $login->setPassword($password); 
       $session->set('login', $login); 
      } 
      return $this->render('default/welcome.html.twig', array('name' =>$user->getFirstName())); 

     } 
     else 
      { 
      return $this->render('default/login.html.twig', array('name' => 'Login Error')); 
     } 
    } 
    else{ 

     if($session->has('login')) 
     { 
      $login=$session->get('login'); 
      $username=$login->getUserName(); 
      $password=$login->getPassword(); 
      $user=$repository->findOneBy(array('userName'=>$username, 'password'=>$password)); 

    if($user) 
     { 
    return $this->render('default/welcome.html.twig', array('name' =>$user->getFirstName())); 
     }  

     } 

     return $this->render('default/login.html.twig'); 
    } 
    } 



    /** 
    * @Route("/signupe", name="signupe") 
    */ 

    public function signupeAction(Request $request) 
    { 
      if($request->getMethod()=='POST'){ 

       $username=$request->get('username'); 
       $firstname=$request->get('firstname'); 
       $password=$request->get('password'); 

       $user = new Users(); 
       $user->setFirstName($firstname); 
       $user->setPassword($password); 
       $user->setUserName($username); 
      $em=$this->getDoctrine()->getEntityManager(); 
      $em->persist($user); 
      $em->flush(); 



    } 

    return $this->render('default/signupe.html.twig'); 

} 

    /** 
    * @Route("/logout", name="logout") 
    */ 

    public function logoutAction(Request $request) 
    { 
    $request->getSession(); 
    $session->clear(); 
    return $this->render('default/login.html.twig'); 
    } 

} 

這是我login.html.twig文件。在這裏我提到我們的用戶名密碼和「記住我」字段。我正在嘗試創建一個登錄和註銷頁面,以便與我的儀表板連接。

<html> 
<head> 
{% block stylesheets %} 
<link rel="stylesheet" href="{{ asset('templates/css/1.css') }}"> 
<link rel="stylesheet" href="{{ asset('templates/css/parsley.css') }}"> 
{% endblock %} 

{% block javascripts %} 

<script src="{{ asset('templates/js/parsley.min.js') }}"></script> 
<script src="{{ asset('templates/js/parsley.extend.min.js') }}"></script> 
<script src="{{ asset('templates/js/parsley-standalone.min.js') }}"></script> 
{% endblock %} 
</head> 
<body> 


{% block container %} 

<div class="wrapper"> 
     <h2>Login User</h2>      
     <form class="form-sigin" method="POST" action="{{path('login')}}" data-validate="parsley"> 
     <div><input type="text" name="username" class="txtbox" placeholder="Email Address" data-trigger="change" data-required="true" data-type="email"></div> 
     <div><input type="password" name="password" class="txtbox" placeholder="Password" data-trigger="change" data-required="true"></div> 
     <div> 
     <label class="checkbox"> 
     <input type="checkbox" value="remember-me"><span>Remember Me</span></div> 
     <div><input type="submit" value="Log In" class="loginBtn"></div> 
     <div><span><a href="">Forgot Password</a></span></div> 



     </form> 
    </div> 
    {% if name is defined %} 
    <div class="alert-info fade in"> 
    <strong>How are you</strong> 
    </div> 
    {% endif %} 
    {% endblock %} 
    </body> 
    </html> 

這是我的signupe.html.twig。這裏我提到了所有需要註冊表單的字段。

{% block stylesheet %} 

    <link rel="stylesheet" href="{{ asset('templates/css/1.css') }}"> 
    <link rel="stylesheet" href="{{ asset('templates/css/parsley.css') }}"> 

    <style> 
form { 
    border: 0px solid #f1f1f1; 
} 

input[type=text]{ 
    /* width: 100%; */ 
    padding: 12px 20px; 
    margin: 8px 0; 
    display: inline-block; 
    border: 1px solid #ccc; 
    box-sizing: border-box; 
} 

input[type=password] { 
    /* width: 100%; */ 
    padding: 12px 20px; 
    margin: 8px 0; 
    display: inline-block; 
    border: 1px solid #ccc; 
    box-sizing: border-box; 
} 


button { 
    background-color: #4CAF50; 
    color: white; 
    padding: 11px 22px; 
    margin: 8px 0; 
    border: none; 
    cursor: pointer; 
    text-align:center; 
    /* width: 100%; */ 
} 

.Btn { 
    margin-left: 50px; 
    } 

button:hover { 
    opacity: 0.8; 
} 

.cancelbtn { 
    width: auto; 
    padding: 11px 18px; 
    background-color: #f44336; 
} 

.imgcontainer { 
    text-align: center; 
    margin: 24px 0 12px 0; 
} 

img.avatar { 
    width: 40%; 
    border-radius: 50%; 
} 

.container { 
    padding: 16px; 
    text-align:center; 
} 

span.psw { 
    float: center; 
    padding-top: 16px; 
} 

/* Change styles for span and cancel button on extra small screens */ 
@media screen and (max-width: 300px) { 
    span.psw { 
     display: block; 
     float: none; 
    } 
    .cancelbtn { 
     width: 100%; 
    } 
} 
</style> 

    {% endblock %} 


    {% block javascripts %} 

<script src="{{ asset('templates/js/parsley.min.js') }}"></script> 
<script src="{{ asset('templates/js/parsley.extend.min.js') }}"></script> 
<script src="{{ asset('templates/js/parsley-standalone.min.js') }}"></script> 

    {% endblock %} 


    {% block container %} 

    <h2 align="center">Sign Up</h2> 
    <form method="POST" action="{{path('signupe') }}" data-validate="parsley"> 
    <div class="container"> 
    <label><b>User Name</b></label> 
    <input type="text" id="username" placeholder="Enter User Name" name="username" placeholder="Email Address" data-trigger="change" data-required="true" data-type="email"> 
    <br> 

    <label><b>Repeat User Name</b></label> 
    <input type="text" id="usernameRe" placeholder="Enter User Name" name="usernameRe" placeholder="Email Address" data-trigger="change" data-required="true" data-type="email" data-equalto="#username"> 
    <br> 

    <label><b>First Name</b></label> 
    <input type="text" placeholder="Enter First Name" name="firstname" placeholder="Email Address" data-trigger="change" data-required="true"> 
    <br> 


    <label><b>Password</b></label> 
    <input type="password" placeholder="Enter Password" name="password" placeholder="Email Address" data-trigger="change" data-required="true"><br> 
    <div class="Btn"> 
    <button type="submit">Create Account</button> 
    </div> 
    </form> 
    </div> 


     {% endblock %} 

這是我的welcome.html.twig文件。在這裏我提到登錄頁面後,我將在這裏重定向該頁面,我也可以註銷。

{%block container %} 
<div class="container"> 
    <h2>Hello {{name}}</h2> 

</div> 
<a href="{{path('logout')}}">Logout</a> 
    {%endblock%} 
+0

請閱讀[MCVE。你在這裏提供的肯定不是「最小」。 – Chris

回答

0

缺少$ session變量:

$session = $request->getSession(); 
+0

謝謝先生,它的工作,但我如何檢查我的會議做工? –

+0

很高興幫助。請標記爲正確答案:)。要檢查它,你可以var_dump($ session-> get('login'))debug。問候 – Albeis

+0

先生其實當我點擊返回並點擊前進我想我的歡迎頁面意味着(我的會話)註銷後我可以回到登錄頁面。你能幫助我完成這個任務嗎?我非常感謝你。 –

0

您從請求中檢索會話$request->getSession();但不綁定到任何變量。將其更改爲$session = $request->getSession();

+0

謝謝先生,它的工作,但我如何檢查我的會議做工? –