我希望我的项目的索引页面是一个登录表单,下面有一个注册链接,未登录的访问者应该只能看到带有路由的登录表单和带有路由/
的注册页面/register
。当日志我希望他们被重定向到主页时 route /home
。我尝试了一些东西,它在开发环境中工作(虽然工具栏有一些问题 - Symfony2 - dev environment)但是当我切换到 prod env 时,浏览器说:“页面没有正确重定向。Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。这个问题有时可能是由禁用或拒绝接受 cookie 引起的。
这是我的文件:
安全.yml
security:
encoders:
EM\MyFriendsBundle\Entity\User:
algorithm: sha1
encode_as_base64: false
iterations: 1
role_hierarchy:
ROLE_ADMIN: ROLE_USER
providers:
administrators:
entity: { class: EMMyFriendsBundle:User }
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
default_target_path: /home
access_control:
- { path: ^/home, roles: ROLE_ADMIN }
路由.yml
login_display:
pattern: /
defaults: { _controller: EMMyFriendsBundle:Welcome:display }
login:
pattern: /login
defaults: { _controller: EMMyFriendsBundle:Welcome:login}
login_check:
pattern: /login_check
register:
pattern: /register
defaults: { _controller: EMMyFriendsBundle:Welcome:register }
home_display:
pattern: /home
defaults: { _controller: EMMyFriendsBundle:Home:display }
欢迎控制器.php
<?php
namespace EM\MyFriendsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class WelcomeController extends Controller
{
public function displayAction()
{
$error=null;
$last_username=null;
return $this->render('EMMyFriendsBundle:Welcome:login.html.twig', array('error' => $error, 'last_username' => $last_username));
}
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('EMMyFriendsBundle:Welcome:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error
));
}
public function registerAction()
{
return $this->render('EMMyFriendsBundle:Welcome:register.html.twig');
}
}
家庭控制器.php
<?php
namespace EM\MyFriendsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller
{
public function displayAction()
{
return $this->render('EMMyFriendsBundle:Home:home.html.twig');
}
}
?>