Is the greeting and hello function a ānormalā function or a class method?
To handle shared logic, I would recommend implementing invokable classes (single action controller) in combination with service classes for the business logic.
By default, Slim controllers have a strict signature: $request, $response, $args.
As far as I know, the php-di/slim-bridge provides such a feature you are looking for. But I had a lot of troubles to get it working (so I do not use it anymore).
You could also implement your own InvocationStrategyInterface class.
The new RequestResponseNamedArgs class is such an example. But it does only use named parameters (PHP 8) for the route arguments, so you may have to implement your own class to make everything dynamic.
In the long run, all this complexity is not needed. Better keep it simple and use the default signature.
@odan Thanks for the info , when calling the route function inside another function it shows as expected more arguments error because we cannot pass $request $response params while calling inside another function is there any other way to resolve this
@odan i am working updating slim 2 to slim 4 i am getting Call to a member function get() on null error when i call this method $Redis = $request->container->get('Redis');
can you help with what will be replace of this in slim 4
Generally I would try to avoid āmagic stringsā for DI container definitions. Instead try to use FQCN instead, e.g. Predis\Client::class instead of just āRedisā.
Second thing is that the DI container helps you to use ācompositionā. So you do not need to use the DI $container object directly anymore (except within the DI container definition itself).
So the āreplacementā would be to write a class constructor instead of using the DI container instance.
For example:
use Predis\Client;
final class Demo
{
private Client $client;
public function _construct(Client $client)
{
$this->client = $client;
}
// ...
}
Also check your composer.json āautoloadā settings. Please note, as of 2014-10-21 the PSR-0 has been marked as deprecated. PSR-4 is now the recommended way to go.
This should then also solve the āclass not found errorā.