加载默认操作和控制器(如果未找到)



我不确定这是否可能,但我需要的是加载默认controller,如果指定controller尚未从url中找到,则action,所以假设我是否有这个网址:

http://mywebsite.com/john

它必须调用user控制器并selected_user操作,

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

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

有没有办法做到这一点?

我正在使用Kohana 3.2

编辑 这是我的访问:

# 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我也在我的bootstrap中设置了'base_url' => '/ep/',

假设启用了

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 品种,否则您可能会发现自己在不需要的路线中有一些复杂的正则表达式。

相关内容

最新更新