Laravel , how to call a function from another controller

21,207

Solution 1

It is a bad practice to call a controller from another controller, this usually signals that you have badly designed your code and you should think of a different way to achieve what you want.

None the less, you can do it like this:

app()->call('App\Http\Controllers\CarController@getCars');

If your controller method has parameters you can pass them as the second argument:

app()->call('App\Http\Controllers\CarController@getCars', [$param1, $param2]);

Solution 2

To answer your question, you should not call one controller method from another. As @elfu mentioned, this is not the intended functionality of a controller anyway. His post is correct and in your case you should probably use the User model as the location of this method, but I thought I'd at to it a little.

If you do want to share methods between multiple controllers, a good place to do this is through a Trait. In some cases, you are not referencing a model that is shared between controllers, and a Trait would be your best option.

To include a trait, you can reference it by including it at the top of your controller and then with a 'use' statement after the class declaration for the controller. Here is an example:

use App\Traits\ExampleTrait;

class CarController extends Controller
{
    use ExampleTrait;
...

You would do the same in the UserController. Then, any method that you place in the ExampleTrait will be directly accessible from the CarController and the UserController by referencing it as $this->methodName(), just like referencing any other method in the same controller.

In your particular case, I would say that your logic should probably be stored in the User model, since the cars for a user are really an ATTRIBUTE of the User model, but the above gives you another option to work with.

Solution 3

You can call one controller function from another but the best way is to create a trait and use it both the controllers like: trait Common { public function method(){} }

class FirstController extends Controller
{
    use Common;
}

class SecondController extends Controller
{
    use Common;
}

Solution 4

In my humble opinion you should not call another controller in a controller.

It looks like you have some business logic in that controller. So you should move your logic to the entity (User.php) and call it in both controllers methods.

A regular controller returns a view (at least that is what is expected), so if you want to call another controller you should just send that route to that method (in web.php file) instead of calling it in another controller.

Hope that helps you.

Solution 5

If you want to bind parameters to the call, you can use:

    $videos = app()->call('App\Http\Controllers\StorageController@returnViewVideo',[
        'course'=>$course,
        'lesson'=>$lesson,
    ]);
Share:
21,207
edica
Author by

edica

Updated on January 24, 2021

Comments

  • edica
    edica over 3 years

    I have a controller with the "getUsers" function in a controller called "UserController" , and inside it I want to call a function of the "CarController" controller called "getCars", the two options I have are:

    a) Make the second call as "static" , then I can call it without instantiating the class

    b) Do not do that function of the static class and I call it in this way

        $ car_id = 100;
        $ userController = new UserController ();
        $ userController-> getCars ($ car_id);
    

    I do not know which is the best practice, or what pros or cons has one or another.

    I'm using laravel. Thanxs.