我有一个大问题,请帮助我.所以我想创建一个只使用以下用户的登录系统:
providers:
in_memory:
memory:
users:
所以我的全局路由:/app/config/routing.yml:
app_admin:
resource: "@AppAdminBundle/Resources/config/routing.yml"
prefix: /admin
我在AdminBundle中的路由:
app_admin_homepage:
path: /
defaults: { _controller: AppAdminBundle:Login:index }
login:
path: /login
defaults: { _controller: AppAdminBundle:Login:login }
login_check:
path: /login_check
我的LoginController:
namespace App\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class LoginController extends Controller
{
public function indexAction()
{
return $this->render('AppAdminBundle:Member:login.html.twig');
}
public function loginAction()
{
$request = $this->get('request');
$session = $this->get('session');
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('AppAdminBundle:Member:login.html.twig', array(
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
}
我的login.html.twig:
Login
而我的security.yml:
security:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_FMTI: ROLE_FMTI
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
access_control:
- { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, role: IS_AUTHENTICATED_ANONYMOUSLY }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
secured_area:
pattern: ^/admin
anonymous: ~
form_login:
login_path: /admin/login
check_path: /admin/login_check
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
providers:
in_memory:
memory:
users:
admin: { password: 111111, roles: 'ROLE_FMTI' }
encoders:
Symfony\Component\Security\Core\User\User: plaintext
当我尝试访问:/ admin / login
我收到此错误:您已请求不存在的服务“请求”.你的意思是其中之一:“router.request_context”,“request_stack”,“monolog.logger.request”,“data_collector.request”?
请你帮助我好吗 ?提前致谢,对不起我的英语
解决方法:
这是因为您试图在控制器中获得不存在的服务.在symfony控制器中,您可以在方法调用中获取请求:
在Logincontroller中,添加以下use语句:
use Symfony\Component\HttpFoundation\Request;
并在您的函数声明中
public function loginAction(Request $request)
{
// you can directly use $request
....
}
您现在可以删除尝试获取不存在服务的行:
// remove this line
$request = $this->get('request');
此外,我不确定你会得到一个get(‘session’)会话.根据Symfony的文档,您应该执行以下操作:
$session = $request->getSession();
标签:php,symfony
来源: https://codeday.me/bug/20190711/1437012.html