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

When I use 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 index.php. I think it looks ugly. There is an easy way to remove index.php from it.

Obviously, I know what str_replace() can do, but I thought there might be a more elegant way?

+4
source share
4 answers

Kohana (as well as CodeIgniter and most other frameworks) relies on the Front-Controller Pattern ( index.php ), so if you hack it deeply, I don’t see how you do not need to rely on it.

After a quick look at the source of 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); } // ... } 

I do not think this is possible without specifying an absolute URL. Perhaps this is the solution if you don't mind:

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

Otherwise, str_replace() or overriding it with the application helper would be the best approach .


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

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

For this:

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

All index.php messages should disappear.


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

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

For this:

 Kohana::init(array('base_url' => '/kohana/', 'index_file' => '')); 
+6
source

for Kohana3, it was executed almost the same way as in Kohana2.x:

in the application /bootstrap.php is an initialization call:

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

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

Please note that you will need to use the .htaccess file

+7
source

I have not played with Kohana 3, except for a few minutes.

Kohana 2 has a configuration setting that you can set to an empty string

 $config['index_page'] = ''; 

One of my employees is on the Kohana 3 development team, so if you don’t have a clear answer for tomorrow, I can ask him. A quick look at form.php shows that the NULL value for the 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 simply by tracing through the Routing instance to find out what happens where. Otherwise, as I mentioned, I will ask my colleague tomorrow.

+1
source

In addition to Casper's answer, this is the default KO3 file .htaccess (renamed from example.htaccess), which allows you to rewrite the URL.

 # 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] 
0
source

All Articles