0
我希望我的項目的索引頁面是一個登錄表單,其下面有一個註冊鏈接,而未登錄的訪問者應該只能看到路由爲/
的登錄表單和路由爲/register
的註冊頁面。當日志我想他們被重定向到路由/home
的主頁。我嘗試了一些東西,它在開發環境中工作(雖然在工具欄上有一些問題 - Symfony2 - dev environment),但是當我切換到prod env時,瀏覽器顯示:「該頁面沒有正確重定向,Firefox檢測到服務器是以永不完成的方式重定向該地址的請求。有時可能由於禁用或拒絕接受cookie而導致此問題。「安全和路由 - Symfony2
這裏是我的文件:
security.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 }
的routing.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 }
WelcomeController.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');
}
}
HomeController.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');
}
}
?>
我有類似配置的東西。區別在於我有 - {path:^/login $,role:IS_AUTHENTICATED_ANONYMOUSLY}和 - {path:^/register,role:IS_AUTHENTICATED_ANONYMOUSLY}。不知道這是否可以解決這個問題 –
謝謝!我會嘗試! – Faery