9

In laravel, a given url is routed to a specific controller method. Inside that method, I want to return the response as if the user has visited a different route. I can do this like so:

return App::make('OtherController')->otherMethod();

However, that depends on my hardcoding the class and method names I want to send them to. I would prefer, to send them to another controller identified by the name of the route, rather than the name of the controller class. How can I do that?

One possibility is to return Redirect::route($otherRoute), except that a) this is an actual redirect, which means it adds to the page load time and replaces the url they see, and b) it makes it hard to transfer POST data. So, I don't want to do that.

How can I call a controller, knowing only the name of the route it is linked to?

Benubird
  • 18,551
  • 27
  • 90
  • 141
  • in laravel 5 use $request = Request::create('/internal/location', 'GET'); $response = Route::dispatch($request); – Mruf May 03 '16 at 12:57
  • This works fine in Laravel 4 too. Route::get('events', ['as' => 'events', function () { $request = Request::create(URL::toSite('lp/events'), 'GET'); return Route::dispatch($request); }]); – Etienne Marais Sep 20 '16 at 14:08

1 Answers1

1

As @Mruf said, you could try:

return \Route::dispatch(\Request::create($otherRoute, 'GET')); 
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • When I use this on a remote clinet, the route is not found error appears. If I dd($otherroute) the correct route is displayed. When I copy-paste the dumped route in the browser on the server it works. Any suggestions? – davejal May 02 '17 at 15:03