简体   繁体   中英

Laravel , how to call a function from another controller

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.

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]);

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.

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.

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;
}

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]);
}

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

    $videos = app()->call('App\Http\Controllers\StorageController@returnViewVideo',[
        'course'=>$course,
        'lesson'=>$lesson,
    ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM