php - How to reconstruct a url using cakephp while removing selected query parameters? -


i using cakephp 2.4

i have url e.g. /sent?note=123&test=abc

i want remove note parameter while giving me rest of url back. i.e.

/sent?test=abc 

i have piece of code works query parameters. i find out how improve code works with:

  1. named parameters
  2. passed parameters
  3. hashtag

e.g.

/sent/name1:value1?note=123&test=abc#top 

this code have written far. https://github.com/simkimsia/utilitycomponents/blob/master/controller/component/requestextrashandlercomponent.php#l79

update part iii:

let me illustrate more examples demonstrate mean more generic answer.

the more generic answer should assume no prior knowledge url patterns.

assuming given url

/sent/name1:value1?note=123&test=abc 

i want rid of query parameter note ,

/sent/name1:value1?test=abc 

the more generic solution should work give me url.

another example. time rid of named parameters.

assuming given url again

/sent/name1:value1?note=123&test=abc 

i want rid of name1 , back:

/sent?note=123&test=abc 

once again, more generic solution should able accomplish well.

update part ii:

i looking more generic answer. assuming web app not know url called sent. not know if query parameters contain word note or test. how still accomplish above?

i want able use same code actions. way, can package component reused easily.

update part i:

i understand hashtag not passed php. please ignore that.

clues on how values hashtag:

what using mod_rewrite ?

you can handle urls in other way :

<ifmodule mod_rewrite.c>     rewriteengine on     rewriterule ^/sent/name:(.*)?note=(.*)&test=([az-az])(#(.*))$ /sent/name:$1/note:$2/test:$3$4 </ifmodule> 

i'm not sure regex, may pass variables cakephp in clean way (but haven't tested it, though)

[edit]

but if want work without knowing urls patterns, can use $this->request array : url

action/id:10/test:sample?sothertest=othersample&lorem=ipsum 

i can arguments using in controller :

// in controller's method $arguments= array_merge_recursive($this->request->named,$this->request->query); 

then, $arguments array containing both named , passed params :

array(     'id' => '10',     'test' => 'sample',     'sothertest' => 'othersample',     'lorem' => 'ipsum' ) 

is better ?

[edit 2] if know parameter have rid of, , directly redirect new url, should work:

action/id:10/test:sample?knownparam=value&lorem=ipsum 

or with

action/id:10/knownparam:value?othertest=othersample&lorem=ipsum 

in controller/appcontroller action:

    // name of known param     $knownparam = 'knownparam';     // arguments     $arguments = array_merge_recursive($this->request->named, $this->request->query);     if (key_exists($knownparam, $arguments)) {         // unset in named params:         unset($arguments[$knownparam]);         // creating url:         $url = array(             'admin' => $this->request->params['prefix'],             'plugin' => $this->request->params['plugin'],             'controller' => $this->request->params['controller'],             'action' => $this->request->params['action']         );         // adding args         foreach ($arguments $k => $v) {             $url[$k] = $v;         }         // redirect         $this->redirect($url);     } 

this redirect both urls to

action/id:10/param1:value1/param2:value2 

without "know param"...


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -