Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Path to Ajax request

Started by Dr Zoidberg 8 years ago · 2 replies · 995 views
8 years ago

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?

8 years ago

public function onPagesInitialized()
{
/* @var Uri $uri /
$uri = $this->grav['uri'];
$this->current_route = $uri->path();

PHP
    if ('/my_ajax_route' === $this->current_route) {
         return json_encode($something);
    }       
}
8 years ago

My mistake, i forgot to set the $this->callback to my plugin config.

the onPagesInitialized function:

PHP
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();
    }
}

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 47 1 week ago
Plugins · by Xavier, 4 weeks ago
2 56 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1182 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 50 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 74 2 months ago