Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Kohana 3, how can I tell the form helper to stop inserting 'index.php'

Tags:

php

kohana

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?

like image 868
alex Avatar asked Dec 09 '22 17:12

alex


2 Answers

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

like image 177
SpadXIII Avatar answered May 14 '23 06:05

SpadXIII


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' => ''));
like image 40
Alix Axel Avatar answered May 14 '23 07:05

Alix Axel