Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
1.7k views
in Q2A Core by
edited by

I am trying to override the behaviour of qa_redirect and qa_redirect_raw, only for the case where the source page is the 'register' page. The code below is very similar to the case of the qa_redirect_raw and it works like a charm:

    function qa_redirect($request, $params=null, $rooturl=null, $neaturls=null, $anchor=null){
              require_once QA_INCLUDE_DIR.'qa-base.php';
                $qa_page=qa_request_part(0);
                $to_page=qa_get('to');
                if($qa_page=='register'){
                    if(isset($to_page)){
                        $request='thePageThatIwant';
                        $params=array('to'=>$to_page);
                    }
                    else{
                                            $request='thePageThatIwant';
                                            $params=null;
                    }
                    $rooturl=null;
                    $neaturls=null;
                    $anchor=null;
                    $url=qa_path($request, $params, $rooturl, $neaturls, $anchor);
                    header('Location: '.$url);
                    qa_exit('redirect');
                }
                else {

                    return qa_redirect_base($request, $params=null, $rooturl=null, $neaturls=null, $anchor=null);
                }
        }

 However, I have realized this code breaks the button for the edition of questions (the page is not rendered). I tried to understand what is happening and it is not clear for me (since it should change the behaviour of the function only in the case you are in the register page). I see that if the button "q_doedit" is clicked, the function "qa_page_q_refresh" is called and this function uses "qa_redirect":

qa_redirect(qa_request(), $params, null, null, $anchor);

Since I am not sure what is happening I tried to speculate. Note that the "$request" argument is differently defined by the behaviour of other function "qa_request". The function "qa_request" returns the value of a global variable "$qa_request" that is not clear for me.

Could this be the problem? If yes, How could I avoid this unexpected (for me) behaviour?

Q2A version: 1.7.0

1 Answer

+1 vote
by
selected by
 
Best answer

I applied some 100% mechanical refactorings to your code plus a minor fixes. I haven't tested this, I just formatted and compacted the code and made a minor fix that could be the cause of your issue. This code is equivalent except for the qa_redirect_base line:

function qa_redirect($request, $params = null, $rooturl = null, $neaturls = null, $anchor = null) {
    $qa_page = qa_request_part(0);
    if ($qa_page === 'register') {
        $to_page = qa_get('to');
        $params = isset($to_page) ? array('to' => $to_page) : null;
        $url = qa_path('thePageThatIwant', $params, null, null, null);
        header('Location: ' . $url);
        qa_exit('redirect');
    } else {
        return qa_redirect_base($request, $params, $rooturl, $neaturls, $anchor);
    }
}

Notes:
1. Including qa-base.php is never needed
2. When calling a function, parameters are evaluated before the call so this:

return qa_redirect_base($request, $params=null, $rooturl=null, $neaturls=null, $anchor=null); 

Is actually assigning null to each parameter and THEN calling the function so basically you're calling this function like this:

return qa_redirect_base($request, null, null, null, null);

Hence, losing all parameters. So it makes sense that, e.g., in pages that are not the register one and require parameters you might have issues because you're removing all the parameters you receive.

Remember I don't know if my code there is what you expect, but I'm pretty sure yours is not :)

by
Thank you! I am a very beginning in PHP. I am a guy from scientific computation, and algorithms. I had no idea how complex and interesting could be a framework like this built in PHP. You are a fantastic lecturer!
...