kohana 3.1 routing , set controller forward domain , in url - php

I want make such URL: http://somecontroller.example.com where somecontroller will be any controller... I am using Kohana3.1.
I know routing as well as make many routes but I haven't one such as that...
I have those default routes in bootstrap:
Route::set('default', '(<controller>(/<action>(/page<page>)(/<id>)))')
->defaults(array(
'directory' => 'index',
'controller' => 'main',
'action' => 'index',
));

Kohana's routing system only allows you to parse the URI, so you can't do it in a clean way. But, you can do something like this to have your desired behaviour:
$controller = preg_match('/^([\w]+)\.example\.com$/', $_SERVER['HTTP_HOST'], $match)
? $match[1]
: 'main';
Route::set('default', '(<action>(/page<page>)(/<id>))')
->defaults(array(
'directory' => 'index',
'controller' => $controller,
'action' => 'index',
));
However, this routing won't work in console (php index.php --uri=<uri>), because HTTP_HOST isn't defined.

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 3.2: Route the control to another path than default

I have tried the following way
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin|affiliate)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Instead of Home controller in Campaign folder I need to load Home Controller from Campaign/City folder by default. I have used the above code in bootstrap.php, but it gives 'URL not found on this server' error
The array that is passed as the third parameter to Route::set() restricts the values that can be passed to the route. In your code array('directory' => '(admin|affiliate)') restricts the directory parameter to be either 'admin' or 'affiliate' To have it go deeper you would need to modify the route.
The Kohana Routing Guide has a bunch of examples using filters to route in any way you could possibly imagine, but you could route to subdirectories without turning to filters.
For example, with the following directory structure:
classes/Controller/
Admin/
Cupertino/
Home.php (Controller_Admin_Cupertino_Home)
Home.php (Controller_Admin_Home)
Affiliate/
Cupertino/
Home.php (Controller_Affiliate_Cupertino_Home)
Home.php (Controller_Affiliate_Home)
And the following route:
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin/cupertino|admin|affiliate/cupertino|affiliate)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
The URLs index.php/admin, index.php/admin/cupertino, index.php/affiliate, and index.php/affiliate/cupertino will route through their respective controllers.
Subdirectories need to be listed before their parents otherwise Kohana will always match to the parent. e.g. the following will always route to Controller_Admin_Home even for the URL index.php/admin/cupertino:
`array('directory' => 'admin|admin/cupertino')`.
Using filters might look something like the following:
Route::set('admin_subsections', 'admin/<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(cupertino|sanjose|santacruz)'
))
->filter(function($route, $params, $request)
{
// append "admin/" to the directory param
$params['directory'] = 'admin/' . $params['directory'];
return $params; // Returning an array will replace the parameters
})
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
And again, order matters.

Kohana 3.3 controllers in subfolders

I have controllers
--controllers
--Administrator
-Base.php
-Admin.php (extend Base.php)
-controller1.php
-controller2.php
-etc
And my route looks like this
Route::set('administrator', 'Administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Try to load this controller and i get message Not found
What's wrong?
UPDATE!
class Controller_Administrator_Base extends Controller_Template {
public $template = 'panel/index';
public function action_index(){
echo 'kupakonia';
}
} // End Welcome
All routes. I was trying alot of sugesstions from google and nothing helps, and I write something becouse i cant update this tobic becouse is too much code inside.
Still can't update.
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'read',
'action' => 'index',
));
Route::set('user', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
Route::set('administrator', 'administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Any sugestions?
Ps. still can't update this topic :/ Wtf must be more characters in tekst than in code ?
this is my first asc on stackoverflow. So sorry for this ;)
You have two catch all routes (default and user) in your Bootstrap file. These two routes do exactly the same.
Also, since these routes are catch all and the order of the routes matter, this means if the catch all route is the first it will match all URLs.
Try this:
Route::set('administrator', 'administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'read',
'action' => 'index',
));
Another thing, it is usually bad practice to have a catch all route. Try to create routes specific for your Controllers.

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: Routing with subdirectories error, controller does not exist

So I'm trying to build a route with sub directories and following the Kerkness wiki guide but keep getting errors. If someone could point out what I'm doing wrong I would greatly appreciate it.
http://kerkness.ca/wiki/doku.php?id=routing:building_routes_with_subdirectories
The code:
Route::set('default', '(<directory>(/<controller>(/<action>(/<id>))))', array('directory' => '.+?'))
->defaults(array(
'directory' => 'admin',
'controller' => 'main',
'action' => 'index',
));
The url:
/admin/weather/feedback
The file:
/application/classes/controller/admin/weather/feedback.php
class Controller_Admin_Weather extends Controller_Admin_Base {
The error:
ReflectionException [ -1 ]: Class controller_admin_weather does not exist
Weather needs to be the controller not feedback. Make a weather.php in the admin folder and put the controller as Controller_Admin_Weather and then the action action_feedback.
As #mikelbring said, your controller class is named wrongly. A class in that file should be called Controller_Admin_Weather_Feedback
Do you really need so many optional segments in your route?
Also; if there are no variable elements to the urls you can just stick with defaults like this:
Route::set('my_route_name', 'admin/weather/feedback')
->defaults(array(
'directory' => 'admin/weather',
'controller' => 'feedback',
'action' => 'index',
));
If your class was in /application/classes/controller/admin/weather.php and had an action_feedback(...) method, you could use the following route
Route::set('my_route_name', 'admin/weather/feedback')
->defaults(array(
'directory' => 'admin',
'controller' => 'weather',
'action' => 'feedback',
));

Categories