Block ajax actions only work from the homepage. Help!

Permalink
I'm pretty new to Concrete5 and developed a block that has a few actions that I call via ajax.

Example:

public function action_getMapperQuestions(){
        header('Content-Type: application/json');
        $db = Loader::db();
        $questions = $db->GetAll("select blah");
         $data = array("questions" => $questions);
         echo json_encode($data);
         exit();
   }


I can now use the path http://mysite.com/getmapperquestions...
The route appears to be automatically setup when the block has been added to the home page and works as expected.

If I then put the block into another page on the site and keep it in the home page it still works fine, however if I remove it from the home page, the route no longer resolves.

Am I missing something obvious? Thanks.

 
WillemAnchor replied on at Permalink Reply
WillemAnchor
d0nkeykong replied on at Permalink Reply
Hi Willem.

Thanks for taking the time to respond.

I have looked at these posts and perhaps making a package is the way to go. I could then at least register some global routes. To be clear, I am calling the action from a js file that is loaded by my block controller so I have no way to use php to call $view-> or $this->.

I would really like to know how the action route is automagically registered globally when the block is on the home page but the route disappears if I remove the block. Is this a bug perhaps?

Example js:

// pathToEndPoint is my action - mysite.com/getmapperquestions
   p.loadJsonQuestions = function(pathToEndpoint) {
      $.ajax({url: pathToEndpoint, type:'POST', dataType: 'json'} )
       .done(function (data) { self.parseJsonQuestions(data); })
       .fail(function (jqXHR, textStatus, errorThrown) { self.dispatchEvent("questionsLoadFailed"); })
       .always(function() { console.log("always"); });
   };
WillemAnchor replied on at Permalink Reply
WillemAnchor
your block does have a view.php file...you can add a bit of javascript there:
pathToEndPoint = <?php echo $this.....?>;
It will be available in your js file.
d0nkeykong replied on at Permalink Reply
Hi again.

The block view loads a html5 canvas app that relies on other js files and I didn't want to have modify it to accept a path from the view.

I have found a solution which I will build upon later when I create a package.
For now, if anyone needs to create routes that map to their controller actions this is what I did:

1. Create the actions in your block controller

2. Open the file /application/bootstrap/app.php - In this file you can register custom routes to controllers or actions

3. Register the route. In my case this was

/**
*   Custom route for getting career mapper questions
**/
Route::register(
    '/getmapperquestions', 
    'Application\Block\CareerMapper\Controller::action_getmapperquestions'
);


As long as the block is installed, this will now work anywhere within your site. I have added the block to a few pages everything appears to work as expected.

Thanks