简体   繁体   English

Laravel,如何从另一个控制器调用函数

[英]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:我在名为 "UserController" 的控制器中有一个带有 "getUsers" 函数的控制器,在其中我想调用名为 "getCars" 的 "CarController" 控制器的函数,我有两个选项:

a) Make the second call as "static" , then I can call it without instantiating the class a) 将第二个调用设为 "static" ,然后我可以在不实例化类的情况下调用它

b) Do not do that function of the static class and I call it in this way b)不要做静态类的那个功能,我这样称呼它

    $ 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.我正在使用 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.正如@elfu 所提到的,这无论如何都不是控制器的预期功能。 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.他的帖子是正确的,在您的情况下,您可能应该使用 User 模型作为此方法的位置,但我想我会稍微了解一下。

If you do want to share methods between multiple controllers, a good place to do this is through a Trait.如果您确实想在多个控制器之间共享方法,那么最好通过 Trait 来实现。 In some cases, you are not referencing a model that is shared between controllers, and a Trait would be your best option.在某些情况下,您没有引用在控制器之间共享的模型,而 Trait 将是您的最佳选择。

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.要包含特征,您可以通过将其包含在控制器顶部来引用它,然后在控制器的类声明之后使用“use”语句。 Here is an example:下面是一个例子:

use App\Traits\ExampleTrait;

class CarController extends Controller
{
    use ExampleTrait;
...

You would do the same in the UserController.你会在 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.然后,您放置在 ExampleTrait 中的任何方法都可以通过将其引用为 $this->methodName() 来直接从 CarController 和 UserController 访问,就像引用同一控制器中的任何其他方法一样。

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.在您的特定情况下,我会说您的逻辑可能应该存储在 User 模型中,因为用户的汽车实际上是 User 模型的属性,但上面为您提供了另一种选择。

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.所以你应该将你的逻辑移到实体( User.php )并在两个控制器方法中调用它。

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.常规控制器返回一个视图(至少这是预期的),因此如果您想调用另一个控制器,您应该将该路由发送到该方法(在web.php文件中),而不是在另一个控制器中调用它。

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(){} }您可以从另一个调用一个控制器函数,但最好的方法是创建一个trait并将其用于两个控制器,例如: 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也可以在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,
    ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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