2

I have a custom module, with an IndexController and an indexAction. This has a route with a fronName set in the config.xml(for example angular-app). On this page I have an angular app, with it's own routing. Everything works fine until I don't refresh the browser. After refresh I get 404.
How can I setup magento's router to handle every request under the angular-app with the IndexController:indexAction.

So what I would like:

  • request: http://domain.com/angular-app -> IndexController:indexAction
  • request: http://domain.com/angular-app/x/y/z -> IndexController:indexAction where x, y, z are dynamic parameters.
1

I am not expert in angular app but as per as my understand,You want to send all angular requests with prefix angular-app in your url.

So,you need to create new custom router. To understand the custom route,Follow the below link.

For your case, using controller_front_init_routers add custom router at system by $front->addRouter('addangularapp',$this); on function initControllerRouters of class ModuleNameSpace_ModuleName_Controller_Router

<!--  define route -->
<frontend>
  <routers>
      <angularapp> <!--  router identifier -->
          <use>standard</use>
          <args>
              <modules>
                  <module>ModuleNameSpace_ModuleName</module>
                  <frontName>angularapp</frontName>
              </modules>
          </args>
      </angularapp>
  </routers>
</frontend>
<global>
  <!-- event for add a router -->
  <events>
      <controller_front_init_routers>
          <observers> 
          <add_angularapp_route>  <!-- observer identifier -->
              <class>ModuleNameSpace_ModuleName_Controller_Router</class>
              <method>initControllerRouters</method>
          </add_angularapp_route> 
          </observers>
      </controller_front_init_routers>
  </events>
</global>

Then at match() validate and modify http request (Zend_Controller_Request_Http). and if request have contain angular-app then it internally set IndexController as controller as ,indexAction as request action and set your module as request module.

$request->setModuleName('YOURROUTE') 
            ->setControllerName('index')
            ->setActionName('index');

Controller code may like:

<?php
class ModuleNameSpace_ModuleName_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
    /**
     * Initialize Controller Router
     *
     * @param Varien_Event_Observer $observer
     */

    public function initControllerRouters($observer){
        $front=$observer->getEvent()->getFront();
        $front->addRouter('addangularapp',$this);

    }


    /* validate and modify the  request
     *  Params Zend_Controller_Request_Htt
     */
    public function match(Zend_Controller_Request_Http $request){

        /* If Magento Magento is not install then 
         * redirect to installer url
         */
        If(!Mage::isInstalled()):
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
                 exit;

        endif;
        $requestPathInfo=trim($request->getPathInfo(),'/');
        Mage::log('aaa'.$requestPathInfo.'StoreId'.Mage::app()->getStore()->getId(), null, 'logfile.log');
        /* check -review-form not exit
         * then immediate return false
         */ 
        if(strpos($requestPathInfo,'angular-app')==false):
            return  false;
        endif;

        /* get productut from url 
            by  substr   
         */
        $producturl=str_replace('angular-app','',$requestPathInfo);

        $condition=new Varien_Object(array('product_url'=>$producturl,
            'continue'=>true));

        Mage::dispatchEvent('angular_app_controller_router_match_before', array(
                'router'    => $this,
                'condition' => $condition
        ));


        if($condition->getRedirectUrl()){
            Mage::app()->getResponse()
                ->setRedirect($condition->getReDirectUrl())
                ->sendResponse();
            $request->setDispatched(true);
            return true;
        }

        if(!$condition->getContinue()){
            return  false;
        }


        $request->setModuleName('YOURROUTE') 
            ->setControllerName('index')
            ->setActionName('index')
        ->setParam('customparam', $requestPathInfo);
        $request->setAlias(
                Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                $requestPathInfo
        );


        return  true;

    }


}

Note: code is not tested.

same concet havec in https://magento.stackexchange.com/a/70619/4564 answer

| improve this answer | |

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.