2014-04-27 45 views
0

我正在使用Zend Framework並創建基於配置文件的網站。當我想創建個人資料網址時,我被卡住了。要求是這樣的:在Zend Framework中創建根URL 2

http[:]//abc.com/myprofileurl 

我是新的Zend和我只知道,在Zend框架,URL規則是

http[:]//domain.com/controller/action/params 

但我需要在控制器的地方,把配置文件名稱。

所以,大家請幫我解決這個問題。我已經花了4個小時在互聯網上尋找解決方案,但找不到任何東西。

由於提前

+1

請閱讀我的快速入門重寫:[第05章 - 理解路由](https:// github。com/manuakasam/ZF2_3_QuickStart_Rewrite/pull/12/files) – Sam

回答

0

我通過試驗我的自我得到了解決方案。

我已經創建了一個名爲「用戶」模塊,然後創建了一個名爲「檔案」控制器和module.config.php用戶模塊中添加一個路線如下:

'router' => array(
    'routes' => array(

     // other routes configs here 

     'profile' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/:username', 
       'constraints' => array(
        'username' => '[\w-.]+', 
       ), 
       'defaults' => array(
        '__NAMESPACE__' => 'User\Controller', 
        'controller' => 'Profile', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
      ), 
     ), 
    ), 
), 

這個工程到底是什麼我想要:P

http[:]//mydomain.com/myusername 

謝謝你們!

0

我建議你到一些判別值添加到您的網址,以避免碰撞規則。 在這個例子中,我將顯示所有'用戶'相關的網址都在前綴'用戶'之下,所以我們可以有用戶/登錄,用戶/註銷,用戶/註冊,用戶/更改密碼等(btw同樣的邏輯用於zfcuser模塊)。

我們將製作一個像/ user/profile /:user這樣的網址,其中:user部分是網址的動態部分。

我假設你正在使用的應用模塊上,並且您已經創建並註冊了一個UserController的(如果名稱不同,下面的代碼需要改變)

'router' => array(
    'routes' => array(

     // other routes configs here 

     'user' => array(
      'type' => 'Literal', 
      'priority' => 1000, 
      'options' => array(
       'route' => '/user', 
       'defaults' => array(
        '__NAMESPACE__' => 'Application\Controller', 
        'controller' => 'User', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'profile' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/profile/:user', 
         'defaults' => array(
          'action'  => 'profile', 
         ), 
        ), 
       ), 
      ), 
     ), 

使用這條路線配置您的應用程序將UserController的迴應::當url/user被調用時的索引。 當/ user/profile/someuser被調用時,將使用UserController :: profile進行響應。

請注意,someuser部分是強制性的,因爲我們在規則中這樣說。爲了使它不是強制性的,我們應該這樣寫規則遵循

'route' => '/profile/[:user]', 

在UserController中的配置文件操作中可以採取:用戶價值一樣遵循

$this->params()->fromRoute('user') 

這僅僅是一個解決方案,您可以之一採用,比如你可以這樣寫沒有子路由您的規則,就像跟着

'user' => array(
     'type' => 'Segment', 
      'options' => array(
       'route' => '/user/profile/:user', 
       'defaults' => array(
        '__NAMESPACE__' => 'Application\Controller', 
        'controller' => 'User', 
        'action'  => 'profile', 
      ), 
     ), 
    ), 

它是由你來選擇適合您需求的解決方案。 我建議你也閱讀ZF2 official routing documentation

+0

謝謝@Sergio,我知道這個規則,但客戶端需要在域的根目錄中提供用戶名。 –

+0

這將只是一個取消'/用戶/資料'部分的問題,並根據您的用戶的用戶名添加costraint ... –