Laravel , how to call a function from another controller
Asked Answered
P

7

5

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.

Psalm answered 8/11, 2018 at 14:1 Comment(0)
S
10

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]);
Starstudded answered 8/11, 2018 at 14:12 Comment(0)
D
3

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.

Dallman answered 8/11, 2018 at 14:21 Comment(0)
J
2

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.

Joel answered 8/11, 2018 at 14:9 Comment(0)
H
1

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;
}
Hushaby answered 8/11, 2018 at 14:55 Comment(0)
B
1

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

    $videos = app()->call('App\Http\Controllers\StorageController@returnViewVideo',[
        'course'=>$course,
        'lesson'=>$lesson,
    ]);
Babettebabeuf answered 23/1, 2021 at 12:56 Comment(0)
L
0

The following code worked for me well. and also it also can be used in routes.php

 public function mobileImageUpload(Request $request){
    $this->validate($request,[
        'data'=>'required',
        'filetype'=>'required',
        'userid'=>'required',
    ]);
    $namespace = 'App\Http\Controllers';
    $controller = app()->make($namespace.'\ImageController');
    return $controller->callAction('mobileImageUpload',[$request]);
}
Laundryman answered 18/6, 2019 at 9:12 Comment(0)
O
0

you can use it like

 // Convert the array to a Request object
        $request = Request::create('/store', 'POST', $requestData);

        // Instantiate CustomerController
        $customerController = app(CustomerController::class);

        // Call the store method and pass the Request object and sync_process flag
        $response = $customerController->store($request);
Overwrought answered 2/8 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.