Im creating a plugin and using the star-rating plugin as a base. But i dont know how to make a route to my ajax requests. Example: on star rating they have this line:
‘uri’ => Uri::addNonce($this->grav[‘base_url’] . $this->config->get(‘plugins.star-ratings.callback’) . ‘.json’,‘star-ratings’)
who return something like localhost/star-ratings.json and the javascript make a post on this url and works fine, but when i change to my plugin name doesnt work, ex: localhost/plugin-name.json do i have to create this url in some file?
public function onPagesInitialized()
{
/** @var Uri $uri */
$uri = $this->grav['uri'];
$this->current_route = $uri->path();
if ('/my_ajax_route' === $this->current_route) {
return json_encode($something);
}
}
My mistake, i forgot to set the $this->callback to my plugin config.
the onPagesInitialized function:
public function onPageInitialized() {
// initialize with page settings (post-cache)
$this->initSettings($this->grav['page']);
// Process vote if required
if ($this->callback === $this->grav['uri']->path()) {
// try to add the vote
$result = $this->addVote();
echo json_encode(['status' => $result[0], 'message' => $result[1]]);
exit();
}
}