Overriding a View Function

TiG
TiG
@tig
6 years ago
184 posts
Is there a way to redirect a view url so that another module can handle the view details? Basically overriding the implementation of the view? For example, I would like to redirect something like:

{$jamroom_url}/blog/update/id=147   to  {$jamroom_url}/article/update-blog/id=147

The 'article' module (in this example) would fully implement the update UX and the rest of the system can remain the same. That is, all the client requests (current and future) in the code to blog/update can remain intact.

__________________

( A 'module_view' listener looked very promising but it seems there is no way to effect a change in the url. )
( This is a piece of a broader function, but it is an important piece. )


--
TiG

updated by @tig: 07/13/18 07:53:15AM
michael
@michael
6 years ago
7,715 posts
I made this diagram of override points a long time ago, there are probably more now, but its a map of router.php

https://upload.wikimedia.org/wikipedia/commons/6/60/A_diagram_of_the_routing_system_used_in_Jamroom_5.png

There are lots of override points.
TiG
TiG
@tig
6 years ago
184 posts
Michael

I have used your diagram before, very helpful. Per your diagram, it makes sense for me (in this case) to establish a 'module view' listener and use same to grab control of the URL. As I noted, I already tried that and saw no way to influence the routing through the listener. Changing the module, option, etc. has no effect - the $_data changes are ignored by the router code. I was hoping there might be some other trick to use in this listener to influence the routing.

Per your flow, if a url has a view we hit a terminal node. That is, if there is a specific view for a URL the only chance for overriding was the 'module view' listener. After that we fall into the catch-all.

Tomorrow I will investigate the catch-all of view-results. That seems to be the only option left.

Thanks,
TiG


--
TiG
michael
@michael
6 years ago
7,715 posts
There are many ways to do things, what I normally do when trying to figure something out is use a server with xdebug on it and put a breakpoint where I want to be, then look back up to see what its run over.

If there's nothing in the chart of override points that works for you, how about a listener early on in the flow of things that checks if the $_post data matches a specific structure that is desired to be redirected and if it fits the case use jrCore_location() to redirect to where you want to go. It would cause a page refresh, but would probably not be noticed by the user who is waiting for a page refresh at that point anyhow.
brian
@brian
6 years ago
10,148 posts
TiG:
Tomorrow I will investigate the catch-all of view-results. That seems to be the only option left.

the view_results function comes at the very end - AFTER the view has been processed, so that is not going to help you. Instead you need to move further up the routing to the parse_url function - that way you can watch for the module and option and CHANGE then to your module if they match - i.e.

function xxCustom_parse_url_listener($_data, $_user, $_conf, $_args, $event)
{ if (isset($data['module']) && $_data['module'] == 'jrBlog' && isset($_data['option']) && $_data['option'] == 'update') { $_data['module'] = 'xxCustom'; $_data['option'] = 'update-blog'; } return $_data; }

The parse_url event is where Jamroom parses out the URL to see what needs to be run - so you can swap things around in there if you need to.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net

updated by @brian: 04/13/18 06:38:34AM
TiG
TiG
@tig
6 years ago
184 posts
Brian

Well nuts. First thing I did in this process was review the listeners to find logical candidates.
I tried parse_url early on and moved on because it did not seem to work.

Turns out that It works fine. The reason it did not seem to work for me is that the address bar still shows the original uri. It looked as though my redirection changes were ignored so I continued my search for 'the proper' event.

This is the value of being able to tap into JR expertise - the confidence to know when you are on the right path.

I un-commented my old parse_url listener and verified that in spite of the address bar showing the original uri, the redirection actually was working.

Thanks very much for your help!
TiG


--
TiG
brian
@brian
6 years ago
10,148 posts
If you want the URL to change, simple use a jrCore_location call instead - i.e.

function xxCustom_parse_url_listener($_data, $_user, $_conf, $_args, $event) {
    if (isset($data['module']) && $_data['module'] == 'jrBlog' && isset($_data['option']) && $_data['option'] == 'update') {
        $url = jrCore_get_module_url('xxCustom');
        jrCore_location("{$_conf['jrCore_base_url']}/{$url}/update-blog");
    }
    return $_data;
}

That will redirect the user to the URL passed into jrCore_location.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
TiG
TiG
@tig
6 years ago
184 posts
Brian

I will probably make that cosmetic change to avoid potential confusion.

Thanks,
TiG


--
TiG

Tags