Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a controller in Laravel 4

In Laravel 3, you could call a controller using the Controller::call method, like so:

Controller::call('api.items@index', $params);

I looked through the Controller class in L4 and found this method which seems to replace the older method: callAction(). Though it isn't a static method and I couldn't get it to work. Probably not the right way to do it?

How can I do this in Laravel 4?

like image 833
Thomas Clackston Avatar asked Mar 04 '13 15:03

Thomas Clackston


People also ask

How do you call a controller in Laravel?

use App\Http\Controllers\UserController; Route::get('/user/{id}', [UserController::class, 'show']); When an incoming request matches the specified route URI, the show method on the App\Http\Controllers\UserController class will be invoked and the route parameters will be passed to the method.

Can a controller call another controller Laravel?

Laravel gives you lots of other other options too i.e. event, jobs, model oberservers etc. If you have two controllers that need to perform the same functionality, then one option is to use a trait which you can then include in both controllers. But you should never need to transfer from one controller to another.

What is $this in Laravel?

The pseudo-variable $this is available when a method is called from within an object context. $ this is a reference to the calling object.


1 Answers

You may use IoC.

Try this:

App::make($controller)->{$action}(); 

Eg:

App::make('HomeController')->getIndex(); 

and you may also give params:

App::make('HomeController')->getIndex($params); 
like image 63
Neto Braghetto Avatar answered Sep 18 '22 13:09

Neto Braghetto