{"id":5195,"date":"2017-03-18T21:48:13","date_gmt":"2017-03-19T01:48:13","guid":{"rendered":"http:\/\/www.dr-chuck.com\/csev-blog\/?p=5195"},"modified":"2017-03-18T21:57:16","modified_gmt":"2017-03-19T01:57:16","slug":"silex-bridge-to-old-style-php-sessions-_session","status":"publish","type":"post","link":"https:\/\/www.dr-chuck.com\/csev-blog\/2017\/03\/silex-bridge-to-old-style-php-sessions-_session\/","title":{"rendered":"Silex Bridge to Old-Style PHP Sessions ($_SESSION)"},"content":{"rendered":"<p>I am exploring the <a href=\"http:\/\/silex.sensiolabs.org\/\" target=\"_blank\">Silex framework<\/a>. I am trying to gently evolve a very nice app from &#8220;old school PHP&#8221; to &#8220;new school PHP&#8221;. I have chosen Silex (atop Symfony) as my first framework to try. I like Silex so far because it helps me without lecturing me. I cannot rewrite my whole app overnight &#8211; I need to evolve to a Silex App, one feature at a time.<\/p>\n<p>This code uses the <a href=\"http:\/\/symfony.com\/doc\/current\/components\/http_foundation\/session_php_bridge.html\" target=\"_blank\">Symfony PhpBridgeSessionStorage<\/a> &#8211; and it makes me very happy.<\/p>\n<pre>\r\n\r\n&amp;lt;?php\r\n\r\n\/\/ http:\/\/localhost:8888\/silex\/hello\/bob\r\n\r\nuse Symfony\\Component\\HttpFoundation\\Session\\Session;\r\nuse Symfony\\Component\\HttpFoundation\\Session\\Storage\\PhpBridgeSessionStorage;\r\n\r\nrequire_once &amp;quot;vendor\/autoload.php&amp;quot;;\r\n\r\nsession_start();\r\n$session = new Session(new PhpBridgeSessionStorage());\r\n$session-&amp;gt;start();\r\n$app = new Silex\\Application();\r\n$app[&amp;apos;session&amp;apos;] = $session;\r\n\r\n$app-&amp;gt;get(&amp;apos;\/hello\/{name}&amp;apos;, function ($name) use ($app) {\r\n    echo(&amp;quot;&amp;lt;pre&amp;gt;\\n&amp;quot;);\r\n    \/\/ New Session\r\n    if ( $app[&amp;apos;session&amp;apos;]-&amp;gt;has(&amp;apos;y&amp;apos;) ) {\r\n        $app[&amp;apos;session&amp;apos;]-&amp;gt;set(&amp;apos;y&amp;apos;, $app[&amp;apos;session&amp;apos;]-&amp;gt;get(&amp;apos;y&amp;apos;)+1);\r\n    } else {\r\n        $app[&amp;apos;session&amp;apos;]-&amp;gt;set(&amp;apos;y&amp;apos;, 20);\r\n    }\r\n    print_r($app[&amp;apos;session&amp;apos;]-&amp;gt;all());\r\n\r\n    \/\/ Read and write old session\r\n    if ( isset($_SESSION[&amp;apos;x&amp;apos;])) {\r\n        $_SESSION[&amp;apos;x&amp;apos;]++;\r\n    } else {\r\n        $_SESSION[&amp;apos;x&amp;apos;] = 42;\r\n    }\r\n    print_r($_SESSION);\r\n\r\n    echo(&amp;quot;&amp;lt;\/pre&amp;gt;\\n&amp;quot;);\r\n    return &amp;quot;&amp;lt;p&amp;gt;Hello $name&amp;lt;\/p&amp;gt;\\n&amp;quot;;\r\n});\r\n\r\n$app-&amp;gt;run();\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am exploring the Silex framework. I am trying to gently evolve a very nice app from &#8220;old school PHP&#8221; to &#8220;new school PHP&#8221;. I have chosen Silex (atop Symfony) as my first framework to try. I like Silex so far because it helps me without lecturing me. I cannot rewrite my whole app overnight [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5195","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/5195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/comments?post=5195"}],"version-history":[{"count":10,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/5195\/revisions"}],"predecessor-version":[{"id":5205,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/5195\/revisions\/5205"}],"wp:attachment":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/media?parent=5195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/categories?post=5195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/tags?post=5195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}