Routing depending on the subdomain - php

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

Related

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...

Set routes in Kohana

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.

Do not understand routing in Kohana php framework

Trying to learn Kohana, coming from Asp.Net MVC 3. In MVC I am used to the default route (very similar to the default route in Kohana) working like this:
The default route matches any of these:
/
/Home
/Home/Index
I expected it to be the same in Kohana, but it seems all it matches is this:
/
Here's my setup in bootstrap.php:
Kohana::init(array(
'base_url' => '/kohana',
'index_file' => FALSE
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
So if I enter localhost/kohana in the address bar I get to the view called by controller home and action index (action_index). But if I enter localhost/kohana/home/index I get an error saying the object doesn't exist.
Why is this? Shouldn't I be able to enter controller and action in the url and get the correct routing? So basically I have no idea how to enter URLs to get to an action method...
Sorry if this is a stupid newbie question, but I can't figure it out Googling and looking at the Kohana docs... I've been sort of spoiled by the fact that Asp.Net MVC routing always just worked, so I never had to really learn about it...
Rename example.htaccess to .htaccess, open it and change the line RewriteBase / to RewriteBase /kohana/. Windows explorer will probably not allow for a file without the name, so you have to use another file manager (Total Commander for example).

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',
));

Routes on zend modules

I want to make a kind of alias for my modules:
Is there a way to make
http://www.example.com/news point to
/modules/news/controller/news/->action
index.
And if i go to
http://www.example.com/news/show it
automaticly points to
/modules/news/controller/news/ ->
action show
Or do I have to make a route for every single action I make in this module?
If I don't make a route my links will look like:
http://www.example.com/news/news/show and http://www.example.com/news/news/show.
P.s i'm using Zend 1.10.6
Thanx in advance!
I guess it will be something like that:
$route = new Zend_Controller_Router_Route(
'news',
array('module' => 'news', 'controller' => 'news', 'action' => 'index'));
$router->addRoute('news', $route);
where first arg 'news' for Zend_Controller_Router_Route is the url http://www.example.com/news that will route to http://www.example.com/news/news/index
salu2
In your config file you must enable modules by putting this in your config file
resources.modules[] =
And it should by default route correctly in your application.
You will have to define a route for each URL you want routed in a non-standard way.

Categories