2011-03-31 78 views
0

我是Kohana的新手,想知道在application/classes/controller目錄中組織很多文件的最佳方法。Kohana 3:如何在應用程序/類/控制器中路由子目錄

我現在的結構是:

-application 
--classes 
---controller 
----page 
-----test.php 

我想打電話從URL頁面內無需頁面或任意其它任何子目錄的名稱:

www.website.com/test/ 

我的控制器類開始:

<?php defined('SYSPATH') OR die('No direct access allowed.'); 

/** 
* Test 
* 
* @package Test 
* @category Page 
* @author 
* 
*/ 
class Controller_Page_Test extends Controller_Template { 

     // Default 
     public function action_index() { 
      // Template vars 
    } 

} 

我需要做些什麼來避免它拋出的404錯誤?我假設我需要在bootstrap.php中設置一個路徑,但我真的不知道該怎麼做才能讓頁面在子目錄內激活。

在此先感謝。

回答

4

使用directory PARAM在路線:

Route::set('with_dir', 
      'test(/<action>(/<id>))', 
      ) 
    ->defaults(array(
      'directory' => 'page', 
      'controller' => 'test', 
    )); 

您可以使用正則表達式控制器列表。例如,在頁面目錄中有Controller_Test和Controller_Foo。這裏是它的路線:

Route::set('with_dir', 
      '<controller>(/<action>(/<id>))', 
      array(
       'controller' => '(test|foo)', 
      )) 
    ->defaults(array(
      'directory' => 'page', 
      'controller' => 'test', 
    )); 
+0

這使我更加接近,謝謝。 :) 我唯一的問題是如何使目錄名稱可選? 在你的例子中,目錄必須是'page'。 – Das123 2011-03-31 18:43:38

+0

您可以在URI中設置''的值(如''或''),但這樣您的URI將更大,簡單的'/ test /'。此外,在3.1(和PHPv5.3)中,您可以使用[lambda路由](http://kohanahanaworkwork.org/3.1/guide/kohana/routing#lambdacallback-route-logic)以及許多功能來設置路由參數。 – biakaveron 2011-04-01 05:17:39