Slim 4 - middleware - $request->getAttribute() on the way out?

How would you reference $request->getAttribute(“foo”) on the way out in a middleware?

heres my “onion”

"output start"
"profiler start"
"attributes start"
"system start"
"module start"
"module end"
"system end"
"attributes end"
"profiler end"
"output end"

if i set $request->withAttribute(“foo”,“bar”) in any of the first layers up till “route” then the layers after that have access to it. but the same isnt true for the layers under route. (makes sense since the “function” would already have been called.

so im guessing there has to be somethign to do with

$response = $handler->handle($request);

// $response->getAttribute("foo"); but this doesnt work cause not PSR implementation :( 

how does everyone else get this right? or do you simply never use the “way out” parts?

i saw 1 suggestion that says “implement your own response with getAttributes / setAttributes”. but then at which point would you do the “flip” from request to response? it would have to be a middleware right outside the route, but if you’re using a group middleware you wont have access to it.

so im now thinking to just simply not use setAttriubutes on request at all and to roll my own attributes in a container. but that feels dirty. but i cant think of any way to do it “cleanly” using the tools provided (more dirty)

so far my options: (that i see)

  1. custom Response with get / set attributes, add a middleware around route that does a $response->setAttributes($request->getAttributes()) - but have issues with missmatches. or “when” to use $request and $response
  2. place my attributes in a container object and “never” use request->setAttributes(), inject the container “hive” in wherever i want to use a value set.

it doesnt make sense to duplicate the values in both setAttribute and “hive” i guess.

anyone have other suggestions?

i think what slim has achieved is really really good. but i seriously wish it was more… “usefull” instead of “show piece” :frowning: (but im committed to use it now)

I’m noticing some negative vibes here that I really don’t like. Based on your questions, I don’t see that the problem is coming from the Slim side. As a first tip, please try to stay friendly and objective. For example, begin your sentence with a capital letter to make it easier for others to read your question. We are willing to take our valuable time for you, but try to describe the goal and the problem in more detail using examples.

but then at which point would you do the “flip” from request to response?

Please read this Slim 4 - Tutorial first, then you may get the answer.

I’ve read those tutorials sooo many times. So its pretty much “use the way i say you must use it” (follow the tutorial) - but the framework is “open” to you to use as you want (its part of the sell of the framework). “If you don’t follow how I’ve set it out then i wont try help you”. (I’ll soldier on regardless, even if you feel that way)

Sorry if me not using a capital letter in a sentence makes my posts less readable. I’ll try to do better. If you look at my recent posts you might notice that i do include as much code as i possibly can.

And lastly (im not sure if the “A” in and should be capital here -puzzled-) I think your post perfectly illustrates my “negativity”. Instead of helping someone that is clearly struggling with the framework you attack my use of capitalization in sentences.

I honestly have no clue how else to ask the above stuff to make it more clear (my original 2 posts in this thread).

Nice post you made tho.

I’m a newb myself with frameworks and Slim. In my opinion, you have to learn and understand how Slim works before trying to make it work the way you want or expect.

A couple of pro’s have offered suggestions to your posts and you appear to disagree and are sometimes argumentative.

Just my 2 cents.

by the time ive made these posts i had been struggling with slim for AGES to get it to work as i “expect”. sure this is 100% a case of just me struggling with it. if i understood slim the way others do (or coding in general i guess) then i wouldnt ever have had to come onto the forum and ask any questions right?

so me asking a question means that im stuck. means that ive at least been trying to come up with a solution to what im stuck with for at least 2 hours as an absolute minimum.

(im excluding odan’s post in this thread cause its very very not helpful at all, being told “yeah like you being stuck is a you problem” is a massive slap to the face why not just ignore the thread? or at least seem to offer “any” advice?)

some comments re the custom request thread:

  • you shouldnt change the request object cause then you loose psr7 (i disputed it that i dont ever intend changing it so what are the risks of extending it then? surely if all the other request psr7 libraries are exactly the same then why are there multiple of them) “i dont see the value in doing it the way you want to do it, therfore you are wrong?” vibes here? im sure if the pro in question had seen the value in what i want to do they would of made a blog post about it and potentially had a way better solution to my issue which in another thread i described in a lot of detail why i want to do it this way even. just cause someone wants to do something “differently” doesn’t automatically make them “wrong” i distinctly got the vibes of “you want to do it differently, therfore you are wrong” in my interactions here. i do admit, i am probably wrong, but then tell me why im wrong, show me why what i intend wont work instead of just telling me im wrong.
  • akrabat actually gave me advice that on reflection was very accurate and the solution i ultimately went with. but at the time it looked awfully like “include this extra dependency to your project just to be able to extend the request by a single method” and “just go figure it our yourself” when a simple "why dont you do what slim-http is doing by creating a decorator for the ServerRequestInterface Slim-Http/ServerRequest.php at master · slimphp/Slim-Http · GitHub " would of saved me at least a handful of hair and frustration. i actually on more than 1 point in this journey felt like the “pros” got a kick out of watching people struggle vs either just ignoring them or helping them. again this is my opinion. if you go through my posts you will see that in just about every 1 of them i try mention what im struggling with, what goal i want, what ive tried, what im going to still try. (then to be told in this thread “try describe your problem in more detail and examples” really?)

maybe ive re read my posts and i honestly cant see where im “disagreeing” with the pros, i question certain things but i dont disagree with them. (unless my definition of disgree is outdated and simply asking questions is considered disagreeing)

so my question. if you implement things exactly as set out in a copy paste fashion how will you ever learn? dont you ever think of a “better” way to do something (right or wrong, at the time it seemed like a “better” way) just cause no one else thought of it (or seems so, how is it that there is a ton of info on some things but extending the request/response has nothing on it, not even motivations on why its a bad idea, simply nothing. confusing or as at the time i couldn’t find, my googlefu wasnt good enough)

and again. every time you have a problem, its ALWAYS a “you” problem. isnt that the definition of “having a problem”.

ive made silly posts (and ive admitted those) and i will continue making silly mistakes till i die.

the only thing ive been argumentative about would probably be the “read the tutorial” type help. if the tutorial had the answer i needed to solve my problem i wouldn’t be making a post about it right? im sure the tutorial DOES have the answer, its just not described in a way that “solves” my current thought process (so to “me”, the tutorial fails to do what its supposed to, which is answer my question, its akin to “Answer to the Ultimate Question of Life, the Universe, and Everything, is 42” a hint would help… ).