简体   繁体   English

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

[英]Call Controller Function from Laravel Routes.php

I have a function in my Orders_Controller in Laravel called getOrders($user).我在Orders_Controller中有一个名为 getOrders($user) 的函数。 It calls a raw DB query and builds a JSON array to return.它调用原始数据库查询并构建要返回的 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 ...");
    }
}

As suggested from larauser you could move your myOrders() method to a model and call it statically.根据 larauser 的建议,您可以将 myOrders() 方法移动到模型并静态调用它。 Here's an example这是一个例子

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.希望有帮助。

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 类的文档。

the myOrders function should probably go in your orders model since its working with data access. myOrders 函数可能应该进入您的订单模型,因为它使用数据访问。 Then, to call it you'd do Orders::myOrders($user).然后,要调用它,您需要执行 Orders::myOrders($user)。

Easier way is call controller as class更简单的方法是将控制器作为类调用

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

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

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

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