简体   繁体   中英

Pass url from current page to controller in cakephp

I tried to pass url from current page to controller but it dosen't show me full url.

EX, http://xxxxxx.localhost/cards/card_list/page:2 when i echo it in controller it shows xxxxxx.localhost . I knew because of the special character such as ("/" ":" ) ...etc.

My project purpose is pass url with the form to controller after controller finished the work, I will redirect it to the current page.

card_list (View)

<?php echo $url5 = urlencode(Router::url($this->here, true)); ?>
<?php echo $form->create('CardSaleAll', array('url'=> array('controller' => 'cards', 'action' => 'complete_sale_card', $url5)));?>
<input type="hidden" name="data[CardSaleAll][sale_card_id]" value="'+id+'"> 
<?php echo $form->submit('Submit', array('alt' => '売却','type' => 'image', 'src' => '../../img/btn_52.png', 'style' => 'width: 307px;'));?>

cards (controller) => complete_sale_all (action)

function complete_sale_card($url5){
    echo $url5;
    exit();
}

Give the following a try

$this->request->url

The above should contain the URL as part of the current request for a view.

Use a greedy-star route

Normally a forward slash denotes a parameter delimiter:

Ie the url /controller/action/one/two/three/four maps to the following variables:

function action ($one, $two, $three, $four) {
}

If you want $one to contain everything that follows the action name (in the above example) you can use a greedy-star route:

Router::connect(
    '/anything/**',
    array('controller' => 'cards', 'action' => 'complete_sale_card')
);

Or make life simple

Instead of putting a url in the url, you can just make it a form field:

<?php 
echo $form->create('CardSaleAll', array(
    'action' => 'complete_sale_card'
));
echo $form->input('redirect', array(
    'type'=> 'hidden',
    'value' => $url5
));
?>

Don't mix raw html with the form helper

In the question there is this snippet:

<input type="hidden" name="data[CardSaleAll][sale_card_id]" value="'+id+'"> 

As well as being an error (unless id really is a constant) it's a bad idea to write inputs by hand instead of letting cake generate them for you. If nothing else, because it's very easy to generate invalid markup or allow injection attacks. Instead replace with:

echo $this->Form->input('sale_card_id', array('type' => 'hidden'));

Or similar.

Don't use relative urls

In the question there's also this:

'src' => '../../img/btn_52.png', 'style' => 'width: 307px;'

That's also a bad idea.

This will work for the url http://xxxxxx.localhost/cards/card_list/ (I assume) and fail for the url http://xxxxxx.localhost/cards/card_list (no trailing slash).

Instead:

  1. Use css where appropriate - this is an example of where it is appropriate
  2. if you really want to put a url in your source code, use Router::url() to generate the right absolute url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM