在Kohana 3中,如何告诉表单助手停止插入'index.php'

ale*_*lex 4 php kohana

当我使用form::openKohana 3时,我得到了这个

<form action="/my-site/index.php/bla" method="post" accept-charset="utf-8"> 
Run Code Online (Sandbox Code Playgroud)

我的网站上没有任何地方依赖index.php.我觉得它看起来很难看.有没有一种简单的方法可以从中删除index.php.

显然我知道我可以做一个str_replace(),但我认为可能有更优雅的方式?

Spa*_*III 7

对于Kohana3,它的工作方式与Kohana2.x完全相同:

在application/bootstrap.php中是一个初始化调用:

Kohana::init(array(
  'base_url'   => '/',
  'index_file' => FALSE // This removes the index.php from urls
));
Run Code Online (Sandbox Code Playgroud)

这将从所有生成的URL中删除index.php.无需重载/编辑任何Kohana课程.

请注意,您必须使用.htaccess文件


Ali*_*xel 6

Kohana(以及CodeIgniter和大多数其他框架)依赖于Front-Controller Pattern(index.php),所以除非你深入攻击它,否则我看不出你不需要依赖它.

快速浏览一下form::open()来源:

public static function open($action = NULL, array $attributes = NULL)
{
    if ($action === NULL)
    {
        // Use the current URI
        $action = Request::instance()->uri;
    }

    if ($action === '')
    {
        // Use only the base URI
        $action = Kohana::$base_url;
    }
    elseif (strpos($action, '://') === FALSE)
    {
        // Make the URI absolute
        $action = URL::site($action);
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

我不认为没有指定绝对URL是可能的.如果你不介意做,可能是一个解决方案:

form::open('http://domain.com/my-site/bla');
Run Code Online (Sandbox Code Playgroud)

否则,您最好的方法是str_replace() 使用应用程序帮助程序或覆盖它.


如果编辑url帮助程序(/system/classes/kohana/url.php)并从中更改第71行:

return URL::base(TRUE, $protocol).$path.$query.$fragment;
Run Code Online (Sandbox Code Playgroud)

对此:

return URL::base(FALSE, $protocol).$path.$query.$fragment;
Run Code Online (Sandbox Code Playgroud)

所有index.php外表都应该消失.


我不确定这是否有效,但application/bootstrap.php改变了这个:

Kohana::init(array('base_url' => '/kohana/'));
Run Code Online (Sandbox Code Playgroud)

对此:

Kohana::init(array('base_url' => '/kohana/', 'index_file' => ''));
Run Code Online (Sandbox Code Playgroud)