2012-05-13 7 views
1

まず、Kohanaのドキュメントはひどいものです。人々は「ドキュメントを読む」ようになります。私はドキュメントを読んでいて、それほど意味をなさないと思われます。コードの一部をコピーして貼り付けることはできません。ドキュメンテーションのいくつかのことのために働く。念頭に置いてKohana 3.2 - ルーティングに関する質問

、私はそうのようなルートを持っている:

//(enables the user to view the profile/photos/blog, default is profile) 
Route::set('profile', '<userid>(/<action>)(/)', array(// (/) for trailing slash 
    "userid" => "[a-zA-Z0-9_]+", 
    "action" => "(photos|blog)" 
))->defaults(array(
    'controller' => 'profile', 
    'action' => 'view' 
)) 

これは、ビューにユーザー写真やhttp://example.com/username/blogを閲覧するために取るべきhttp://example.com/usernameを行くために、ユーザーのプロファイルに取られ、http://example.com/username/photos私を可能にしますブログ。

誰かがhttp://example.com/username/something_elseに行く場合<userid>で指定されたユーザーに対しては、アクションviewをデフォルトにしたいが、これを行う方法が見つからないようだ。

私はこのようにそれを行うことができます:

Route::set('profile', '<userid>(/<useraction>)(/)', array(
    "userid" => "[a-zA-Z0-9_]+", 
    "useraction" => "(photos|blog)" 
))->defaults(array(
    'controller' => 'profile', 
    'action' => 'index' 
)) 

を、コントローラで次の操作を行います。

public function action_index(){ 
    $method = $this->request->param('useraction'); 
    if ($method && method_exists($this, "action_{$method}")) { 
     $this->{"action_{$method}"}(); 
    } else if ($method) { 
    // redirect to remove erroneous method from url 
    } else { 
     $this->action_view(); // view profile 
    } 
} 

(。それは__construct()機能で良いかもしれないが、あなたはそれの要点を得ます)

より良い方法がある場合は、私はむしろそれをやりたいと思います(本当にあるはずです)

私は答えは正規表現であるかもしれないが、以下が動作しないと思う:

$profile_functions = "blog|images"; 
//(enables the user to view the images/blog) 
Route::set('profile', '<id>/<action>(/)', array( 
      "id" => "[a-zA-Z0-9_]+", 
      "action" => "($profile_functions)", 
))->defaults(array(
    'controller' => 'profile' 
)); 
Route::set('profile_2', '<id>(<useraction>)', array(
      "id" => "[a-zA-Z0-9_]+", 
      "useraction" => "(?!({$profile_functions}))", 
))->defaults(array(
    'controller' => 'profile', 
    'action'  => 'view' 
)); 

何もIDの後にされていないとき、それは一致しないが。

答えて

1

私はこのようなルートを設定します:コントローラで

Route::set('profile', '<userid>(/<action>)(/)', array(
    "userid" => "[a-zA-Z0-9_]+", 
    "action" => "[a-zA-Z]+" 
))->defaults(array(
    'controller' => 'profile', 
    'action' => 'index' 
)) 

そしてを()メソッドの前に:

if(!in_array($this->request->_action, array('photos', 'blog', 'index')){ 
    $this->request->_action = 'view'; 
} 

それともsomethig似は、ちょうどコントローラでアクションを検証します。.. 。

EDIT:

これも仕事ができる:

if(!is_callable(array($this, 'action_' . $this->request->_action))){ 
    $this->request->_action = 'view'; 
} 
+0

申し訳ありませんがわかりません。何に戻る?配列を使用するのではなく –

+0

の代わりに私を無視してください... 'in_array(" action _ {$ request - > _ action} "、get_class_methods($ this))' –

+0

私がcorretlyを理解していれば、私の編集を見てください。 –

関連する問題