How To Hijack A Url (using a module_view_listener)

  • The Example

    For this example of using a module_view_listener we’ll redirect a visitor to a different url when they try to view the jrUser signup form.

    You are not going to need to hijack any urls in Jamroom, probably, but we are going to use it in our example because it is is very brief and easy to understand. We will redirect a visitor to a different url when they try to view the jrUser signup form.

    One way of doing this would be using a module_view_listener.
  • The module_view_listener

    First register the listener with the core in the myModule_init() function.
        jrCore_register_event_listener('jrCore', 'module_view', 'myModule_module_view_listener');
  • Add the nearly naked listener to include.php
    function myModule_module_view_listener($_data, $_user, $_conf, $_args, $event)
    {
    fdebug("If this appears in the log then myModule_module_view_listener is working",$_data, $_user, $_conf, $_args, $event);
    	return $_data;
    }
  • All that’s in there is an fdebug() function which will write all of the input arrays to the debug log when the listener is triggered.

    We need that info to figure out what conditions we can use to make sure our code is run only when we want it to. The module_view listener itself is run each and every module view, but the redirect must happen only on one specific url: '/user/signup'

    Visit that url and in the debug log you will see the output of the fdebug(), and notice that the $_data['_uri'] has our url as its value. For this example that is the only condition we need, so it is relatively simple. You will normally have several conditions in the listener checking module status, permissions, etc.
    function myModule_module_view_listener($_data, $_user, $_conf, $_args, $event)
    {
    	// want to hijack the form success display in jrUser and redirect to a myModule view
    
    
    	if ($_data['_uri'] == '/user/signup'){  
    		jrCore_location($_conf['jrCore_base_url'].'/'.jrCore_get_module_url('myModule').'/contact_us_by_phone');
    	}
    	return $_data;
    }
  • And that’s it. When someone visits /user/signup they will be shown the module view view_myModule_contact_us_by_phone which would be in myModule’s index.php

    You can redirect to a skin template view as well, the following would display the contact_us_by_phone.tpl which would be in the skin directory.
    jrCore_location($_conf['jrCore_base_url'].'/contact_us_by_phone');

Tags