Call Block method directly.

Permalink
Hey everyone,

So here's what I got. I have a block (yes it's enabled) and within the controller I have this:

class MysuperblockBlockController extends BlockController {
// .... typical block stuff
  public function view_layout($id){
    echo "Test!".$id;
    exit;
  }
}


Now, how do I call that from a URL? I need to send in an ID and then have it do the work and spit back "Test! 1" if I send it the param of 1. How do I do that?

I tried:
mydomain.com/Mysuperblock/view_layout/1 and that's not it.

Also the block is in:
/blocks/mysuperblock
It installed fine so I know there's no issues there, just need to learn the finer points of how the dispatcher works.

thanks!

chadsmith729
 
itrio replied on at Permalink Reply
itrio
If your block name is MySuperBlock, you can call a public function with the name action_save in the controller by this URL:

http://mydomain.com/index.php/mysuperblock/save...

This is not very documented on the site, but try to look into the blocks that comes with concrete5 under the directories concrete/blocks, there you will find the best help actually. :)
chadsmith729 replied on at Permalink Reply
chadsmith729
Hmm, still not working I went to:
http://mydomain.com/index.php/mysuperblock/dosomething...

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class MysuperblockBlockController extends BlockController {
   var $pobj;
   protected $btTable = 'btMysuperblock';
   protected $btInterfaceWidth = "600";
   protected $btInterfaceHeight = "465";
   public function getBlockTypeDescription() {
          return t("My super block for doing super things");
   }
   public function getBlockTypeName() {
          return t("Super Block");
   }    
   public function __construct($obj = null) {
          parent::__construct($obj);


What am I missing?
Mnkras replied on at Permalink Reply
Mnkras
$this->action('dosomething')
chadsmith729 replied on at Permalink Reply
chadsmith729
How can I call that directly from the URL? Are you saying I have to load a different page to then have it call the controller?
ScottC replied on at Permalink Reply
ScottC
nah honestly chad this is a bit screwy, your block methods you want to call via $this->action need the action_ appended to the method name, and still for me it has been a bit hit and miss. Here's one that worked for me, (and it did take an hour to track down, luckily no sheets of drywall were ruined in the process

<form method="post" id="save-map-center" action="<?php echo $this->action('update_map_center'); ?>">
//in view
//controller method
public function action_update_map_center(){}


That's it.
chadsmith729 replied on at Permalink Reply
chadsmith729
Thanks Scott,

I guess I wanted a similar CakePHP way of doing this. Something where you could just do: /controller/method/param1/2/3/4/5/6 ect. I just don't think that's possible with the dispatcher that's shipped right now.

Then within your controller you have:
public function mymethod($p1,$p2,$p3){
}


I've opted to go with a different way all together and hopefully will be able to move my application around to use the block.

More like I should say I'm still learning the in's and out's of what makes this framework tick.