Laravel , how to call a function from another controller

混江龙づ霸主 提交于 2020-01-16 19:41:37

问题


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.


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



回答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.




回答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.




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/53209299/laravel-how-to-call-a-function-from-another-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!