We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.

https://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.



47.7k

https://docs.phalcon.io/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"
        ));
    }

}


2.8k

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

https://docs.phalcon.io/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"
       ));
   }

}


2.8k
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?



47.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?

https://docs.phalcon.io/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"
       ));
   }

}

Here is the documentation for Jquery post. https://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 json_encode($return_array); } }

$('#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.



2.8k

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.



2.8k

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 $this->dispatcher->forward(array("controller" => "post", "action" => "save")); ?>

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

https://docs.phalcon.io/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"
       ));
   }

}


47.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 $this->dispatcher->forward(array("controller" => "post", "action" => "save")); ?>

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

https://docs.phalcon.io/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"
       ));
   }

}


2.8k
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 $this->dispatcher->forward(array("controller" => "post", "action" => "save")); ?>

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

https://docs.phalcon.io/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"
       ));
   }

}