tags:

views:

204

answers:

4

When I use the form::open in Kohana 3, I get this

<form action="/my-site/index.php/bla" method="post" accept-charset="utf-8"> 

Nowhere on my site do I rely on the index.php being there. I think it looks ugly. Is there an easy way to remove the index.php from it.

Obviously I know I could do a str_replace(), but I thought there may be a more elegant way?

+2  A: 

Kohana (as well as CodeIgniter and most of other frameworks) relies on the Front-Controller Pattern (index.php) so unless you deeply hacked it I cannot see how you don't need to rely on it.

After a quick look at the form::open() source:

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);
    }

    // ...
}

I don't think it's possible without specifying a absolute URL. Might be a solution if you don't mind doing:

form::open('http://domain.com/my-site/bla');

Otherwise your best approach would be to str_replace() or override the it with an application helper.


If you edit the url helper (/system/classes/kohana/url.php) and change line 71 from this:

return URL::base(TRUE, $protocol).$path.$query.$fragment;

To this:

return URL::base(FALSE, $protocol).$path.$query.$fragment;

All index.php appearances should be gone.


I'm not sure if this will work, but in application/bootstrap.php change this:

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

To this:

Kohana::init(array('base_url' => '/kohana/', 'index_file' => ''));
Alix Axel
Whilst it relies on it, I use .htaccess to send everything to index.php silently. Therefore all my URLs are /about/, /company/ etc. I'd like to maintain that consistency.
alex
@alex: Check my edit. =)
Alix Axel
Hey thanks for your answer. I don't want to edit any system files - in 2.3 you could do it somehow!
alex
@Alix: you are quoting codes from Kohana 2, while OP is asking about Kohana 3. The two versions have lots of major differences.
Lukman
@Lukman: Sorry for that, I'll update my answer.
Alix Axel
@alex: Try my last edit.
Alix Axel
Thank you very much!
alex
+1  A: 

I haven't played with Kohana 3 except for a few minutes.

In Kohana 2 there is a config setting you can set to empty string

$config['index_page'] = '';

One of my co-workers is on the Kohana 3 development team so if you don't have a solid answer for this by tomorrow I can ask him. A quick look at form.php shows that the NULL value for action will get the value from Request::instance()->uri(), which in turn will get its values from the Route class. You could probably find the answer by just tracing back through the Routing instantiation to see what is getting set where. Otherwise, like I mentioned, I will ask my coworker tomorrow.

sberry2A
I got it figured out, but many thanks for looking into it for me.
alex
+3  A: 

for Kohana3 it's done almost the same way as in Kohana2.x:

in application/bootstrap.php is an initialization call:

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

This removes the index.php from all generated urls. No need to overload/edit any Kohana class.

Note that you'll have to use the .htaccess file

Caspar
A: 

In addition to Casper's response, here is the default KO3 .htaccess (renamed from example.htaccess) file that enables url rewriting.

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /kohana/

# 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]
John Himmelman