0

我不确定这是否可能,但我需要的是加载默认值controlleraction如果controller没有从 中找到指定的url,那么假设我是否有这个 url:

http://mywebsite.com/john

它必须调用user控制器和selected_user动作,

如果我有网址http://mywebsite.com/pages/profile

它必须调用pages控制器和profile动作,因为两者都已指定并找到

有没有办法做到这一点?

我在用Kohana 3.2

编辑这是我的 htaccess:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /ep/

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

/ep我的目录是否htdocs也在'base_url' => '/ep/',我的目录bootstrap

4

1 回答 1

1

假设 mod_rewriting 已启用并且 .htaccess 文件配置正确。您需要做的就是在引导程序中指定一个新路由,在当前默认路由之后。

例如:

<?php

  Route::set('default', '(<controller>(/<action>(/<stuff>)))', array('stuff' => '.*'))
    ->defaults(array(
        'controller' => 'welcome',
        'action' => 'index',
  ));

  /** Set a new route for the users **/
  Route::set(
    "users", "<name>", array("name" => ".*")
  )->defaults(array(
    'controller' => 'users',
    'action' => 'selected_user'
  ));

  /** Within the selected_user method you can then check the request for the "name" 
    validate the user parameter (parhaps against the db) and then again route the correct
    pages/profile if found

    e.g.
  **/

  $username = $this->request->param('name');
  if ($username == "alexp") {
    /** reroute to the users/profile controller with data **/
  }

?>

编辑:我也忘了提到上述路线将在基本 Uri 之后被调用,因此“http://mysite.com/john”和“http://mysite.com/89s88”也将尝试使用它路线。您可以想象随着时间的推移需要分配的路线,所以最好至少坚持最少的 /controller/action 种类,否则您可能会发现自己在不需要的路线中有一些复杂的正则表达式。

于 2012-09-25T11:11:47.490 回答