这是我的DefaultController.php
文件。我无法解决
注意:未定义的变量:symfony中的会话
在第184行报告了错误。我正在为仪表板创建登录和注销页面。任何人都可以帮我解决这个问题吗?
以下是完整的错误消息:
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 :(得分:1)
您可以通过$request->getSession();
从请求中检索会话,但不绑定到任何变量。将其更改为$session = $request->getSession();
答案 1 :(得分:1)
缺少$ session变量:
$session = $request->getSession();