2017-08-23 145 views
0

我想打開另一個url,讓我們說:'cakephp'中的'localhost/test.html'。如何在cakephp的新選項卡中打開外部URL?

我已經有一個視圖(page.ctp)和控制器(PagesController.php)。在這個頁面上有一個名爲Wireshark的按鈕,每當我點擊這個按鈕,我想去另一個URL但不想重定向到cakePHP服務器中的另一個頁面。

在page.ctp我用這個:

<?php echo $this->Html->link(__('Wireshark'), array('controller' => 'tests','action' => 'wireshark'))?> 

在TestsController:

<?php 
App::uses('AppController', 'Controller'); 


class TestsController extends AppController { 

    public function wireshark() { 

     } 
    } 

在wireshark.ctp:

<?php 
echo "hello" 
?> 

直到現在,當我點擊按鈕,它將我重定向到「http://localhost:9877/tests/wireshark」並顯示「hello」。我認爲它工作正常,但我需要的是去另一個網頁(localhost/test.html)。

我也試圖與這個在同一個文件:

<?php echo $this->Html->link(__('Wireshark'), 'localhost/test.html')?> 

我得到這個,當我使用上面的代碼:

Error: The requested address '/pages/localhost/test.html' was not found on this server.

我完全新的和自我學習CakePHP的。我搜索了一下,但沒有任何一篇文章向我詳細解釋如何做到這一點(可能是因爲我對此很陌生)。

任何人都可以幫助我這個。如果不清楚請向我澄清,我很抱歉。

謝謝

回答

1

前置HTTP://的URL

<a href="http://localhost/test.html" target="_blank"> 
    Wireshark 
</a> 

<!-- or--> 

<?= $this->Html->link(
    __('Wireshark'), 
    'http://localhost/test.html', 
    [ 
     'target' => '_blank', 
    ] 
) ?> 
+0

謝謝它工作:)。我想我沒有使用http://。使用http後,它現在正在工作。謝謝 – Sheenam

0

好吧,讓我來解釋一下。

  1. Wireshark的第一個關鍵字代表到href
  2. 第二個關鍵字調用Tests是代表你的控制器和
  3. 第三關鍵字調用wiresha是控制器的action標籤

希望這對你有幫助。

echo $this->Html->link('Wireshark',array('controller'=>'Tests','action'=>'wireshark'), array('target'=>'_blank'));