简体   繁体   English

PHP如何将控制器URL调用转换为函数

[英]PHP How to convert a controller url call to a function

I am trying to integrate two pieces of code together. 我正在尝试将两段代码集成在一起。 The existing code already generates a hash code and the function is called with this URL 现有代码已经生成了哈希代码,并且使用此URL调用了该函数

 header(Location:  http://".$_SERVER['HTTP_HOST']."/subfolder/controller.php?document&validate&patient_id=".$pid."&document_id=".$nid."&process=true");

Is there another way to execute this function without doing a header redirect because the header redirect is causing the code to halt processing upon redirect. 是否有另一种方法可以执行此功能而不执行标头重定向,因为标头重定向导致代码在重定向时暂停处理。

In the controller file there is only one line echo Controller::act($_GET); 在控制器文件中,只有一行echo Controller :: act($ _ GET);

I tried to convert it to a function. 我试图将其转换为函数。 I tried. 我试过了。

   include_once controller.php  //class file

  function hash_tag($pid, $nid){

           $filetag = "document&validate&patient_id=".$pid."&document_id=".$nid."&process=true";

      echo Controller::act($filetag);


 }

 hashtag($pid, $nid);

Any help would be greatly appreciated. 任何帮助将不胜感激。 Code for the Controller.class.php file can be seen here https://github.com/juggernautsei/Drag-N-Drop/blob/master/library/classes/Controller.class.php 可在此处查看Controller.class.php文件的代码https://github.com/juggernautsei/Drag-N-Drop/blob/master/library/classes/Controller.class.php

You didn't post your code inside controller.php but considering that your first approach was accessing that code via url parameters, I'll assume that you are executing variables in that code as GET variables (example: $_GET['patient_id'] ). 您没有将代码发布在controller.php但是考虑到您的第一种方法是通过url参数访问该代码,因此我假设您正在执行该代码中的变量作为GET变量(例如: $_GET['patient_id'] )。

If that is the case, now that you are executing that code via include_once you have to change the way you set your variables in controller.php because there is not more $_GET . 如果include_once这样,现在您要通过include_once执行该代码,则您必须更改在controller.php设置变量的方式,因为没有更多的$_GET

You are trying to pass the string from the GET request to the controller. 您正在尝试将字符串从GET请求传递到控制器。 But Contract::act() works on an array ($_GET is a superglobal array). 但是Contract :: act()在数组上起作用($ _GET是超全局数组)。

Build an array inside the function and assign the parameters of the function to it, then pass it to the controller, like so: 在函数内部构建一个数组,并为其分配函数参数,然后将其传递给控制器​​,如下所示:

include_once 'controller.php';

function hash_tag($pid, $nid)
{
    $array = array();

    $array['document'] = 1;
    $array['validate'] = 1;
    $array['patient_id'] = $pid:
    $array['document_id'] = $nid;
    $array['process'] = 1;

    echo Controller::act($array);
} 

hashtag($pid, $nid);

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

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