Kohana 3.2 Routing and subdomains problem - php

I have subdomain www.panel.example.com and domain www.example.com.
My bootstrap.php:
<?php
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE,
));
Route::set('panel', '(<controller>(/<action>(/<id>)))', array('subdomain' => 'panel'))
->defaults(array(
'directory' => 'panel',
'controller' => 'panel',
'action' => 'index',
'subdomain' => 'panel',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
?>
When I'm writing adress on browser: www.panel.example.com I have an error:
HTTP_Exception_404 [ 404 ]: The requested URL / was not found on this server.
My structure:
application/classes/controller (controllers of domain)
application/classes/controller/panel (controllers of subdomain)
How to do it properly?

There is no built in way to deal with subdomains in routes. So my suggestion comes from searching the internet:
One way to do this is get the subdomain from the SERVER global:
list($subdomain) = explode('.', $_SERVER['SERVER_NAME'], 2);
Then, call a controller or directory in the route based on this subdomain:
Route::set('panel', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => $subdomain,
'controller' => 'panel',
'action' => 'index',
));
Or use lambda/callback routes for more flexibility when handling the subdomain: http://kohanaframework.org/3.2/guide/kohana/routing#lambdacallback-route-logic
This answer is based on using different templates for different subdomains: kohana v3: using different templates for different subdomains

I use this code to check if subdomain route need to be set.
//Set an array with subdomains and Configs
$arrDomainsDirectories = array(
'services'=>array(
'subdomain'=>'services',
'directory'=>'Services',
'controller' => 'Home',
'action' => 'index'
),
'default'=>array(
'subdomain'=>NULL,
'directory'=>'',
'controller' => 'Home',
'action' => 'index'
)
);
//Config Route based on SERVER_NAME
$subdomain = explode('.', $_SERVER['SERVER_NAME'], 2);
//If Not Subdomain set Default
if(count($subdomain) <= 1){
$subdomain = 'default';
} else {
$subdomain = $subdomain[0];
}
$routeConfig = $arrDomainsDirectories[$subdomain];
Route::set('default', '(<controller>(/<action>(/<id>)))', array('subdomain'=>$routeConfig['subdomain']))
->defaults(array(
'directory' => $routeConfig['directory'],
'controller' => $routeConfig['controller'],
'action' => $routeConfig['action']
));

Related

Kohana domain routes

I can't set default route on my domain. I want mydomain.com to lead to mydomain.com/app/new
i have tried many route sets but nothing worked.
Route::set('homePage', '')
->defaults(array(
'controller' => 'app',
'action' => 'new',
));
Route::set("home","mydomain.com")
->defaults(array(
'controller' => 'app',
'action' => 'new',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'app',
'action' => 'new',
));
You need to correctly set base_url (in application/bootstrap.php), e.g.
Kohana::init(array(
'base_url' =>'http://example.com/app/new/',
...
));
Depending on how you're serving the site, you may also need to use RewriteBase within .htaccess.
RewriteBase /app/new/ should do it.

Kohana subdirectory controllers (The requested URL :uri was not found on this server.)

Trying to call controllers in controllers/api/v1/ folder in browser. It was working on localhost properly but i get a kohana error after moving to server :
if ( ! class_exists($prefix.$controller))
{
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $request->uri()) )->request($request);
}
// Load the controller using reflection
$class = new ReflectionClass($prefix.$controller);
Init:
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE,
));
Here are my routes:
Route::set('api', 'api/v1(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'api/v1',
'controller' => 'admin',
'action' => 'index',
));
Route::set('subsource', 'api/v1/<controller>(/<id>(/<action>))')
->defaults(array(
'directory' => 'api/v1',
'controller' => 'admin',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
Controllers name start with Controller_Api_V1_
Controllers in /controllers/ folder are working properly.
If i understand you, you need a new externel request. Here are the documentation: Requests

kohana 3.0 directory to controller

I have my website under Kohana 3.0 which works perfectly with the defaut Route
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
When I try to access my website at this address http://127.0.0.1/web/ It loads the url http://127.0.0.1/web/user . It is OK.
But now I want to add the admin directory under the controller. So my web tree looks like this
classes
| controller/
Admin/
dashboard
web.php
| model
I would like to allow the Admin to access the admin's page in a url like this
http://127.0.0.1/admin/dashboard. Where dashboard is the controller under the admin's directory.
I modify the bootstrap file with this
Route::set('admin', '<directory>(/<controller>(/<action>(/<id>)))',
array('directory' => '(admin)'))->defaults(array(
'controller' => 'user',
'action' => 'index',
));
I can access the admin session through http://127.0.0.1/web/admin/dashboard/
But I can't access the default controller that is http://127.0.0.1/web/ . The error Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI:
I am missing the default access for the controller.
How can I set Route it to make access to my web site either through the link:
http://127.0.0.1/web/
and
http://127.0.0.1/web/admin/dashboard/
EDIT
From the kohana documentation, it is written
In this example, we have controllers in two directories, admin and affiliate. Because this route will only match urls that begin with admin or affiliate, the default route would still work for controllers in classes/controller.
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin|affiliate)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Source : http://kohanaframework.org/3.0/guide/kohana/routing#examples
Now I modify my code to
Route::set('default', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin)'))
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
but I have this error
Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI:
when I want to access the default controller like http://127.0.0.1/user/index
This route would translate to: http://127.0.0.1/admin/web, but your Admin folder would need to have user controller inside.
Route::set('default', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin)'))
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
If you want the directory to be optional, you'd need to
Route::set('default', '(<directory>(/<controller>(/<action>(/<id>))))',
array(
'directory' => '(admin)'
)
)
->defaults(array(
'directory' => 'admin',
'controller' => 'dashboard',
'action' => 'index',
));
But, in your case, you need multiple routes. Above the "catch all" route, put this:
Route::set('user', 'user(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'user',
'controller' => 'user',
'action' => 'index',
));
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'dashboard',
'action' => 'index',
));
Route::set('default', '(/<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Try to insert the directory inside ->defaults
Route::set('whatever', 'whatever')
->defaults(array(
'directory' => 'admin',
'controller' => 'user',
'action' => 'index',
));

Kohana 3.3 Routes where the name of a Controller and Directory are the same

How do I correctly setup routes in Kohana 3.3 where the name of my controller and directory are the same as in my example below?
/application/classes/Controller/Admin/Author.php
- admin/author
- admin/author/add
- admin/author/edit
/application/classes/Controller/Admin/Author/Book.php
- admin/author/book
- admin/author/book/add
- admin/author/book/edit
When using the following routes in the specified order, I'm only able to access admin/author{/action}, but not admin/author/book{/action}.
Reversing the routing order gives me access to admin/author/book{/action}, but not admin/author{/action}
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'Main',
'action' => 'index',
));
Route::set('admin/author', 'admin/author(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin/author',
'controller' => 'Main',
'action' => 'index',
));
You need something like this:
Route::set('admin-author', '<directory>/<controller>(/<action>(/<id>))', array(
'directory' => '(admin|admin/author)',
'action' => '(add|edit|delete|index)'
))
->defaults(array(
'directory' => 'admin',
'controller' => 'author',
));
Also, you can try to check action with regex ^book (not tested).
Another way is to use Route filters.
you need to define for admin/author/book as well
Route::set('admin-author-book', 'admin/author(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin/author',
'controller' => 'book',
'action' => 'index',
));
Route::set('admin-author-book', 'admin/author(/book(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin/author',
'controller' => 'book',
'action' => 'index',
));
The most important thing about routes: Routes are matched in the order they are added. Reversing the order of your routes was correct, but the problem is that your route admin/author also matches admin/author{/action}. What you could do: make the controller and action mandatory:
// Does not match admin/author/add
Route::set('admin/author', 'admin/author/<controller>/<action>(/<id>)')
->defaults(array(
'directory' => 'admin/author',
));
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'Main',
'action' => 'index',
));
There are several approaches, another one would be to create a "whitelist" of controllers:
// Does not match admin/author/add
Route::set('admin/author', 'admin/author/<controller>(/<action>(/<id>))',
array(
'controller' => '(book|another-controller)'
))
->defaults(array(
'directory' => 'admin/author',
));
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'Main',
'action' => 'index',
));

Kohana 3.2 Route - controllers in subdirectories

I need to create next structure:
+controller
++admin
+++catalog
++++category.php
++++product.php
+++users
and I need to open them by url /admin/catalog/category/action/param
I tried to create route:
Route::set('admin', '(<directory>(/<controller>(/<action>(/<custom_param>))))',array(
'directory' => '(admin/.*)'
))
->defaults(array(
'controller' => 'dashboard',
'action' => 'index'
));
Not tested:
Route::set('admin', 'admin/<directory>/(<controller>(/<action>(/<custom_param>)))',
array(
'directory' => '(catalog|users)'
))
->defaults(array(
'controller' => 'dashboard',
'action' => 'index',
));

Categories