[eluser]Tottys Design[/eluser]
Can a hook load a controller?
[eluser]davidbehler[/eluser]
Without thinking too much about it I would say no.
The controller that will be loaded is defined by the url/the routing defined in config/routes.php
The "pre_system" or the "pre_controller" controllers could be used to redirect to another url I guess...maybe even hack the routing? Don't know...just a wild guess here.
Why would you need that anyway?
[eluser]Tottys Design[/eluser]
[quote author="waldmeister" date="1258508147"]Without thinking too much about it I would say no.
The controller that will be loaded is defined by the url/the routing defined in config/routes.php
The "pre_system" or the "pre_controller" controllers could be used to redirect to another url I guess...maybe even hack the routing? Don't know...just a wild guess here.
Why would you need that anyway?[/quote]
I need to make a dynamic multilanguage website, take a look at the urls:
en/gallery/
pt/galeria/
it/galeria/
both should trigger the gallery controller.
1) the params are stored,
2) then converted in the main language (gallery to gallery, and galeria to gallery)
3) load the controller named gallery
[eluser]Colin Williams[/eluser]
Let us know exactly what you are trying to achieve and we might be able to offer a good solution that fits with CI's philosophy and conventions.
[eluser]Tottys Design[/eluser]
[quote author="Colin Williams" date="1258509415"]Let us know exactly what you are trying to achieve and we might be able to offer a good solution that fits with CI's philosophy and conventions.[/quote]
I need to make a dynamic multilanguage website, take a look at the urls:
en/gallery/
pt/galeria/
it/galeria/
both should trigger the gallery controller.
1) the params are stored,
2) then converted in the main language (gallery to gallery, and galeria to gallery)
3) load the controller named gallery
[eluser]Colin Williams[/eluser]
Sorry about that. I posted mine before seeing your response.
You could achieve 2 and 3 with simple routing rules:
Code: $route['([a-z]{2})/gal([^\/]*)'] = "gallery/$1";
Then take care of #1 in the gallery controller constructor, where $this->uri->segment(1) is the language code.
[eluser]Tottys Design[/eluser]
[quote author="Colin Williams" date="1258509759"]Sorry about that. I posted mine before seeing your response.
You could achieve 2 and 3 with simple routing rules:
Code: $route['([a-z]{2})/gal([^\/]*)'] = "gallery/$1";
Then take care of #1 in the gallery controller constructor, where $this->uri->segment(1) is the language code.[/quote]
No problem. I know but I don't want to work with routers because I can add new categories and then I should make them available in different languages, and i have to mod always the route file.
Look at the code I have to use to get the controller name, then this should dispatch the action to that controller, but how?-I really don't know. Here I share my code:
(in the next post)
[eluser]Tottys Design[/eluser]
Code: <?php
class Core_router
{
public function __construct()
{
$this->obj =& get_instance();
$this->obj->load->model('Folder', '', TRUE);
$this->obj->load->helper('url');
}
function ex(){
//redirect('/en/gallery', 'location');
}
function index(){
// set default data
$segments = &func;_get_args();
// check if is a HTML or AJAX request
if(isset($segments) && count($segments) > 0){
if($segments[count($segments)-1] == '__AJAX__'){
$requestType = 'AJAX';
}else{
$requestType = 'HTML';
}
}
// set language
if(isset($segments[0])){
$lang = $segments[0];
$languages = array('pt', 'en');
// check if is a valid language
if(!in_array($lang, $languages)){
$lang = 'en';
}
}else{
$lang = 'en';
}
// get controller name
if(isset($segments[1])){
$controllerSEO = $this->Folder->getControllerName($segments[1], $lang);
if($controllerSEO == ''){
$controllerSEO = 'error';
}
}else{
$segments[1] = 'gallery';
$controllerSEO = $segments[1];
}
// set params
unset($segments[0]); // remove language
if($requestType == 'AJAX'){
// remove __AJAX__ from end
unset($segments[count($segments)]);
}
$params = array();
$params[0] = 'root';
foreach($segments as $segment){
array_push($params, $segment);
}
// load language files
//$this->lang->load('generic', $lang);
//$this->lang->load($controllerSEO, $lang);
// load helper
$this->obj->load->helper('array_to_object');
$this->obj->load->helper('array_to_object');
//$this->load->helper('array_to_object');
//$this->load->helper('url');
// load models
$this->obj->load->model('Folder', '', TRUE);
//$this->load->model('Folder','', TRUE);
//$this->load->model('String_lang','String', TRUE);
// dispatch to: HTML or AJAX
if($requestType == 'HTML'){
$data = $this->_html($params, $lang);
}else{
//$this->ajax($params, $lang);
}
//print_r($data);
//$this->run($data);
}
function _html($params, $lang){
$breadcrumbs = $params; array_shift($breadcrumbs);
$data['breadcrumbs'] = $breadcrumbs;
$data['title'] = 'Tottys Design';
$parent_id = 1;
// loop segments until we find the last segment in db
for($i = 0; $i < count($params); $i++){
$seo_name = $params[$i];
// not last loop
if($i != count($params) - 1){
$parent_id = $this->_getId($parent_id, $i, $seo_name, $lang);
}
// last loop
else{
$moreData = $this->_getData($seo_name, $parent_id, $lang, $breadcrumbs);
$data = array_merge($data, $moreData);
}
}
//$data['translation'] = $this->String->getTranslationsForView($data['currentSelection']['view'], $lang);
$data['title'] = 'Tottys Design: ' . $data['currentSelection']['name'];
$data['navData'] = $this->obj->Folder->getNavigation($lang);
//print_r($data);
//$this->loadTemplate($data);
return $data;
}
function _getId($parent_id, $i, $seo_name, $lang){
$root = false;
if($i == 0){$root = true;}
if($parent_id = $this->obj->Folder->getId($seo_name, $parent_id, $lang,$root)){
}else{
$parent_id = '';
}
return $parent_id;
}
function _getData($seo_name, $parent_id, $lang, $breadcrumbs){
$errorPage = false;
// get this data: check if is folder
if($thisData = $this->obj->Folder->getData($seo_name, $parent_id, $lang)){
$thisData = parseObjectToArray($thisData);
$data['currentSelection'] = $thisData;
}
// check if is a file
else if($thisData = $this->Folder->getImageData($seo_name, $parent_id, $lang)){
$thisData = parseObjectToArray($thisData);
$data['currentSelection'] = $thisData;
}
else{
$errorPage = true;
}
// set layout
if(!$errorPage){
$data['layout_type'] = $data['currentSelection']['view'];
}
// if the url is right and this has data
if(!$errorPage && isset($thisData->id)){
$hasChildren = false;
// if has children
if($children = $this->obj->Folder->getChildren($thisData['id'], $lang)){
// add entries number & link_url
foreach($children as &$child){
// add entries number on children
if(!$child['image']){
$child['entries'] = &$this->obj->Folder->getChildrenNumber($child['id']);
[eluser]Tottys Design[/eluser]
Code: }
// add link url on children
$link_url = $breadcrumbs;
array_unshift($link_url, $lang);
$child['link_url'] = &site;_url(implode($link_url, '/'));
$child['link_url'] .= '/' . $child['seo_name'];
}
// set children var
$data['children'] = $children;
$hasChildren = true;
}
}
if($errorPage){
$data['currentSelection']['view'] = 'error';
$data['currentSelection']['name'] = 'error page';
}
$data['lang'] = $lang;
return $data;
}
}
[eluser]Colin Williams[/eluser]
Advanced routing usually involves overloading the core Router class (creating a MY_Router library). If you take a look at the Router class, it should be pretty clear where you'll want to make your modifications.
|