我正在进入symfony,实际上我被授权卡住了。
我想根据用户的角色将其转发到特定的路由。 哦,我正在使用FOS Bundle。
services.yaml:
LogoutListener:
class: App\Listeners\LogoutListener
arguments:
userManager: "@fos_user.user_manager"
security.yaml:
firewalls:
main:
....
logout:
handlers: [logoutlistener]
ListenerClass:
namespace App\Listeners;
use FOS\UserBundle\Model\UserManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
class LogoutListener implements LogoutHandlerInterface {
protected $userManager;
/**
* LogoutListener constructor.
* @param UserManagerInterface $userManager
*/
public function __construct(UserManagerInterface $userManager){
$this->userManager = $userManager;
}
public function logout(Request $request, Response $response, TokenInterface $token) {
die('user logged out');
}
}
它总是给我:
InvalidArgumentException
Invalid service "LogoutListener": method "App\Listeners\LogoutListener::__construct()" has no argument type-hinted as "userManager". Check your service definition.
对我有什么提示吗? 问候deAndro
答案 0 :(得分:1)
Symfony 4的配置有所更改。这应该可以解决问题:
services.yaml
services:
App\Listeners\LogoutListener:
security.yaml
firewalls:
main:
....
logout:
handlers: [App\Listeners\LogoutListener]
ListenerClass
namespace App\Listeners;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
class LogoutListener implements LogoutHandlerInterface {
protected $userManager;
public function __construct(UserManagerInterface $userManager = null)
{
$this->userManager = $userManager;
}
public function logout(Request $Request, Response $Response, TokenInterface $Token) {
die('user logged out');
}
}