enchance enchance - 1 month ago 8
PHP Question

Laravel: Load method in another controller without changing the url

I have this route:

Route::controller('/', 'PearsController');
Is it possible in Laravel to get the PearsController to load a method from another controller so the URL doesn't change?

For example:

// route:
Route::controller('/', 'PearsController');


// controllers
class PearsController extends BaseController {

public function getAbc() {
// How do I load ApplesController@getSomething so I can split up
// my methods without changing the url? (retains domain.com/abc)
}

}

class ApplesController extends BaseController {

public function getSomething() {
echo 'It works!'
}

}

Answer

You can use (L3 only)

Controller::call('ApplesController@getSomething');

In L4 you can use

$request = Request::create('/apples', 'GET', array());
return Route::dispatch($request)->getContent();

In this case, you have to define a route for ApplesController, something like this

Route::get('/apples', 'ApplesController@getSomething'); // in routes.php

In the array() you can pass arguments if required.

Comments