Kohana domain routes - php

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.

Related

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: Omit action from url

Is there any way I can default a route to use action_index and not have to specify it in the url?
ie.
Route::set('user_profile','(<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'public',
'controller' => 'user',
'action' => 'index',
));
To use that I need to specify /users/index/1234
But I'd like to use /users/1234
I tried taking out action from the Route::set() but I ended up with a 404 page.
UPDATE
Now that I have added this route (the top one) my default route doesn't seem to be working now
Route::set('user_profile','(<controller>(/<id>))')
->defaults(array(
'directory' => 'public',
'controller' => 'users', // Note I changed it to plural to match 'users/*' from your url
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'public',
'controller' => 'home',
'action' => 'index',
));
It's as simple as omitting the <action> param from the URL, but still keeping the default value:
Route::set('user_profile','(<controller>(/<id>))')
->defaults(array(
'directory' => 'public',
'controller' => 'users', // Note I changed it to plural to match 'users/*' from your url
'action' => 'index',
));
Note, that unless you have other Route that overrides this behaviour, your user controller will only be able to execute the index action.
Edit
If your users_profile route is only handling /users path then you can set it in the route explicitly:
Route::set('user_profile','users(/<id>)')
->defaults(array(
'directory' => 'public',
'controller' => 'users', // Note I changed it to plural to match 'users/*' from your url
'action' => 'index',
));
This should address the conflicting routes.

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

How to route domain.com/locale/controller to domain.com/controller in Kohana?

I'm trying to implement localization in my website. Currently, the basic (English) website is at http://domain.com/controller/action and I want each localization to be at http://domain.com/locale/controller/action. Basically, if a user visit the latter URL, Kohana will use the same controller and action than for the English version. In code, I will simply swap the strings.
Currently, I tried by adding the following route but that didn't work:
// This is my default route:
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
// This the route for the localizations:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
With this setup, if I visit http://domain.com/es/controller/action, I will get a 404 error. Any idea how I should setup my routes to make this work?
Edit:
Just to complete matino and John Himmelman's answer, if I simply swap the rules as suggested, it will work. However, the "locale" route would then become the catch-all route and you will always have to specify the locale, even if all you need is the default one (in my case "en" / English). To fix that, you can limit the "locale" parameter to the locales you support. For example:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('locale' => '(fr|zh|en)', 'overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
In that case, only URLs that start with "fr", "zh" or "en" will be supported. Additionally, unsupported locales will return a 404 errors, and "domain.com/controller/action" will correctly display the default, English locale.
Kohana applies routes in the order they appear in your bootstrap. This is why your default/catch-all route should always be defined last.
From KO 3.0 routing doc:
It is important to understand that routes are matched in the order
they are added, and as soon as a URL matches a route, routing is
essentially "stopped" and the remaining routes are never tried.
Because the default route matches almost anything, including an empty
url, new routes must be place before it.
As suggested, swapping routes will resolve the issue.
// This the route for the localizations:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
// This is my default route:
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));

Kohana 3.2 Routing and subdomains problem

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

Categories