# Call Route Function with Parameters in another function

**URL:** <https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088>\
**Category:** Questions\
**Created:** [January 24, 2022, 1:41pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088 "2022-01-24T13:41:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 24, 2022, 1:41pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/1 "2022-01-24T13:41:07Z")

</div>

I am working on slim v4 application i have a one route with the function

example:

```auto
$group->get('/greeting', '\V1:greeting');

```

```auto
function greeting($request,$response,$args,$message){
    //execution code
}

```

i want to call this same function in another function with only one parameter

example

```auto
function hello($message){
   data= this->greeting($message);
   //execution code
}

```

when we call the `greeting` function this ways it throws a error as

`"Call to a member function getBody()`

because of less number of parameter on call , we cannot pass the request,response parameter while call,

so how we can call the route function within another function  
can any one help me to solve this

if there is any more doubt in question feel free to ask

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 24, 2022, 3:24pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/2 "2022-01-24T15:24:15Z")

</div>

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.

For example:

```php
$group->get('/greeting', \App\Action\ExampleGroup\GreetingAction::class);

```

The action class just needs to implement the

`public function __invoke($request, $response, $args)`

method and returns the new response object.

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 24, 2022, 4:22pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/3 "2022-01-24T16:22:50Z")

</div>

greeting and hello are normal function

instead of this can we pass the parameters as null for request and response when it is called from another function

pass arguments as null which are not required

```auto
function hello($message){
   data= this->greeting(null,null,$message);
   //execution code
}

```

when i tried with this it did not return any error  
whether it is good way?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 25, 2022, 12:49pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/4 "2022-01-25T12:49:51Z")

</div>

Generally, **$this** is used inside a class to access (non-static) members of the current object.  
I would suggest to use classes (for everything) instead.

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 27, 2022, 10:44am UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/5 "2022-01-27T10:44:41Z")

</div>

@odan How we can pass the $request and $response parameters as optional parameters in slim v4 is this possible in slim v4

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 27, 2022, 2:51pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/6 "2022-01-27T14:51:11Z")

</div>

This is possible, but quite complex to set up.

By default, Slim controllers have a strict signature: $request, $response, $args.  
As far as I know, the [php-di/slim-bridge](https://php-di.org/doc/frameworks/slim.html#controller-parameters) 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](https://github.com/slimphp/Slim/pull/3132/files) 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.

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 27, 2022, 4:34pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/7 "2022-01-27T16:34:15Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 27, 2022, 5:24pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/8 "2022-01-27T17:24:49Z")

</div>

I would try to solve it on another layer. Either just redirect to the other URL or create a service layer to handle the shared business logic.

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 28, 2022, 4:43am UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/9 "2022-01-28T04:43:26Z")

</div>

@odan Thanks odan , having another doubt what is replacement of `$request->getBody()`  
in slim v4 function without $request and $resposne parameters

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 28, 2022, 9:45am UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/10 "2022-01-28T09:45:36Z")

</div>

Not sure what you mean. Please create a new question and add more details.

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 28, 2022, 11:51am UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/11 "2022-01-28T11:51:10Z")

</div>

@odan in slim v2 we get the body using below query

```auto
$app = new \Slim\Slim();
$body = $app->request->getBody();

```

but in slim v4 function how we can get the body in non route function  
in non route function the `$request` is return as undefined variable

```auto
  function xyz(param1 , param2,param3)
        {      
            $json_request = json_decode($request->getBody());
        }

```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 28, 2022, 2:45pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/12 "2022-01-28T14:45:08Z")

</div>

Now, the Slim 4 Body Parsing Middleware does that for you.

You can read the parsed Json from the HTTP request body using the `$request->getParsedBody()` method.

```php
$data = $request->getParsedBody();

```

Please check the documentation: [Body Parsing Middleware - Slim Framework](https://www.slimframework.com/docs/v4/middleware/body-parsing.html)

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [February 4, 2022, 2:31pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/13 "2022-02-04T14:31:59Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 4, 2022, 6:16pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/14 "2022-02-04T18:16:18Z")

</div>

For further assistance regarding the migration to Slim 4, I would appreciate if you would consult me.

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [February 6, 2022, 1:34pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/15 "2022-02-06T13:34:59Z")

</div>

@odan mostly updated all having only two doubts which was

1. what will be replacement of ` $Redis = $request->container->get('Redis');` this container method in slim 4
2. how we can resolve the class not found error in slim 4  
here i attached the compoer.json file code

```auto
{
    "name": "slim/framework-standard-edition",
    "description": "The \"Slim Standard Edition\" distribution",
    "autoload": {
        "psr-0": { "": "app/" },
    },
    "require": {
        "slim/slim": "4.*",
        "predis/predis": "1.0.3",
        "symfony/event-dispatcher" : "~2.7",
        "symfony/console": "v4.0.2",
        "slim/psr7": "1.5",
        "php-di/php-di": "6.3.0"
    },
    "require-dev": {
        "phpunit/phpunit": "4.*"
    }

}

```

can you help me with the answers for this

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 6, 2022, 4:10pm UTC](https://discourse.slimframework.com/t/call-route-function-with-parameters-in-another-function/5088/16 "2022-02-06T16:10:06Z")

</div>

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:

```php
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”.
