【发布时间】:2017-12-07 15:29:51
【问题描述】:
我正在 Symfony 框架中创建我的第一个项目。 我选择了4.0版本。我有一个关于创建表单的问题。
config/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/Controller/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();
// ?????
}
}
模板/home/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() 和 register() 方法中的控制器应该是什么?
我已经阅读了有关在 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'
]
]);
}
【问题讨论】:
-
没有密码哈希?没有盐?如果您是 Symfony 的新手,请开始使用 FosUserBundle。您正在尝试重新发明轮子,而您现在所拥有的是方形的。请阅读symfony.com/doc/current/security/entity_provider.html。
-
无论你做什么都远离 FOSUserBundle,尤其是作为一个新的 Symfony 开发者。相反,请考虑查阅 Symfony 文档:symfony.com/doc/current/security/form_login_setup.html
-
请不要仅仅因为你想要它而手动思考。安全、表单等组件的存在是有原因的。如果您是 Symfony 的新手,有时很难弄清楚某些东西应该如何工作。但是请记住,通过重新发明轮子来弄乱您的应用程序要容易得多(短期内)。如果你想学习;如何配置组件。创建自定义 Twig 模板,扩展 User 类,使用事件系统等。Symfony 是可配置和可扩展的。
-
我不确定我是否同意@Cerad,但我可以想象他为什么这么说。阅读文档(我们已经提供了两个链接,但 Symfony.com 上还有很多其他链接)并自己选择。不管你做什么,开始阅读symfony.com/doc/current/security.html。以目前的形式,这个问题太宽泛了,恕我直言,应该关闭。
-
好的,谢谢。你们真是好人。