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()
.
populatelist
returns data as Json
to client and the client dojo ui elements use that as a memory store.
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.
module.config.php
of module: User
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'register',
),
Json is registered in module myService
. register.phtml makes a call like:
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.
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
.
Somewhere I have missed a configuration which is resulting in this issue. I do not want to hardcode path of Json service Module 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.
$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();
}
You should use routes and url()
helper to build urls and relative and absolutes paths, instead of raw '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).