gpt4 book ai didi

javascript - 试图调用类 "getRequest"的名为 "AppBundle\Controller\DefaultController"的未定义方法

转载 作者:太空宇宙 更新时间:2023-11-04 01:37:31 32 4
gpt4 key购买 nike

我想通过 session 进行登录和注销。但我收到错误。

"Attempted to call an undefined method named "getRequest" of class "AppBundle\Controller\DefaultController".

这是我的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)
{

$session=$this->getRequest()->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)
{
$session=$this->getRequest()->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%}


[1]: /image/40KxK.jpg

最佳答案

Symfony Controller 中的

getRequest() 方法自 2.4 版以来已被弃用,并在 3.0 中被删除。您已经注入(inject)了 $request 对象,因此只需使用 $request->getSession()

关于javascript - 试图调用类 "getRequest"的名为 "AppBundle\Controller\DefaultController"的未定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934989/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com