How to pass data from Kohana controller to JQuery?

87 Views Asked by At

I'm new to Kohana and I find it hard to search for some reference. I need to use the 'fullcalendar' library for my current project. I have to pass events data from my controller to my js file.

For example, I have this code in my controller:

/modules/calendar/classes/controller/calendar.php

$data[] = array(
    'id' => '0',
    'title' => 'Event 1',
    'start' => '2020-02-15',
    'end' => '2020-02-15'
);

echo json_encode($data);

I want to pass this $data to here:

/js/admin/fullcalendar.js

$('#prog-calendar').fullCalendar({
    events: "url-of-the-php-controller-or-template-file-I'm-not-sure"
});

I don't know how to link my php script to js. I thought of using AJAX but I also need a url to send the request to and I just really don't know how to do it.

1

There are 1 best solutions below

2
WinterSilence On

https://github.com/WinterSilence/kohana-cms/blob/3.3/develop/common/classes/Controller/CMS/Ajax.php as example of backend, at frontend you send AJAX request to controller extended Controller_CMS_Ajax by route https://github.com/WinterSilence/kohana-cms/blob/be7fae88b2e1d0c9febb49c6aba8bac084842f31/bootstrap.php#L134 for example:

$.post(
    'ajax/data/cities/json',
    {country: 'Russia'},
    function (response) {
        console.log(response.cities);
    }
);

P.s. use route->uri() to create url for AJAX query