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

Why are there 2 different request objects passed in a class based route...

eko3alpha's Avatar

eko3alpha

19 Jan, 2016 07:43 PM

The Slim\Http\Request Object in the Slim\Container that is passed to the class constructor is not the same instance of the Slim\Http\Request being passed in the method. I did a var_dump on both and their object reference ID's are different. The problem I'm having is I need access to the Slim\Http\Request instance thats being passed in the method in the constructor.

// route
$app->get('/some-page/do-something/', 'SomeClass:doSomething');

// SomeClass.php
class SomeClass extends BaseController  
{
    public function doSomething($request, $response, $args)
    {
        var_dump($request);  // Slim\Http\Request Object ID 1
    }
}

// BaseController.php
abstract class BaseController  
{
    public function __construct($container)
    {
        var_dump($container->request); // Slim\Http\Request Object ID 2
    }
}

Why are these two objects different instances? Should they not be the same?

This is something related to the issue I was trying to initially solve, how to get current route name in controller constructor.

you can read more on this here...
https://stackoverflow.com/questions/34859752/access-current-route-n...

Thanks!

  1. Support Staff 1 Posted by Andrew Smith on 19 Jan, 2016 11:53 PM

    Andrew Smith's Avatar

    Yes they are different in Slim 3, because PSR7 Request/Response objects are immutable. The original object will remain in its original state, but a cloned version of that object is what gets modified and passed around.

    If you need to share something like that in your class constructor, I would suggest creating a singleton and passing around values in that instead.

  2. 2 Posted by eko3alpha on 20 Jan, 2016 02:51 AM

    eko3alpha's Avatar

    I suspected it had something to do with PSR7 immutable objects. Im currently learning the consequences of adopting PSR7 as I upgrade my Slim2 projects to Slim3. I've had to rethink how I do things like redirects, templates, json api responses. It's been surprisingly easy, and even though there have been some slight hiccups, I do like the overall direction. It's starting to grow on me... slowly...

    Great job!

  3. Support Staff 3 Posted by Andrew Smith on 20 Jan, 2016 01:37 PM

    Andrew Smith's Avatar

    I am happy you are enjoying it. Even myself at times get caught out by the PSR7 stuff, I just have to constantly remind myself its not Slim 2 anymore.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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