Reputation: 2811
I have some routes defined as below:
$app->get('/user/posts', [
"as" => 'user.posts',
"uses" => 'UserController@getPosts'
]);
$app->get('/user/ads', [
"as" => 'user.ads',
"uses" => 'UserController@getAds'
]);
It is possible to call these two routes by their name inside my third route (below)? Something like reverse routing?
$app->get('/user/all', function() use ($app){
/** This does not work
$request = Request::create('MyRouteName', 'GET');
// I can't use the Route class in Lumen
$response = Route::dispatch($request);
**/
return [
"posts" => Request::response_from('user.posts'),
"ads" => Request::response_from('user.ads')
];
});
I would like a method like Request::response_from($routeNameOrUri)
that can get a the data from a route (by calling the controller's action)
Upvotes: -1
Views: 805
Reputation: 964
From the looks of it you're having too much logic in your controllers where you could easily push this towards a model and have your router just point to different controller functions that bind to that model (in a sense).
Usually you want your router to not have inline functions as they cannot be cached and are much slower. Using facades is also a sign that you're probably doing something wrong.
As I said before you're better off extracting whatever data you're retrieving into a user posts model and user ads model and having the controller action get what it needs.
/user/posts -> UserController@posts -> User->posts
/user/ads -> UserController@ads -> User->ads
/user/all -> UserController@all -> User->posts() & User->ads() or User->all()
If your goal is for this to be an api then I highly suggest looking into what RESTful routing is and good design guidelines.
Upvotes: 1