Kohana can’t find the controllers, routes are wrong

kohanakohana-3

I'm still playing with Kohana, and found another stopper for me 🙂

I've created a new simple controller called compte.php and is located inside /app/classes/controller/compte.php

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Compte extends Controller {

public function action_index()
{
    $this->response->body('hello, world comptes!');
}
} // End Comptes

Why I can't go to this controller with this url?

http://127.0.0.1/~user/kohana/compte/

This url works:

http://127.0.0.1/~user/kohana/

I've edited the routes in the bootstrap.php file but without success, sure I'm missing something …

If I use the default route to point to my controller comptes, I can see it ok, but I want to know how to go directly to this controllers.

 Route::set('compte', '()') ->defaults(array( 'controller' => 'compte', 'action' => 'index', ));

thanks!

EDIT

I'm getting the error 404 Not Found error from Web Server, not from Kohana

EDIT 2

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /~user/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]

Also the file is located at the same level as the examples.htaccess but renamed to .htaccess, in the kohana folder

EDIT 3

I've followed for third time the tutorial linked, the initial problem was with Apache, now the 404 is from Kohana:

HTTP_Exception_404 [ 404 ]: Unable to find a route to match the URI: compte

This is my only route in bootstrap.php

 Route::set('compte', '') ->defaults(array( 'controller' => 'compte', 'action' => 'index', ));

If I enter an URL without any controller, I can see the default compte Controller, but if I use compte or comptes or whatever, always 404 not found …

Best Answer

Regarding edit 3:

Your route only matches an empty URI string. 'compte' or 'comptes' are not empty strings. If you want the route to match an empty URI as well as the URIs 'compte' and 'comptes' then use this:

Route::set('compte', '(compte(s))')
    ->defaults(array(
        'controller' => 'compte',
        'action' => 'index',
    ));

The whole thing is optional so it can match an empty URI. The 's' is optional too once 'compte' is present. Routing is very powerfull once you get to know it, which you can here.

Related Topic