Is there a way to access the container and thus the$app
array of settings from a Slim Model file? It appears that this is ok for controllers but when I try to access$this->container
always comes backnull
.
You can't. All that is not instantiated by the app itself won't be injected with dependencies/have access to the container.
However, there are ways to solve the problem you are facing.
You could simply add a parameter in your constructor that takes the settings array:
function __construct(array $settings){...}
and then pass it when instantiating from a controller.
Pay attention to the fact that a container is not a service locator (see part 1.3 of the PSR), so you should not pass the container directly.
You could create a Factory that takes the array of settings as constructor, then store an instance in your container and add it as property of your class, this is an extension to the first answer that will save you time by not having to pass the settings as argument ever time.
// The factory
class MyModelFactory{
private array $settings;
function __construct(array $settings){
$this->settings = $settings;
}
public function makeInstance(): MyModel
{
return new MyModel($this->settings);
}
}
// In the controller
class Controller{
private MyModelFactory $myModelFactory;
...
// In your route method
$myModelInstance = $this->myModelFactory->makeInstance();
...
}
I have not included how to put the Factory inside the container and how to populate the controller class with it since this depends on the way you setup your application, but it's the easy part of the whole process :)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Slim is, as its name suggests, a micro-framework that is compact and fast. The reason for its such characteristics lies in the fact that it is completely independent of third-party code. It was created in 2010, at the moment its most recent version is 4.5.0.
https://www.slimframework.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.