Getting block action url in controller

Permalink 1 user found helpful
In the view you can call $this->action('save') to get an url to run the blocks function action_save() in the controller.

The problem is getting this url in the controller. Anyone knows?

As for now I have to tweak things and let the view pass itself to the code that needs the action function.

itrio
 
jordanlev replied on at Permalink Reply
jordanlev
Try this:
$bv = new BlockView();
$bv->setBlockObject($this->getBlockObject());
$url = $bv->action('save');


(I'm pretty sure that will work, although I haven't tested it so not 100% -- let me know please).
itrio replied on at Permalink Reply
itrio
Thanks for you answer. I tried your code in a block controller, but line 2 eats up all memory allowed for PHP.

Right now I let the view give itself to the model to have access to View->action(). That means that I can't use the model until I am in the view.
jereme replied on at Permalink Reply
jereme
No dice here because BlockView's action() function is contingent upon upon the $bv instance having the private block variable set, but setBlockObject() only sets $bv->blockObj.

It's a weird situation and I'm having a heck of a time working around it.
itrio replied on at Permalink Reply
itrio
This is an old thread now, but i bring it up again because I see this as a big problem in Concrete5 when trying to work with blocks.

The controller should only be a controller, directing what operations should be executed. The view should get what should be the output. I find myself using views both as controllers and models, because I need action url's, and they can only be retrieved in the view ($this->action('post_form'))

This ruins the MVC structure and makes the complexity of the code high. It is really unnecessary! :) Anyone in the Concrete5 team should give a comment on this.

Right now I am working on a block that needs a pagination like "< << 1 2 3 4 5 >> >" to move between pages of information. This pagination needs urls, so generating the paginator must be done in the view, because that is the only place I can get urls for the block. That means that all information about the pageination must be sendt the view, and the pagination generated there.

You see the complexity? All this should be done in a model called from the controller.
Mnkras replied on at Permalink Reply
Mnkras
try View::action('blah');
jordanlev replied on at Permalink Reply
jordanlev
I agree that the current setup is messy and breaks MVC. Unfortunately I don't know of a solution for you, but just wanted to pipe in and let you know you're not crazy.
savan replied on at Permalink Best Answer Reply
Hi

May be i am late to it, but as i got the same problem and may be this can be helpfull to you , inside controller function i have got the url this way ,
global $b;
$url = $b->getBlockPassThruAction(). '&method=your_action_here';
so if your action name is "login" then the "your_action_here" will be replaced with "login"

Try this if this can help you and if i am not late
programmieraffe replied on at Permalink Reply
programmieraffe
Nice one, works for me :)
mlocati replied on at Permalink Reply
mlocati
Just found this page. Here's my solution to retrieve a block action URL from inside the view() method:
class MyBlockController extends BlockController {
   public function view() {
      $actionURL = $this->block->getBlockPassThruAction() . '&amp;method=methodname';
   }
}


Please note that $actionURL will be URL encoded, so if you want to use it in (for instance) ajax calls, you have to:
$actionURL = str_replace('&amp;', '&', $actionURL);
everden replied on at Permalink Reply
Easy Solution in version 8 from within the controller
$this->getActionURL('my_action_nm')