Zend Framework 2 Json,默认路由

时间:2022-10-09 23:35:55

I am building up a user creation page (controller/module: User) which has UI controls (DOJO filterselectbox, username, etc.). The UI controls get populated with a Json service deployed as module (module name/controller) myService, and action populatelist().

我正在建立一个用户创建页面(控制器/模块:用户),它有UI控件(DOJO filterselectbox,用户名等)。 UI控件将使用部署为模块(模块名称/控制器)myService和操作populatelist()的Json服务进行填充。

populatelist returns data as Json to client and the client dojo ui elements use that as a memory store.

populatelist将数据作为Json返回给客户端,客户端dojo ui元素将其用作内存存储。

I have 2 modules, User and myService. For the User module, I have setup default page as register and in register.phtml as given below. I have added logic for user input validation and data post.

我有2个模块,User和myService。对于用户模块,我将默认页面设置为寄存器,并在register.phtml中设置如下。我添加了用户输入验证和数据发布的逻辑。

module.config.php of module: User

module.config.php模块:用户

'defaults' => array(
                   'controller' => 'User\Controller\User',
                   'action'     => 'register',
                   ),

Json is registered in module myService. register.phtml makes a call like:

Json在模块myService中注册。 register.phtml发出如下调用:

myservice = new dojo.rpc.JsonService("myService/populatelist");
var dojoDeferredObject=myservice.getCategoryList();

//comment: getCtegoryList is actual method of remote object which returns the json data

When I open the url as http://localhost/user, any reference to myService JSONRPC call works perfectly fine: it parses the JSON call as http://localhost/myService/populatelist and I get the data I need.

当我以http:// localhost / user打开url时,对myService JSONRPC调用的任何引用都可以正常工作:它将JSON调用解析为http:// localhost / myService / populatelist,并获得我需要的数据。

When I access the url as http://localhost/user/register, things fail with 404 page not found exception for every Json RPC call. Reason is, the RPC call is going on a non-existent path, i.e. http://localhost/user/myService/populatelist instead of http://localhost/myService/populatelist.

当我以http:// localhost / user / register访问url时,每次Json RPC调用都会发生404页面找不到异常。原因是,RPC调用是在不存在的路径上进行的,即http:// localhost / user / myService / populatelist而不是http:// localhost / myService / populatelist。

Somewhere I have missed a configuration which is resulting in this issue. I do not want to hardcode path of Json service Module myService.

在某个地方,我错过了导致此问题的配置。我不想硬编码Json服务模块myService的路径。

I believe the problem is this line:

我相信问题就在这一行:

$server->setTarget('myService/populatelist'); 

in the below code, used to set up the Json Service. This is adding up to the path which does not exist. But I am not sure how can I control it as I want a separate module for Json service.

在下面的代码中,用于设置Json服务。这加起来是不存在的路径。但我不知道如何控制它,因为我想要一个单独的模块用于Json服务。

$class = "MOCAPI\Model\MOCGuest";
        $server = new Server();
        $server->setClass($class);
        //echo $_SERVER['REQUEST_METHOD'];
        if ('GET' == $_SERVER['REQUEST_METHOD']) {
            $server->setTarget('myService/populatelist')
                   ->setEnvelope(Smd::ENV_JSONRPC_2);
            $smd = $server->getServiceMap();
            // Set Dojo compatibility:
            $smd->setDojoCompatible(true);
            header('Content-Type: application/json');
            echo $smd;
            return $this->getResponse();
        } else {
            //$server->handle();   
        }

1 个解决方案

#1


0  

You should use routes and url() helper to build urls and relative and absolutes paths, instead of raw 'myService/populatelist'.

您应该使用routes和url()帮助程序来构建URL和相对和绝对路径,而不是原始的'myService / populatelist'。

Check the docs at https://framework.zend.com/manual/2.4/en/modules/zend.view.helpers.url.html (version 2.4, but it almost the same in zf2.* and zf3).

查看https://framework.zend.com/manual/2.4/en/modules/zend.view.helpers.url.html(版本2.4,但在zf2。*和zf3中几乎相同)的文档。

#1


0  

You should use routes and url() helper to build urls and relative and absolutes paths, instead of raw 'myService/populatelist'.

您应该使用routes和url()帮助程序来构建URL和相对和绝对路径,而不是原始的'myService / populatelist'。

Check the docs at https://framework.zend.com/manual/2.4/en/modules/zend.view.helpers.url.html (version 2.4, but it almost the same in zf2.* and zf3).

查看https://framework.zend.com/manual/2.4/en/modules/zend.view.helpers.url.html(版本2.4,但在zf2。*和zf3中几乎相同)的文档。