首页 > 解决方案 > 如何在同一个 Symfony v2.8 项目的不同控制器中调用所需文件中的函数?

问题描述

我有一个用 Symfony v2.8/php 编写的网络应用程序,我试图在三个控制器捆绑文件中使用相同的“require 'file.php'”来验证几个发送表单数据的网页回到三个控制器捆绑文件中的不同控制器功能。

最初,我将验证函数直接放在每个控制器的主类中,效果很好,只是验证检查的复杂性随着表单需要收集更多不同类型的数据而增加,每个都需要新的验证“规则”,所以我想在一个地方集中管理验证功能,以最好地确保功能始终相同。我尝试将这些函数放在一个单独的文件中,在这里我称之为'file.php',并且我将“require'file.php'”语句放在三个控制器文件中的每个文件的底部,就在结束之后主类,但这会导致重新定义编译器错误。接下来,我从使用验证功能的三个控制器的第二个和第三个控制器中删除了“require 'file.php'”,

这导致我尝试使用命名空间和使用语句,但这并没有解决未找到函数名称的问题。

注意,所有三个控制器包文件都使用了验证功能,但只有第一个,实际上可以找到它来使用,这可能是因为包含这些功能的“file.php”文件实际上是控制器 brundle 文件中需要的。

我的第一个 Symfony 控制器包文件看起来像这样:

<?php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
⋮
use Symfony\Component\HttpFoundation\Response;

class CBController extends Controller {
  ⋮
  /**
   * @Route("/CB/join")
   * join page.
   *
   * @return: join page response string.
   */
   public function joinAction() {
     ⋮
     // Validate the posted data, starting with signup_firstname ...

     $isDataValid[] = test_input( $signup_firstname,
                                  filter_input( INPUT_POST, 
                                                'signup_firstname',
                                                FILTER_DEFAULT ),
                                  $signup_firstname_error,
                                  $rules[ 'signup_firstname' ] );
     ⋮
   } // End of joinAction() public function.
   ⋮
} // End of CBController class.

require 'common_code/file.php'; // <== file containing the validation functions.
?>

其他两个控制器包文件看起来与第一个类似,只是它们在尝试使用 test_input 验证函数时出错。

<?php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
⋮
use Symfony\Component\HttpFoundation\Response;

class CBPagesController extends Controller {
  ⋮
  /**
   * @Route("/CBPages/pms/{action}/{page})
   * 
   * @return: string // response
   */
  public function pmsAction( $action, $page ) {
    ⋮
    // Validate the posted data, starting with $firstname ...

    $isDataValid = test_input( $firstname,
                               trim( $_POST[ 'firstname' ] ),
                               $firstname_message,
                               $rules[ 'firstname' ] );
    ⋮
    } // End of pmsAction() public function.
  ⋮
} // End of CBPagesController class.

// require 'common_code/file.php'; // Removed to prevent redefinition error.
?>

'common_code/file.php' 所需文件的一些内容:

<?php
namespace AppBundle\Controller; // Same namespace as the other three controller
                                // bundle files in hopes that this would solve
                                // the problem where test_input wasn't found.

function test_input( &$valueOut, $valueIn, &$error_message, array $rules,
                     $extraInfo = NULL ) {
⋮
}

这是我在运行 pms 页面时遇到的运行时错误:

Attempted to call function "test_input" from namespace "AppBundle\Controller".
500 Internal Server Error - UndefinedFunctionException

Stack Trace

1. in src/AppBundle/Controller/CBPagesController.php at line 166   -

   164.             // $firstname
   165. 
   166.             $isDataValid = test_input( $firstname,
   167.                                        trim( $_POST[ 'firstname' ] ),
   168.                                        $firstname_message,
   169.                                        $rules[ 'firstname' ] ); 
   170.

2. in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 142   + 
⋮

我尝试使用不同的命名空间名称和子命名空间名称,并在控制器捆绑文件中使用它们,但这不起作用。

我在 https://www.php.net/manual/en/language.namespaces.nested.php 阅读了 PHP 手动命名空间定义部分,并在https://www.php.net/manual/en/language使用它们。 namespaces.basics.php,这是我想到将所有文件放在同一个命名空间中的想法可能会有所帮助。

标签: phpnamespacessymfony-2.8

解决方案


推荐阅读