从 Laravel Routes.php 调用控制器函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13674226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:52:37  来源:igfitidea点击:

Call Controller Function from Laravel Routes.php

phppostcontrollerlaravel

提问by jamis0n

I have a function in my Orders_Controllerin Laravel called getOrders($user). It calls a raw DB query and builds a JSON array to return.

Orders_Controller在 Laravel 中有一个名为 getOrders($user)的函数。它调用原始数据库查询并构建要返回的 JSON 数组。

I cannot figure out how to call this function from within my routes file.

我不知道如何从我的路由文件中调用这个函数。

Basically I receive a POST in the routes, insert the new Order, then I want to call getOrders(user) from this Routes function to get all the existing Orders for the given user.

基本上我在路由中收到一个 POST,插入新的订单,然后我想从这个路由函数调用 getOrders(user) 来获取给定用户的所有现有订单。

Can someone help me figure out how to call this function from within Routes.php?

有人能帮我弄清楚如何从 Routes.php 中调用这个函数吗?

Routes.php

路由.php

//Handle a new order POST
Route::post('order', array('do' => function() {
    ...
    ... //HANDLE POST DATA AND INSERT NEW ORDER
    ...

    //GET ALL ORDERS FOR THIS USER FROM ORDER CONTROLLER
    $userOrders = Order_Controller::myOrders($thisUser);
}

order.php (in Controllers folder)

order.php(在 Controllers 文件夹中)

class Order_Controller extends Base_Controller
{
    public function myOrders($user){
        return DB::query("SELECT ...");
    }
}

采纳答案by afarazit

As suggested from larauser you could move your myOrders() method to a model and call it statically. Here's an example

根据 larauser 的建议,您可以将 myOrders() 方法移动到模型并静态调用它。这是一个例子

class Order extends Eloquent {

    public static function myOrders($user)
    {
         // DB call here
    }
}

Then in your route you could do

然后在你的路线中你可以做

Route::post('order/(:num)', function($user)
{
    $userOrders = Order::myOrders($user);

    // Do stuff here
});

I'm guessing you're passing the user ID that's the reason i'm using (:num) in the route.

我猜你正在传递用户 ID,这就是我在路由中使用 (:num) 的原因。

Hope that helps.

希望有帮助。

回答by mattl

There is another way that I have used, maybe incorrectly, but it might work for you.

我使用了另一种方法,可能不正确,但它可能对您有用。

$userOrders = Controller::call('order@myOrders', array($thisUser))->content

For more information and how to use it check out the documentation for the Controller class.

有关更多信息以及如何使用它,请查看Controller 类的文档。

回答by larauser

the myOrders function should probably go in your orders model since its working with data access. Then, to call it you'd do Orders::myOrders($user).

myOrders 函数可能应该进入您的订单模型,因为它使用数据访问。然后,要调用它,您需要执行 Orders::myOrders($user)。

回答by mogetutu

Routes.php

路由.php

//Handle a new order POST
Route::post('orders/(:num)', function($user) {
    ...
    ... //HANDLE POST DATA AND INSERT NEW ORDER
    ...
});

Route::get('orders', function($user) {
   //GET ALL ORDERS FOR THIS USER FROM ORDER MODEL
   $userOrders = Order::myOrders($user)->get();
});

application/models/Order.php

应用程序/模型/Order.php

class Order extends Eloquent
{
    public function myOrders($user_id)
    {
        return Order::where_user_id($user_id);
    }
}

回答by Mohsen

Easier way is call controller as class

更简单的方法是将控制器作为类调用

Route::post( 'post', function() use ($user) {
  return (new OrderController)->myOrders($user);
});