Coder Social home page Coder Social logo

Comments (2)

weierophinney avatar weierophinney commented on June 18, 2024

Why did you event allow setting a custom attribute key?

To allow usage of multiple different FlashMessageMiddleware instances that were capable of segregating their own sets of messages, without having to worry about naming collisions. When such a scenario is in play, you either hard-code in the other middleware and handlers the other attribute value, or, better, inject it as a dependency.

We tend to discourage static methods within the framework, as they lead to tight coupling, and discourage the ability to have multiple configurations — which is exactly what the configurable attribute key is for.

One possible alternative would be to provide an instance method on the FlashMessageMiddleware, and then compose the middleware as a dependency on anything that needs to fetch messages:

public function __construct(FlashMessageMiddleware $flashContainer)
{
    $this->flashContainer = $flashContainer;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
    $messages = $this->flashContainer->flashMessages($request);
    // ...
}

That said, the following would be similar, and can be done now:

public function __construct(string $flashAttribute)
{
    $this->flashAttribute = $flashAttribute;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
    $messages = $request->getAttribute($this->flashAttribute) ?? [];
    // ...
}

To sum up:

  • Having a configurable attribute name was a design decision to allow segregating messages by domain.
  • As a general rule, we do not provide static methods within the framework, and have, in fact, worked to remove them where we previously had them.
  • Any helper method would have to be attribute-aware, and provide significant convenience over what is already possible.

from mezzio-flash.

rieschl avatar rieschl commented on June 18, 2024

Thanks for the detailed explanation! Yes, the configurable attribute key somewhat defeats the usage of such a static method. And of course, coupling in the framework is an issue, Inside my own projects the coupling is less of a problem, so I did not think of that.
But it's okay, I just stick with my own helper method in the project.

from mezzio-flash.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.