Может быть, это не является хорошим решением, но это быстро и должно работать.
Вы можете написать пользовательские UrlGenerator
что добавит лексему URLS
namespace nnn;
class UrlGenerator extends \Symfony\Component\Routing\Generator\UrlGenerator
{
protected $csrfToken;
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
{
$url = parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
if (isset($this->csrfToken)) {
$url = rtrim($url, '/') . '/key/' . $this->csrfToken;
}
return $url;
}
}
и UrlMatcher
, что позволит сократить маркер из URLs
namespace nnn;
class RedirectableUrlMatcher extends \Silex\RedirectableUrlMatcher
{
protected function matchCollection($pathinfo, \Symfony\Component\Routing\RouteCollection $routes)
{
$pathinfo = preg_replace('~/key/.+?(/|$)~', '$1', $pathinfo);
if ($pathinfo == '') $pathinfo = '/';
return parent::matchCollection($pathinfo, $routes);
}
}
и настроить приложение
$app['url_generator'] = $app->share(function ($app) {
$app->flush();
$urlGenerator = new \nnn\UrlGenerator($app['routes'], $app['request_context']);
$urlGenerator->setCsrfToken('1234567890');
return $urlGenerator;
});
$app['url_matcher'] = $app->share(function() use ($app) {
return new \nnn\RedirectableUrlMatcher($app['routes'], $app['request_context']);
});
$app['dispatcher']->addListener(\Symfony\Component\HttpKernel\KernelEvents::REQUEST, function($event) use ($app) {
$request = $event->getRequest();
$path = $request->getPathInfo();
if (\preg_match('~/key/(.+?)(?:/|$)~', $path, $matches)) {
$request->attributes->set('_csrfToken', $matches[1]);
}
}, 1024);
$app->run();
Токен будет доступен в контроллеры через $app['request']->get('_csrfToken')
Могу ли я спросить, какую проблему вы пытаетесь решить, выполнив это? например, для чего вы используете CSRF_TOKEN? Если возвращается форма, форма будет иметь токен, и он будет в сеансе. Если вы не получаете форму, вам не нужен токен? –
Как и Magento, добавьте секретный ключ к URL-адресу для CSRF (подделка подбора сайтов). – Ste
Спасибо за обновление. Это помогает мне понять больше. –