can a tools file use controller methods?

Permalink
Hey folks-

I've created a block that has a couple of files in a tools folder. These tools files are called via ajax with a $.get() command in my view.php file which loads properly. But I was wondering if there is a way to have these files also use some methods I've placed in my controller file.

For instance, in my tools file if a place a line like
$something = $controller->method()


I get a 'Call to a member function get_date_info() on a non-object' error message

I can place the code in the tools file and reference it like this:

$something = $method()


and it works just fine, but I was wondering if there was a way to keep these methods in one place.

In my tools file, I do have the defined('C5_EXEUCTE')... statement and I'm creating the url for the tools file with the following:
$uh = Loader::helper('concrete/urls');
         $btID = $b->getBlockTypeID();
          $bt = BlockType::getByID($btID);
          $base = $uh->getBlockTypeToolsURL($bt);
          $path = $base . '/'.$tool_file;

cartersch
View Replies:
ryan replied on at Permalink Reply
ryan
Two different ways to do this:
1. If you just make ajax calls to controller functions directly, just like you'd do a form post:
<script>
$.get('<?= $this->action('ajax_results');?>');
</script>

Then in your controller you'd have a function named ajax_results, you'd do your json output, then exit at the end of the function so it doesn't continue to render the page.

2. Instantiate your controller in the tools file:
$controller = Loader::controller('/path/to/single_page_controller');


The user search in the dashboard works this way: /concrete/tools/users/search_results.php
cartersch replied on at Permalink Reply
cartersch
Your second solution worked perfectly!

Thanks, Ryan.
guyDesign replied on at Permalink Reply
How did you get the block controller?
Loader::controller('/path/to/block');
Doesn't work for me, I think Loader::controller is for page controllers only
CWSpear replied on at Permalink Reply
CWSpear
For example 1: I believe the function needs to be named action_ajax_results() in this example. It's action_[string_you_passed_to_action]