【问题标题】:Routing in Kohana in development开发中的 Kohana 路由
【发布时间】:2013-11-02 23:52:59
【问题描述】:

我正在使用 Kohana 框架设置我的新 Web 应用程序。

我正在使用 MAMP,因此该应用位于 htdocs 文件夹中,具有以下结构:

---htdocs
 --foo
  -application

查看http://localhost:8888/foo/时出现此错误

Kohana_HTTP_Exception[ 404 ]: The requested URL foo was not found on this server.

bootstrap.php 中的路由是默认的 Kohana 路由

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

【问题讨论】:

  • 您是否正确设置了 bootstrap.php 中的 base_url(以及 .htaccess 中的 RewriteBase)?
  • 你能告诉我这些应该是什么吗?

标签: php routes kohana mamp


【解决方案1】:

检查您的 application/bootstrap.php 文件是否有:

Kohana::init(array(
    'base_url'   => '/foo/',
));

Kohana 需要它来理解它在 /foo/ 文件夹中。

UPD 如果在 Controller 中没有找到 action_&lt;action&gt; 方法,则会生成 The requested URL foo was not found on this server 异常消息。

如果没有找到路由Unable to find a route to match the URI: 会生成异常消息。

不是 shure 路由按预期工作,但它可以工作 ;)。

因此请检查您的控制器文件以获取适当的操作方法。

【讨论】:

    【解决方案2】:

    这将是我在/foo 下的 Kohana 应用程序的首选文件夹结构:

    ├─┬─ htdocs
    │ └─┬─ foo
    │   ├─── .htaccess
    │   └─── index.php
    └─┬─ kohana
      ├─┬─ application
      │ ├─── bootstrap.php
      │ └─── etc...
      ├─┬─ modules
      │ └─── etc...
      ├─┬─ system
      │ └─── etc...
      ├─── index.php
      ├─── install.php
      └─── etc...
    

    htdocs/foo/index.php:

    <?php
    
    require '../../kohana/index.php';
    

    htdocs/foo/.htaccess:

    # Turn on URL rewriting
    RewriteEngine On
    
    # Installation directory
    RewriteBase /foo/
    
    # Protect hidden files from being viewed
    <Files .*>
        Order Deny,Allow
        Deny From All
    </Files>
    
    # 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]
    

    在 kohana/application/bootsrap.php 中将 'base_url' 设置为 foo:

    Kohana::init(array(
            'base_url' => '/foo/',
            'index_file' => NULL,
    ));
    

    如果你想运行 kohana/install.php 创建 htdocs/foo/install.php 并需要 '../../kohana/install.php';

    这样可以保持您的 htdocs 干净。如果您的实时服务器由于某种原因停止处理 PHP 文件,那么人们唯一能看到的是对 Kohana 的 index.php 的要求。

    不需要 applicationmodulessystem 文件夹的 RewriteCond。

    开启维护模式非常简单。

    【讨论】:

    • 不应该application dir 也在foo 里面吗?里面的文件不应该与使用相同 kohana 的其他系统共享,不是吗?
    • 没有。如果您只有一个 Kohana 应用程序,那么这可以正常工作。如果您有多个运行相同的版本,您可以将kohana/application/ 移动到applications/foo。如果可以避免的话,您不希望在 webroot 下使用 application/(或者无论如何命名它)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多