2011-07-09 227 views
0

我與Zend的時建了一個網站,我無法修復了以下問題: ,我編輯的網站(不是我的編碼)具有以下鏈接結構:http://www.site.tld/controller/action隱藏控制器名稱Zend框架

但是,我想擺脫鏈接上的控制器名稱,我無法讓它工作。例如,在 「鏈接1」,當我點擊我想http://www.site.tld/Link1

我有以下文件結構

應用

公共
審判
   的.htaccess
    index.php

.htaccess in所述公共文件夾具有以下內容:

RewriteEngine敘述在
的RewriteCond%{REQUEST_FILENAME} -s [OR]
的RewriteCond%{REQUEST_FILENAME} -1- [OR]
的RewriteCond%{REQUEST_FILENAME} -d
重寫規則^ * $ - [NC,L]
重寫規則^ * $的index.php [NC,L]

如果是有幫助的,我也看到,在該指數的控制器使得每個動作使用

$ this - > _ redirect('/ index/action');

我試着重寫.htaccess文件,但徒勞無功。它基本上沒有指向物理索引控制器。請指教。

謝謝!

+0

我試過brady.vitrano給出的解決方案,但它不適用於我,是否有另一種方法來隱藏控制器和操作名稱,並且用戶也不應該能夠編輯URL。 – jjnguy

回答

3

你看錯了地方。這個問題不在您的.haccess文件中,而是Zend Framework如何將URL路由到合適的Controller和Action。你所要求的是覆蓋Zend_Controller_Router的默認路由。就像@Adrian所說的,「路由器是你的朋友」。我將爲您提供一個解決問題的答案,但它帶有一個很大的警告:

以下內容將覆蓋您的默認路由,並阻止您訪問通過以下URL打開的控制器和模塊:example.com/name

將此放在您/application/Bootstrap.php文件:

public function _initCustomRoute() 
{ 
    $router = Zend_Controller_Front::getInstance()->getRouter(); 
    $route = new Zend_Controller_Router_Route(':action', array(
     'module'  => 'default', 
     'controller' => 'index', 
     'action'  => 'index' 
    )); 
    $router->addRoute('default-override', $route); 

} 

注:有測試的方式,如果請求只能在故障被分派和路由到這個位置,但那是另一個話題。

+1

非常感謝。那樣做了。 – Kyprulez

2

路由器是你的朋友,請查看Zend_Controller_Router上的文檔以及如何在引導程序中設置它。用這幾個詞來指導這個設置將是太多了。

更新 - 除了你的問題
不知道你在哪裏,現在怎麼「默認」確認設定。以下是標準默認設置中路由器配置的簡要說明

URL: /home/user 
// translates in a default route setup into 
home = controller - HomeController.php 
user = action  - HomeController::userAction() 
// by default you must have a controller named home and within it an action named user 
// however, you can override this default to something like this 
home = controller - FloridaController.php 
user = action  - FloridaController::miamiAction() 
// if you add the colon in your config you will change the placeholder in the url to a variable 
:user = action  - FloridaController::miamiAction() 
URL: /home/John /home/Mike 
// all urls are routed to FloridaController::miamiAction() 

有多種方法可以設置路由的配置。

+0

你可以請指導我如何覆蓋'默認'的Zend路線,讓我看看':action'。我無法得到它的工作。 – Kyprulez

+0

@Kyprulez在我的回答中看到更新 –