Comments (4)
Basically the container is injectable because it makes the routecollection class testable. Injecting the constructor through the constructor is a design pattern called dependency injection (and more specifically constructor injection). There's no reason why you should try to access the container through the routecollection.
If someone decides to create a more efficient container, they can adhere to the same interface and let the routecollection use it. All optional but nonetheless possible.
from route.
@G4MR Just for a little further clarification, the idea is that the container would be instantiated and configured before you pass it in to the route collection then your controllers can be resolved via the container allowing them to use dependency injection throughout your application. If you any reason you need to access the container after it has been passed in then just interact with the variable that you attached the container to before it was passed in and the changes will persist as PHP deals with objects by reference.
from route.
@philipobenito You create a new route collection and optionally pass your already created DI container to it else RouteCollection will create one anyways. Then you set your routes and choose your strategy. Once you dispatch what ever strategy is chosen, assuming the default RRS, then the dispatcher handles handleFound if found and allows the strategy to do:
protected function invokeController($controller, array $vars = [])
{
if (is_array($controller)) {
$controller = [
$this->getContainer()->get($controller[0]),
$controller[1]
];
}
return call_user_func_array($controller, array_values($vars));
}
right, well what happens here if the container doesn't contain the class/method? It just reflects the class automatically I'm assuming since the container has those tools it's sort of a convenience? Then I guess if container already had said definition it'll attempt to grab it else you're using the container as a tool to reflect class recreation? Wondering if I understood everything correctly and that's the only reason for passing the container?
from route.
Yes that's correct for the class based strategies. If the container is empty it will attempt to reflect on the constructor and inject any arguments. If there is no constructor or no arguments to be passed, the class will just be instantiated as normal.
from route.
Related Issues (20)
- Catch Exception Best Way HOT 3
- Feature request: map with array of methods HOT 1
- Feature request: make $request available in getOptionsCallable HOT 2
- I cannot make post requests HOT 5
- Url resolve fails with 2 numeric parameter HOT 3
- Add an option to get the target route from the middleware HOT 2
- Why isn't the middleware stack executed for unmatched routes? HOT 2
- psr/simple-cache version constraints HOT 2
- Allow for empty route paths to be treat as the base route path HOT 2
- Nested groups are required HOT 1
- Feature request: Treat Route `$handler` that implements RequestHandlerInterface as callable
- Root exception class (and possibly, its children) not compatible with PHP7+
- Request matcher? guess if ServerRequest can be handled
- Dispatcher: handle and find HOT 1
- Feature: Extra map() options
- Method to get declared routes?
- How can I assign controller to errors http ? HOT 1
- Cannot Change Cache Key
- Is this package still actively maintained? HOT 4
- PHP 8 Error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from route.