如果没有找到,则加载默认操作和控制器

时间:2012-09-25 10:07:01

标签: kohana kohana-3

我不确定是否可能,但我需要的是加载默认controlleraction如果未在controller中找到指定的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]

/ephtdocs中的目录,我也在我的'base_url' => '/ep/',中设置了bootstrap

1 个答案:

答案 0 :(得分: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之后被召唤出来,所以&#34; http://mysite.com/john"和&#34; http://mysite.com/89s88"也会尝试使用那条路线。你可以想象随着时间的推移需要分配的路线,所以最好至少坚持最少的/控制器/动作变种,否则你可能会在不需要的路线中找到一些复杂的正则表达式。