Can't use session variable in routes.php file in codeigniter? - codeigniter

I am use following code to retrieve the session variable in routes.php
if($this->db_session->userdata('request_url')!="")
{
$route['user/(:any)'] = "search_user_name/redirect_url/".$_SESSION['request_url'];
$this->db_session->unset_userdata('request_url');
}
else {
$route['user/(:any)'] = "search_user_name/index/$1";
}
the session variable would be set into template/header.php
$this->db_session->set_userdata('request_url', $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);

You can not use db_session in routes.php because routes.php is parsed before db_session is loaded.
Maybe you should create a base controller and redirect from the constructor of the base controller.

Correct me if iam wrong.
You can use hooks.
Codeigniter user guide hooks

You can use database in routes and put your routes url in database.
Here is an example:
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$table2 = $db->dbprefix.'lang';
$query2 = $db->get( $table2 );
$result2 = $query2->result();
foreach( $result2 as $row )
{
$fields = $db->list_fields($table2);
$findme = 'code';
foreach($fields as $field):
$pos = strpos($field, $findme);
if($pos !== false and $row->$field != ''):
$route[''.$row->$field.''] = 'main/setlang/$1';
endif;
endforeach;
}

Related

How do i fetch session id from database to controller and compare codeigniter

It Return undefined index uname also try $row->name not working return false
public function profile() {
$this->load->view('header');
$uname = $this->session->userdata('uname');
$row = $this->brid_groom_fetch->get_program_specific_gender();
if ($row['uname']->uname == $uname) {
$session_id = $this->session->userdata('session_id');
var_dump($session_id);
} else {
echo 'fail';
}
}
First you need to assign session variables to an array.
Then your code
$id = $this->session->userdata('uname')
will be relevent.
To assign you can use
$this->session->set_userdata(
array(
'uname' => $row->name
)
);
Where $row->name is the value fetched from your model query.
The above sets the value in session variable 'uname'
Now you can use $id wherever you want
Also Check your autoload.php file under config dir
$autoload['libraries'] = array('session');
should be present there.
If not, then you can do the above or call
$this->load->library('session')
in your controller function.
See if that helps

Customize dynamic URL.change ?id to name

I'm using codeigniter for make dynamic website. In news page, I use url method like: http://test.com/test/test/news_details?id=27
how can i change my url to put news title within ?id=27
example:
http://test.com/test/test/news_details/pass-this-url
"pass-this-url" refer to id=27.
Best Regards.
Use routes : http://www.codeigniter.com/user_guide/general/routing.html
In your case it will be something like this :
$route['pass-this-url'] = "test/test/news_details?id=27";
So http://www.example.com/pass-this-url will be understand by codeigniter as http://www.example.com/test/test/news_details?id=27
however, if you want to make it dynamic, you will have to call your db :
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'news' );
$result = $query->result();
foreach( $result as $row )
{
$route[$row->title] = "test/test/news_details?id=". $row->id;
//We suppose here that your title is URL friendly.
}
Go to application/config/config.php and check this:
$config['enable_query_strings'] = FALSE;

Render module in joomla

How do we render module in joomla with the title. Because now, I am able to render the module based on the position but it does not include the module title.
Here's how I render the module.
<?php
jimport('joomla.application.module.helper');
$modules = JModuleHelper::getModules('position-3');
foreach ($modules as $module) {
echo JModuleHelper::renderModule($module->title);
echo JModuleHelper::renderModule($module);
}
?>
Many Thanks.
Try this,
Using this method you can pass parameters to the module .
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$Module = JModuleHelper::getModule('mod_fmDataGrid');
$Params = "param1=bruno\n\rparam2=chris"; //This is the way of passing params values
$Module->params = $Params;
echo $renderer->render($Module);
Hope it helps..
Try using the following:
foreach ($modules as $module)
{
echo $module->title;
echo JModuleHelper::renderModule($module);
}
You can also use the following, however you will have to manually enter the module title. This is only assuming you don't want it to be dynamic. You will also need to change mainmenu
$module = JModuleHelper::getModule( 'mainmenu', 'Module Title Goes Here' );
echo JModuleHelper::renderModule( $module );
Try this.
Hope this will work for you.
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$contents = '';
$db = JFactory::getDBO();
$db->setQuery("SELECT * FROM `#__modules` WHERE id = 'your module id'");
$modules = $db->loadObjectList();
$module = $modules[0];
$contents = $renderer->render($module);

Send params to Joomla module

I write custom module, for example, when module called with parametr 1, module return 1, when called with 2, return 2 e.t.c.
But I cant find any documentation, how to send parametrs from page to module. Its how I call module now:
jimport( 'joomla.application.module.helper' );
$modules = JModuleHelper::getModules('NAME_OF_CUSTOM_POSITION');
$count_array = count($modules);
if ($count_array >0)
{
$attribs['style'] = 'xhtml';
echo JModuleHelper::renderModule( $modules[0], $attribs );
}
?>
But I don't know how to send params nor how to recieve them in my module.
I have used below code in one of my custom component.And it worked for me.
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$params = array('style'=>'xhtml');
$contents = '';
foreach (JModuleHelper::getModules('NAME_OF_CUSTOM_POSITION') as $mod) {
$registry = new JRegistry();
$registry->loadString($mod->params);
$registry->set('paramname','paramvalue');
$mod->params = (string)$registry;
$contents .= $renderer->render($mod, $params);
}
echo $contents;

JModuleHelper::getModules returns an empty array when SEF enabled only

I am using joomla 1.5. in this i have to include a module so i am using the code
$modules =& JModuleHelper::getModules('left1');
foreach ($modules as $module)
{
echo JModuleHelper::renderModule($module);
}
It returns an array value when SEF is disabled. but returns an empty array when SEF is enabled.I am totally confusing with this. Could anyone help me ?
If you would like to render modules loaded at position left1 use below code
$position = 'left1';
jimport( 'joomla.application.module.helper' );
if(JModuleHelper::getModules($position)) {
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('modules');
$options = array('style' => 'xhtml');
return $renderer->render($position, $options, null);
}

Resources