5

Each controller I need to define how many action can be like saleAction() , productAction() something else.

But I want to make a action who receive all action like ......Action() It's receive all request except indexAction(). Is this possible only edit controller file?

Because now my URL like http://www.example.com/controller/action/parameter I need to convert that http://www.example.com/controller/action_as_parameter/somethings_if_need

UPDATE

Actually I not make yet. But I want to make a simple routers like

<frontend>
        <routers>
            <marketshop>
                <use>standard</use>
                <args>
                    <module>Myname_Mymodule</module>
                    <frontName>market</frontName>
                </args>
            </marketshop>
        </routers>
    </frontend>

And controllers also simple. If I can this way then I make my module or find another way

class Myname_Mymodule_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction(){
        $this->loadLayout();
        $this->renderLayout();
    }
    public function globalAction(){
        // I want all action comes here and doing somethings
        // their not have any 404 page
    }
}
5

you will need a custom router.
Add this inside the global tag in config.xml

<events>
    <controller_front_init_routers>
        <observers>
            <market>
                <class>Myname_Mymodule_Controller_Router</class>
                <method>initControllerRouters</method>
            </market>
        </observers>
    </controller_front_init_routers>
</events>

Then create the class Myname_Mymodule_Controller_Router.

<?php 
class Myname_Mymodule_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
    public function initControllerRouters($observer){
        $front = $observer->getEvent()->getFront();
        $front->addRouter('market', $this);
        return $this;
    }
    public function match(Zend_Controller_Request_Http $request){
        if (!Mage::isInstalled()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
            exit;
        }
        $pathInfo = trim($request->getPathInfo(), '/');
        $params = explode('/', $pathInfo);
        //if module is market and controller is index
        if(isset($params[0]) && $params[0] == 'market' && isset($params[1]) && $params[1] == 'index') {
             //if the action is index - proceed as normal
             if (!isset($params[2]) || $params[2] == 'index') {
                 return false; //standard router will pick it up
             }
             //if action is not index, map the request to the `globalAction`
             if (isset($params[2]) && $params[2] != 'index') {
                 $request->setModuleName('marketshop')  
                     ->setControllerName('index')
                     ->setActionName('global');
                 $request->setAlias(
                     Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                     $pathInfo
                 );
                return true;
            }
        }
        return false;
    }
}
  • There some problem default folder name is controllers but U use controller . I change this then it say Front controller reached 100 router match iterations. And I want when my first params is market then I use any thing in 2n to last params all are calling my global function. – Abdus Salam Mar 20 '16 at 5:59
  • It works I have some mistake but I need to change controller to controllers – Abdus Salam Mar 20 '16 at 6:20
  • Awesome. If I use controllers It's ok for local but when I upload online then need controller. It's I first know today. – Abdus Salam Mar 20 '16 at 11:34
2

I guess you can pass a parameter to your main action method and then use a switch to forward to another action of the same controller, for example:

public function myAction() {
   $param = $this->getRequest()->getParam('param');

   switch ($param) {
        case 'product':
             $this->_forward('product');
             break;
        case 'sale':
             $this->_forward('sale');
             break;
        /* And so on... */
   }
}

public function saleAction() {
}

public function productAction() {
}

But I reckon if you're able to pass a parameter to a main action you can also change the action and access directly the action you need.

  • 1
    how can I call myAction() in my controller ? – Kaka Mar 16 '16 at 8:39
  • 1
    Well it depends on how your module is configured, could you post both your config.xml file and your controller file ? – Raphael at Digital Pianism Mar 16 '16 at 8:41
  • Actually I want to make a simple module for this. Just add some code in my question. – Abdus Salam Mar 16 '16 at 8:51
  • 1
    So right now your indexAction is called like this www.example.com/market/index/index if you want to call the myAction you can call www.example.com/market/index/my – Raphael at Digital Pianism Mar 16 '16 at 8:56
  • But I need to convert url www.example.com/market/index/my to www.example.com/market/mymarket or www.example.com/market/mymarket/products their mymarket need to dynamic. If there have any index or other parameters that's not feel better. – Abdus Salam Mar 16 '16 at 9:06
0

You can override this method in your controller:

public function getActionMethodName($action)
{
    return 'indexAction';
}

public function indexAction()
{
    //action name
    var_dump($this->getRequest()->getActionName());
 }

Then always will go to the index action, where you can use the original action name as a parameter.

then:

http://mysite/mymodule/mycontroller/im-dracula-blablabla

Will work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.