无论出于何种原因, session 在我的 Silex 应用程序中都不起作用。我在 php.ini 中将 error_reporting 设置为 E_ALL | E_STRICT 并显示和记录错误。那里没什么可看的。但是由于某种原因,没有创建 session 并且 /project-root/tmp/sessions/ 中没有文件(使用默认 session.save_path 时也没有)。切换到 PdoSessionStorage,以排除文件系统上的读/写权限问题,也没有带来任何结果。我还尝试在 $app['session']、$request->getSession() 和 $app['request']->getSession() 之间切换 无济于事。
我不知道在哪里寻找问题...
这是我写的一个非常简单的测试应用程序(use 被省略以节省空间)。基本上这个测试展示了我试图在我的实际应用程序中实现的目标。我想在 session 中存储一个数组。在 $app->before() 中,我检查该值是否已设置,然后将其传递给要显示的 twig,例如登录信息:您已登录为 {{ user.name }}:
$app = new Application;
$app['debug'] = true;
$app->register(new SessionServiceProvider, array(
'session.storage.save_path' => dirname(__DIR__) . '/tmp/sessions'
));
$app->register(new TwigServiceProvider, array(
'twig.path' => __DIR__ . '/views'
));
$app->before(function (Request $request) use ($app) {
// if ($app['debug'] == true) {
// $request->getSession()->set('test', array('key' => 'value'));
// }
if ($request->hasPreviousSession() && $request->getSession()->has('test')) {
$test = $request->getSession()->get('test');
if ($test !== null) {
$app['twig']->addGlobal('before', $test);
}
}
});
$app->get('/', function () use ($app) {
return $app['twig']->render('sessiontest.twig');
});
$app->get('/test', function () use ($app) {
$app['session']->set('test', array('key' => 'value'));
return $app['twig']->render('sessiontest.twig',
array('get' => $app['session']->get('test')));
});
$app->run();
sessiontest.twig 看起来像这样:
<html>
<head><title>test</title></head>
<body>
{% if before is defined %}before: {{ before.key }}{% endif %}
{% if get is defined %}get: {{ get.key }}{% endif %}
</body>
</html>
going to/什么都不显示,going to/test 只显示“get:test”(但实际上没有存储,因为之前既没有返回/也没有刷新触发器)。
最佳答案
现在您需要手动启动 session :
$app->before(function ($request) {
$request->getSession()->start();
});
silex tracker里面有关于这个的ticket,还没来得及细想。问题是,如果您这样做,您将为每个用户创建一个 session 。每次使用都有一个 session ,意味着每个用户都会得到 cookie。这是你可能不想要的。这就是它此时没有自动启动的原因。
我希望这能让事情变得更清楚一些。
关于php - session 在 Silex\App 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9734549/
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我正在使用Postgres.app在OSX(10.8.3)上。我已经修改了我的PATH,以便应用程序的bin文件夹位于所有其他文件夹之前。Rammy:~phrogz$whichpg_config/Applications/Postgres.app/Contents/MacOS/bin/pg_config我已经安装了rvm并且可以毫无错误地安装pggem,但是当我需要它时我得到一个错误:Rammy:~phrogz$gem-v1.8.25Rammy:~phrogz$geminstallpgFetching:pg-0.15.1.gem(100%)Buildingnativeextension
使用rails4,ruby2。我在rails配置中为我的cookiesession设置了30分钟的超时时间。问题是,如果我转到表单,让session超时,然后提交表单,我会收到此ActionController::InvalidAuthenticityToken错误。如何在Rails中优雅地处理这个错误?比如说,重定向到登录屏幕? 最佳答案 在您的ApplicationController:rescue_fromActionController::InvalidAuthenticityTokendoredirect_tosome_p
我去了这个website查看Rails5.0.0和Rails5.1.1之间的区别为什么5.1.1不再包含:config/initializers/session_store.rb?谢谢 最佳答案 这是删除它的提交:Setupdefaultsessionstoreinternally,nolongerthroughanapplicationinitializer总而言之,新应用没有该初始化器,session存储默认设置为cookie存储。即与在该初始值设定项的生成版本中指定的值相同。 关于
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw
我正在尝试使用Sinatra中的重定向和session在网站周围传递一些数据。这是一个简化的示例,使用PrettyPrint进行调试:require'pp'require'rubygems'require'sinatra'enable:sessionsget'/'dosession[:foo]='12345'puts'session1'ppsessionredirectto('/redir')endget'/redir'doputs'session2'ppsession'helloworld'end查看Thin的输出,我看到:>>Listeningon0.0.0.0:4567,CTRL