如何在控制器中的symfony2中包含一个函数文件

时间:2017-03-24 12:04:50

标签: php symfony

您好我有一个名为payment.php的文件,它包含一些与付款相关的功能和一些多个classess。所以我想在我的symfony2 Controller中包含该文件以访问其所有方法和无类别。我正在尝试如下:

//File location is in src/AppBundle/Controller/payment.php

namespace AppBundle\Controller;

require_once __DIR__.'/./payment.php';

//My controller
class ApiServicesController extends Controller
{
   $this->payment(array('txnId'=>1112548));
}

但我无法访问该文件及其方法。

我正在使用这种方法,因为它保存在/ vendor目录中,它也无法访问,因为此文件在同一文件中包含多个无类别。

请告诉我如何在控制器中访问此文件。

提前致谢

2 个答案:

答案 0 :(得分:0)

您必须将Payment类作为服务,然后您可以在控制器中使用Payment类的所有功能。请参阅此文档。 http://symfony.com/doc/current/service_container.html

答案 1 :(得分:0)

如果paymant.php有类,你需要让该类的实例从中调用方法,这就像基本的OOP一样。

class ApiServicesController extends Controller
{
   public function indexAction()
   {
       $somePaymentClass = new SomePaymantClass(); //whatever class you want from payment.php
       $somePaymentClass->payment(array('txnId'=>1112548));
   }
}

首先,您想要调用方法的控制器中的方法在哪里?那么,如果来自不同的类,为什么要在 $ this 上调用 payment 。它应该是这样的

\

但我强烈建议将其用作服务并将其放入某个自动加载器命名空间。