Set routes in Kohana - php

If I defined 2 controllers, A.php and B.php, and in each controller I defined an index action,
how should I define Route::set in the bootstrap.php file?

The default route should do just fine:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
Please read the documentation: http://kohanaframework.org/3.2/guide/kohana/routing
That default route will match both controller '/a' and controller '/b', and execute the index action if no other action is specified in the URI.

I have read the documentation and it's not completely clear when and why you would want to create a Route entry.
Basically, a Route entry is needed if you have a URL that does not conform to the normal /controller/action structure. If you have created controller A and controller B both with index actions, you don't need to add routes to bootstrap.php if you are always going to access those actions using the standard URL syntax:
http://www.example.com/A/index
http://www.example.com/B/index
You could leave off "index" since it is the default action if none is specified.
Let's say you want controller A to be the default site controller, meaning that you don't want to have to use A in the URL, you want to use the action right after the domain:
http://www.example.com/index
Then you do need a route to tell Kohana that any URL that is not matched by any routes you have created (or if you have none) should be handled by your default route. You would create this route in bootstrap.php:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'A',
'action' => 'index'
));
This says that if a user goes to http://www.example.com, Kohana will use the index action of controller A. If the user goes to http://www.example.com/foo, then Kohana will use the foo action of controller A. Any URL that does not match any other controller will go to the A controller. If a user requests an action that A does not handle, he'll get a 404 exception.
You still have the B controller, so that will work fine without any route. If the user goes to http://www.example.com/B/index, Kohana knows about the B controller in the app so it will go to the index action there.

Your problem may be in .htaccess file in kohana folder.
I needed to change the "RewriteBase" to the Kohana folder ('base_url' from Kohana::init in bootstrap.php file), otherwise I landed in '404 - no object found'.
Then the default route should do just fine.

Related

Routing in Kohana 3.3.1

I recently started working with the kohana 3.3.1 framework and ran into some problems.
I want to create different routes for different entry points. Right now, this is the default route, which seems to work fine(I think):
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
When I go to the website, it displays 'hello, world!'.
I have a controller called Street, located in application/classes/Controller/Street.php.
The code to this controller is:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Street extends Controller {
public function action_index()
{
$this->response->body('hello, street!');
}
The problem is, that I want to type /something behind the URI, and it should redirect to the defined controller, and action. But it doesn't seem to work. I get 404 error's when I type ANYTHING behind the default URI.
For routes, i use this
/**
* Set the routes. Each route must have a minimum of a name, a URI and a set of
* defaults for the URI.
*/
Route::set('test1', 'street/<id>')
->defaults(array(
'controller' => 'street',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
Any help would be gladly appreciated.
EDIT
I just tried #Darsstar 's instruction to go to /index.php/street, and it worked!
But now, when I try to go to /index.php/street/derp, I get an error saying
The requested URL derp was not found on this server.
So it's not yet working properly I guess
Since the /index.php/street version works go and read the Clean URLs tutorial if you haven't already. If you have, double check everything!
If you have just those two routes, in that order, /index.php/street/derp should have matched the route 'test1'.
The error message 'The requested URL derp was not found on this server.' says you went to /index.php/derp, not index.php/street/derp. Which would match the default route and be dispatched to Controller_Derp::action_index(), but it doesn't exist so Request_Internal::execute() throws a HTTP_Exception_404.
The default route is more of and example. The is a discussion on Kohana's issue tracker to remove it since a catchall default route is a bad practice. Routes should be specific. I recommend to remove it.
And if you think having a catchall route is a good way to catch all 404 requests, please let the Custom Error Pages tutorial prove you wrong.

Basic Kohana Routing

I'm new with Kohana and finding their documentation to be lacking (lots of incomplete writings, lots of broken links, etc.). I just want to create a route like so:
Route::set('test1', 'blah/<id>')
->defaults(array(
'controller' => 'Blah',
'action' => 'foo',
));
So if the URL is localhost/blah/8342342 it will run through this controller and action. The problem is I get a 404 error. If I change the URI in the Route::set to be blah/foo/<id> it works fine. I only want the /blah/ directory though, not 'blah/foo'. Is this possible or do you need to have both the controller and action in the URL?
Another question, does the first directory in your URI (in this case /blah) HAVE to match the controller name? For example, if the first directory in the URI is "blah/", does that mean my controller must be named "Blah.php"? From my tests it seems that this is the case, but I don't know why it would be set up that way. What if I wanted the URI "contact/" to go through controller Blah?

Kohana 3.2 Routes: Multiple Directories

I have a route:
Route::set('foo/subdir', '<directory>/<variable>/subdir/<controller>/<action>');
I would like to route this url to the following controller/action:
/application/classes/<directory>/subdir/<controller>.php::action_<action>()
I already have and need this route too, which complicates things:
Route::set('foo', '<controller>/<variable>/<action>');
Is that possible?
Why not, as long as the default route is defined after the directory route.
Route::set('foo/subdir', '<directory>/<variable>/subdir/<controller>/<action>')
->defaults(array(
'directory' => 'default_directory',
'controller' => 'index',
'variable' => 'default_variable',
'action' => 'index',
));
Kohanas routing supports directories 'natively', there is no need to hack anything.
Please note your class names will have to include the directory name as well.
I would like to append the subdir to the directory
This will be possible in Kohana v3.3 using the new Route::filter functionality. There is currently no way to do this in Kohana 3.1 or 3.2 without modifying the Route and/or Request classes.
Use REGEXP to catch directory and subdirectory as /directory/subdirectory/controller/action
to match Route like // where regexp allows you to put / inside directory. Then make little modification in your Route class to change all / to the _
It is not tested ... yet. ;) But im about to...

Turn off default routes in Kohana 3?

I believe I know how to do this, but wanted to verify with my awesome community peeps. =)
Here's an example:
I have a Controller class called 'tami', with an action 'index'.
I know that if I want someone to access that controller/action combo via an URL other than "/tami/" or "/tami/index", then I should add a route, via something like this:
Route::set('pretty_tami', 'these-are-my-initials(/<action>)')
->defaults(array(
'controller' => 'tami',
'action' => 'index',
));
But, users can still access this page via /tami/.
How can I turn off the default routing, so that the only valid routes are the ones I define?
I assume I can just remove the default route found in kohana/application/bootstrap.php. Is that correct? Or would that break something else?
I'd say exactly the same as #simshaun — either remove the default route (leaving other controllers unreachable) or check in the before() function in Controller_Tami for the uri to see if it's what you're after.
If you're using Kohana 3.1, you can now use lambda logic/anonymous functions to define your routes.
Something like this would take the extra routing logic out of the controller (which is good as we're keeping it in one place):
Route::set('default', function($uri)
{
if ($uri == 'tami' OR $uri == 'tami/index')
{
// Route not allowed by the default methods
throw new Kohana_404_Exception("Route not permitted");
}
},
'(<controller>(/<action>(/<id>)))'
);
Something I haven't yet used but it looks amazingly powerful.
I think the easiest way would be to remove the default route in your bootstrap file, yes. However, any controllers that you have not manually specified a route for can no longer be accessed.
What I would do is create a class, e.g. Controller_Derouter that Controller_Tami extends. Use the before() method in Controller_Derouter to test if the controller was accessed from the default route, and if so, throw a 404. I think you should be able to do that by comparing $this->request->controller against the first URI segment.
Edit: The solution mentioned above is unnecessary if you ever only plan on disabling the default route for just the Tami controller. If that's the case, you could just implement the before() method directly in the Tami controller.
Maybe like this?
Route::set('pretty_tami', 'these-are-my-initials/<action>')
->defaults(array(
'controller' => 'tami',
));
So there wouldn't be a default action. And you probably want to update the default route (if you still have one) with a regex to exclude tami.
Route::set('default', '(<controller>(/<action>(/<id>)))', array('controller' => '/^(?!tami)/'))
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));

Routing depending on the subdomain

Please help me. I am quite new to kohana. How best to do so the controller was chosen based on the subdomain. For example:
www.site.com -> Controller: siteroot. Method: run
admin.site.com -> Controller: adminsite. Method: run
moderator.site.com -> Controller: moderatorsite. Method: run
director.site.com -> Controller: directorsite. Method: run
default: supervisor: partnersite. Method: run
The run method performs an action for these sub-domains, and will bring a page from the overseers modules.
I use kohana v3.0
I don't think Kohana offers any way to deal with this directly, but you could always add some custom code to your bootstrap.php file that sets up different routes depending on the current subdomain:
switch ($_SERVER['SERVER_NAME'])
{
case 'site.com':
// Default routes.
$controller = 'siteroot';
break;
case 'admin.site.com':
// Admin routes.
$controller = 'adminsite';
break;
// Etc.
}
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => $controller,
'action' => 'run'));
Do you really need a separate domain for each case though? It might be more sensible just to use site.com/admin, site.com/moderator, etc.
I don't think it will work out of box, MatW.
It will be true if it is a subdirectory of the app_path, but if it isn't, it will never be routed to index.php of the folder.
It can be done with htaccess or httpd.ini from apache.
As that subdomain will be mapping to a directory anyway there should be no need to add any custom code at all, this is exactly what Routes are for in Kohana 3.
subdomain: admin.site.com
maps to directory: ~/public_html/admin/
controller: Controller_Adminsite
controller directory: ~/application/classes/controller/adminsite.php
the route for bootstrap.php:
Route::set('admin', 'admin(/<action>(/<id>))')
->defaults(array(
'controller' => 'adminsite',
'action' => 'run',
));
It looks like someone has actually created a submodule for doing subdomain routing:
https://github.com/jeanmask/subdomain

Categories