How do you call an action, without using a route, from within the view?

router

new Router(false);

view

module/views/index/index.volt

route

$router->add("/", array(
        'module'     => 'module',
        'controller' => 'index',
        'action'     => 'index',
));

How would you call an action named "otherAction" set in the index controller from the index.volt using a button or an anchor without adding a new route?

<?php

namespace Multiple\Module\Controllers;

class IndexController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {

    }

    public function otherAction()
    {

    } 
}

In short Id like to dispatch directly from the view itself.

http://stackoverflow.com/questions/28508198/how-do-you-call-an-action-without-using-a-route-from-within-the-view

It would help a bit to know what you are trying to achieve by calling the 'otherAction'. If you are trying to have that action return some information available on the index you can call it when the indexAction is called and set the variable in the controller for use in the view.

If you use the default anchor functionality you would just do something like <?php echo Phalcon\Tag::linkTo(array('index/other', 'Other Action')); ?>

However this would simply result in navigating to that page. You could use dispatcher to show the index view I guess though. Otherwise you can make a call to that action using javascript called on the element click.



19.7k

http://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

<?php

class PostsController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {

    }

    public function saveAction($year, $postTitle)
    {

        // .. store some product and forward the user

        // Forward flow to the index action
        $this->dispatcher->forward(array(
            "controller" => "post",
            "action" => "index"
        ));
    }

}


1.1k

How would you call that save action from the index view without adding a route?

http://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

```php <?php

class PostsController extends \Phalcon\Mvc\Controller {

public function indexAction() {

}

public function saveAction($year, $postTitle) {

   // .. store some product and forward the user

 // Forward flow to the index action
 $this->dispatcher->forward(array(
     "controller" => "post",
     "action" => "index"
 ));

}

} ```



1.1k
edited Feb '15

you can make a call to that action using javascript called on the element click

Would that action be in that IndexController? If so how?



19.7k
edited Feb '15

From the index view:

<?php $this->dispatcher->forward(array("controller" => "post", "action" => "save")); ?>

How would you call that save action from the index view without adding a route?

http://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

```php <?php

class PostsController extends \Phalcon\Mvc\Controller {

public function indexAction() {

}

public function saveAction($year, $postTitle) {

   // .. store some product and forward the user

 // Forward flow to the index action
 $this->dispatcher->forward(array(
   "controller" => "post",
   "action" => "index"
 ));

}

} ```

Here is the documentation for Jquery post. http://api.jquery.com/jquery.post/

Could be something like below. If you needed or wanted to send variables to the other action you could put those in the post url and change

public function otherAction ($var1, $var2) { if ($this->request->isAjax()) { //do some stuff return jsonencode($returnarray); } }

$('#button_name').click(function() {

$.post( "index/other/variable1/variable2", function( data ) { console.log( data); }, "json"); });

Haven't tested this at all but have done similar quite a bit.

edited Feb '15

Why can't you add a new route? This is entirely what a router is for. Any solution other than having a route for the action is going to be a band-aid workaround solution for an artificial problem.

Edit I realize that sounds harsh. I'm simply meaning unless you have a REALLY good reason for not adding a route, you should just add a new route as that is the cleanest, best solution to getting that action method called.



1.1k

Being able to call an action, that doesn't need to redirect to another page, seems useful. And the logical way to do it would be to have all the actions of that page share the same controller. With that in mind the logical next step would be to have some kind of sugar syntax that would infer the current controller and let's you call any action (using dispatcher?) from within the view. I am not asking to have it for all events, that would require js, but at least on buttons and anchors.

Why can't you add a new route? This is entirely what a router is for. Any solution other than having a route for the action is going to be a band-aid workaround solution for an artificial problem.

Edit I realize that sounds harsh. I'm simply meaning unless you have a REALLY good reason for not adding a route, you should just add a new route as that is the cleanest, best solution to getting that action method called.



1.1k

Maybe I wasn't clear enough I meant using an anchor or button.

use cases:

  • link that does something on that page
  • button that does something to the form

From the index view:

```php <?php $this->dispatcher->forward(array("controller" => "post", "action" => "save")); ?> ```

How would you call that save action from the index view without adding a route?

http://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

```php <?php

class PostsController extends \Phalcon\Mvc\Controller {

public function indexAction() {

}

public function saveAction($year, $postTitle) {

   // .. store some product and forward the user

 // Forward flow to the index action
 $this->dispatcher->forward(array(
 "controller" => "post",
 "action" => "index"
 ));

}

} ```



19.7k

What is something?

Maybe I wasn't clear enough I meant using an anchor or button.

use cases:

  • link that does something on that page
  • button that does something to the form

From the index view:

```php <?php $this->dispatcher->forward(array("controller" => "post", "action" => "save")); ?> ```

How would you call that save action from the index view without adding a route?

http://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

```php <?php

class PostsController extends \Phalcon\Mvc\Controller {

public function indexAction() {

}

public function saveAction($year, $postTitle) {

   // .. store some product and forward the user

 // Forward flow to the index action
 $this->dispatcher->forward(array(
 "controller" => "post",
 "action" => "index"
 ));

}

} ```



1.1k
edited Feb '15

An action that doesn't require to navigate to another page but has an impact on the current one. Something like a database modification for example.

What is something?

Maybe I wasn't clear enough I meant using an anchor or button.

use cases:

  • link that does something on that page
  • button that does something to the form

From the index view:

```php <?php $this->dispatcher->forward(array("controller" => "post", "action" => "save")); ?> ```

How would you call that save action from the index view without adding a route?

http://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

```php <?php

class PostsController extends \Phalcon\Mvc\Controller {

public function indexAction() {

}

public function saveAction($year, $postTitle) {

   // .. store some product and forward the user

 // Forward flow to the index action
 $this->dispatcher->forward(array(
 "controller" => "post",
 "action" => "index"
 ));

}

} ```