我正在 Symfony 框架中创建我的第一个项目。我选择了4.0版本。我有一个关于创建表单的问题。
配置/routes.yaml
home:
path: /
controller: App\Controller\HomeController::index
methods: [GET]
login:
path: /login
controller: App\Controller\HomeController::login
methods: [POST]
register:
path: /register
controller: App\Controller\HomeController::register
methods: [POST]
timeline:
path: /timeline
controller: App\Controller\TimelineController::index
methods: [GET]
src/Entity/User.php
//
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
*/
private $username;
/**
* @ORM\Column(type="string", length=100)
*/
private $email;
/**
* @ORM\Column(type="string", length=100)
*/
private $password;
/**
* @ORM\Column(type="string", length=50)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=50)
*/
private $lastName;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
public function getId()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function getEmail()
{
return $this->email;
}
public function getPassword()
{
return $this->password;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
public function getCreatedAt()
{
return $this->createdAt;
}
public function setUsername($username)
{
$this->username = $username;
}
public function setEmail($email)
{
$this->email = $email;
}
public function setPassword($password)
{
$this->password = $password;
}
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}
}
src/控制器/HomeController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\User;
class HomeController extends Controller
{
public function index() {
return $this->render('home/index.html.twig');
}
public function login(Request $request) {
// ?????
}
public function register(Request $request) {
$user = new User();
// ?????
}
}
模板/主页/index.html.twig
...
<form action="{{ path('login') }}" id="signInForm" role="form"
method="POST" class="visible">
<h2>Sign In</h2>
<hr class="colorgraph">
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address">
</div>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password">
</div>
<span class="button-checkbox">
<button type="button" class="btn btn-info active" data-color="info">
<i class="state-icon glyphicon glyphicon-check"></i> Remember Me</button>
<input type="checkbox" name="remember_me" id="remember_me" class="hidden">
<a href="" class="btn btn-link pull-right">Forgot Password?</a>
</span>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" class="btn btn-lg btn-success btn-block" value="Sign In">
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<a id="registerBtn" href="#" class="btn btn-lg btn-block btn-toggle">To Register</a>
</div>
</div>
</form>
<form action="{{ path('register') }}" id="registerForm" role="form" method="POST">
<h2>
Please Sign Up
<small>It's free and always will be.</small>
</h2>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="first_name" id="first_name" class="form-control input-lg" placeholder="First Name" tabindex="1">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="last_name" id="last_name" class="form-control input-lg" placeholder="Last Name" tabindex="2">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="display_name" id="display_name" class="form-control input-lg" placeholder="Display Name" tabindex="3">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address" tabindex="4">
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="5">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password_confirmation" id="password_confirmation" class="form-control input-lg" placeholder="Confirm Password"
tabindex="6">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-sm-3 col-md-3">
<span class="button-checkbox">
<button type="button" class="btn" data-color="info" tabindex="7">I Agree</button>
<input type="checkbox" name="t_and_c" id="t_and_c" class="hidden" value="1">
</span>
</div>
<div class="col-xs-8 col-sm-9 col-md-9">
By clicking
<strong class="label label-primary">Register</strong>, you agree to the
<a href="#" data-toggle="modal" data-target="#t_and_c_m">Terms and Conditions</a> set out by this site, including our Cookie Use.
</div>
</div>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" value="Register" class="btn btn-primary btn-block btn-lg" tabindex="7">
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<a id="signInBtn" href="#" class="btn btn-block btn-lg btn-toggle">To Sign In</a>
</div>
</div>
</form>
...
login()
控制器中的andregister()
方法应该是什么?我已经阅读了有关在 Symfony 中创建表单的信息,但我想使用手动完成的视图中的表单。
它应该看起来像这样吗?
public function login(Request $request) {
$email = $request->get('email');
$password = $request->get('password');
$repository = $this->getDoctrine()->getRepository(User::class);
$user = $repository->findOneBy([
'email' => $email,
'password' => $password
]);
if($user) {
// TODO: Set session
return $this->redirectToRoute('timeline');
}
return $this->redirectToRoute('home');
}
public function register(Request $request) {
$user = new User();
$firstName = $request->get('first_name');
$lastName = $request->get('last_name');
$nickname = $request->get('display_name');
$email = $request->get('email');
$password = $request->get('password');
$password2 = $request->get('password_confirmation');
// VALIDATION
return $this->render('message.html.twig', [
'header' => 'Congratulations',
'title' => 'Welcome <b>'. ucfirst($firstName) .' '. ucfirst($lastName) .'</b>!',
'message' => 'The registration process completed correctly!
An activation link has been sent to your e-mail address.',
'button' => [
'href' => $this->generateUrl('home'),
'text' => 'To Sign In'
]
]);
}