コントローラ内でセッションの値を保存しています。セッション変数の値を取得できませんsymfony
public function setSessionValue($key,$val){
$session = $this->get('session');
$session->set($key,$val);
echo "Token is: ".$session->get('token');
}
トークンは次のとおりです。私は別の方法を使用して、セッションからそのトークン値を取得しています別の要求では
をe129cb3b547b6017a9f69fe30a96efed。
public function getSessionValue($key){
$session = $this->get('session');
echo "Token is: ".$session->get('token');
return $session->get($key);
}
今応答は次のとおりです。
トークンは次のとおりです。NULL
以下のように私のsecurity.ymlファイル。
# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:
# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
in_memory:
memory: ~
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
#http_basic: ~
# https://symfony.com/doc/current/security/form_login_setup.html
#form_login: ~
私の完全なコントローラクラスはようです:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
class BaseController extends Controller{
public $request;
public function setSessionValue($key,$val){
$session = $this->get('session');
$session->set($key,$val);
echo "Token is: ".$session->get('token');
}
public function getSessionValue($key){
$session = $this->get('session');
echo "Token is: ".$session->get('token');
return $session->get($key);
}
}
そして私のような何かをしています。
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use AppBundle\Entity\UserData;
use AppBundle\Bl\LoginOperation;
use AppBundle\Controller\BaseController;
class LoginController extends BaseController
{
/**
* @Route("/login", name="loginpage")
*/
public function loginAction($request)
{
/* more code */
//storing token
$this->setSessionValue('token',$obj->token);
$this->setSessionValue('uid',$user->getUserId());
$response = json_encode($obj);
return new Response($response);
}
}
何故その理由が考えられますか?私は様々な方法で試しましたが、2回目のリクエストで値を取得できませんでした。
どのようなアイデアが私にとって非常に役に立ちます。この例の場合、1つのファイアウォール(アプリ/設定/のsecurity.ymlファイルを参照してください)
import:
pattern: ^/import$
http_basic:
realm: "Secured Auth"
provider: myprovider
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout: true
anonymous: true
の下でログインしているsymfonyにおいて
、もし:あなたの情報で
"これ"は何のコントローラーですか? –
はい、これはコントローラです。 –
セッションの作成方法symfonyセッションかネイティブPHPセッションですか? なぜ、あなたはtokenStorageを使用していませんか? – Hornth