The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

There results of hooks

Todd Geist's Avatar

Todd Geist

13 Jan, 2012 06:33 PM

Hello,

I have a slim.before hook that needs to store some data about the request so that the function that gets called by the route can see it later.

I thought "env" would work, but apparently you can only change environment in Middleware. I think I get that this is because env is about Rack and Middleware and I need to do something inside the slim app. So the question is where can I put stuff so I can access it later on down the process?

Todd

  1. 1 Posted by Todd Geist on 13 Jan, 2012 06:45 PM

    Todd Geist's Avatar

    answering myself perhaps...

    would config be the right place to store some thing like that?

    $app->config("user", $user );

  2. 2 Posted by Daniel Lillqvis... on 13 Mar, 2012 10:44 AM

    Daniel Lillqvist's Avatar

    What about storing it in $_SESSION?

    With hooks I have noticed anomalies with version differences:

    // PHP > 5.3

    $app->hook('slim.before.router', function () {});

    should be equal with:

    // PHP < 5.3

    $app->hook('slim.before.router', 'hook_before_router');

    function hook_before_fouter() {}

    The latter is however never executed. Forced me to "hook" by explicitly locate code in the execution path instead.

  3. 3 Posted by Garnier on 13 Mar, 2012 02:24 PM

    Garnier's Avatar

    @Todd Geist the config is used to store the app settings, it would probably work but you could accidentally overwrite the settings or the app itself could overwrite your data.

    Anyway, you can use global variables to store your data or you can create a class with some setters and getters to set and retrieve the data. Storing it in the $_SESSION is also an option if you want it to persist between page loads as Daniel already pointed out.

    @Daniel Lillqvist what version are you using? I'm not having any issue with the hooks using the master branch.

  4. 4 Posted by Daniel Lillqvis... on 13 Mar, 2012 03:07 PM

    Daniel Lillqvist's Avatar

    @Garnier I did this test http://pastebin.com/8DeykXds on both releases. Shouldn't the test output both on a PHP > 5.3 or is there some branching going on depending on the PHP version under the Slim hood?

    My home machine is PHP > 5.3 and production server PHP < 5.3. The production server did not hook at all.

  5. 5 Posted by Garnier on 13 Mar, 2012 03:35 PM

    Garnier's Avatar

    The function name is misspelled like hook_before_fouter instead of hook_before_router, also for PHP < 5.3 you'll need to remove or comment the > 5.3 hook as anonymous functions are not supported and you'll get a parse error.

    A part from that it should work just fine.

  6. 6 Posted by Daniel Lillqvis... on 13 Mar, 2012 05:29 PM

    Daniel Lillqvist's Avatar

    @Garnier I feel like, Doh! :) Anomalies.. yeah right! Confirming that It does work just as it is supposed to! This time not even a proper IDE would have helped me. Thanks for pointing out my blindness!

  7. Andrew Smith closed this discussion on 01 Aug, 2012 12:46 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac