# How to invoke a route "manually"

**URL:** <https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458>\
**Category:** Questions\
**Created:** [April 30, 2017, 1:08pm UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458 "2017-04-30T13:08:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![wqking](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/wqking/32/383_2.png) [@wqking](https://discourse.slimframework.com/u/wqking)\
**Post date:** [April 30, 2017, 1:08pm UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/1 "2017-04-30T13:08:03Z")

</div>

Hi all,

With Slim App get/post/patch etc functions, I can register routes and Slim will invoke them automatically.  
My question is how can I invoke those routes from my code?  
What I want to do is to batch the requests.  
For example, I may have routes such like  
get(’/a’);  
get(’/b’);  
Then I may have a get(’/batch’), the batch route parses a JSON data to extract a sequence of sub request such as /a and /b, then it invoke the routes on /a and /b manually.

Thanks

---

<div class="post-metadata">

**Author:** ![wqking](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/wqking/32/383_2.png) [@wqking](https://discourse.slimframework.com/u/wqking)\
**Post date:** [May 3, 2017, 11:13am UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/2 "2017-05-03T11:13:25Z")

</div>

$app-\>subRequest is the answer.

---

<div class="post-metadata">

**Author:** ![SVL](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SVL](https://discourse.slimframework.com/u/SVL)\
**Post date:** [May 3, 2017, 11:42am UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/3 "2017-05-03T11:42:39Z")

</div>

The sub-request functionality will not be available in Slim 4.

---

<div class="post-metadata">

**Author:** ![wqking](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/wqking/32/383_2.png) [@wqking](https://discourse.slimframework.com/u/wqking)\
**Post date:** [May 3, 2017, 12:02pm UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/4 "2017-05-03T12:02:56Z")

</div>

Thanks. Then what’s the alternative?

---

<div class="post-metadata">

**Author:** ![SVL](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SVL](https://discourse.slimframework.com/u/SVL)\
**Post date:** [May 3, 2017, 12:15pm UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/5 "2017-05-03T12:15:47Z")

</div>

You probally need to review your current design.

> [@SubRequest handling into another route](http://discourse.slimframework.com/t/subrequest-handling-into-another-route/140/18):
>
> We are removing subRequest as it does not fit well with proper code design. In instances where one might need a “sub” request, it is almost always the wrong design choice to resend an HTTP Request through Slim again. Typically we remove features that are not considered good practice with every Major release [Slim 2 =\> 3 we removed service location … as an example].

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [May 3, 2017, 1:14pm UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/6 "2017-05-03T13:14:44Z")

</div>

@wqking The answer is to refactor your application to use best practices.

---

<div class="post-metadata">

**Author:** ![damianopetrungaro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/damianopetrungaro/32/236_2.png) [@damianopetrungaro](https://discourse.slimframework.com/u/damianopetrungaro)\
**Post date:** [May 14, 2017, 5:33pm UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/7 "2017-05-14T17:33:27Z")

</div>

You can use the same action from different routes.

But why have you this issue?  
I think is a design issue as @geggleto and @SVL said you should refactor your application.

---

<div class="post-metadata">

**Author:** ![wqking](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/wqking/32/383_2.png) [@wqking](https://discourse.slimframework.com/u/wqking)\
**Post date:** [May 15, 2017, 6:30am UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/8 "2017-05-15T06:30:49Z")

</div>

@SVL, @geggleto, @damianopetrungaro, thanks for your advice.  
But I can’t see the issue in the design of “batch” idea.  
Assume I have API f1 and f2, and 3 pages A, B, C. A uses f1, B uses f2, and C uses both f1 and f2. In page C, instead of sending two requests, I would like to batch f1 and f2 in one single request to save HTTP connections.  
Thoughts?

---

<div class="post-metadata">

**Author:** ![damianopetrungaro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/damianopetrungaro/32/236_2.png) [@damianopetrungaro](https://discourse.slimframework.com/u/damianopetrungaro)\
**Post date:** [May 15, 2017, 7:56am UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/9 "2017-05-15T07:56:43Z")

</div>

@wqking sorry but this could be a bad design idea.

You can abstract the two actions in two methods of a service.  
So the content of f1and f2, will be available from the service-\>f1 and service-\>f2.

If you explains to us the real example, we’ll help you in a better way.

---

<div class="post-metadata">

**Author:** ![wqking](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/wqking/32/383_2.png) [@wqking](https://discourse.slimframework.com/u/wqking)\
**Post date:** [May 15, 2017, 8:35am UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/10 "2017-05-15T08:35:18Z")

</div>

I mean, yes, I do provide two methods f1 and f2, but whenever the client app needs both f1 and f2, I want to batch them in a single HTTP request. The sever still calls the handlers of f1 and f2 respectively. My purpose to do it is to save HTTP connections (1 connection for both f1 and f2 instead of 2 for f1 and f2).  
If anyone in doubt googles “restful batch”, there are pretty some discussion on this topic.  
Any way, since Slim is a framework, I wish it keeps the subRequest API because it’s useful even though the Slim developers don’t think so.

---

<div class="post-metadata">

**Author:** ![damianopetrungaro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/damianopetrungaro/32/236_2.png) [@damianopetrungaro](https://discourse.slimframework.com/u/damianopetrungaro)\
**Post date:** [May 15, 2017, 8:40am UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/11 "2017-05-15T08:40:57Z")

</div>

Batch requests can be handled in other way.  
If you explains your real example we can help you in a better way 😃

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [May 15, 2017, 3:54pm UTC](https://discourse.slimframework.com/t/how-to-invoke-a-route-manually/1458/12 "2017-05-15T15:54:43Z")

</div>

The answer is always better design. 🙂 More abstraction.
