I am trying to set title, meta description and keywords dynamically using the language feature like below:
$lang['/contact_title'] = "Contact Us";
$lang['/contact_desc'] = "Description for contact us page";
$lang['/contact_keywords'] = "key, words";
and in header file I am using it like below:
<title><?echo $this->lang->line($_SERVER['PATH_INFO']."_title");?></title>
<meta name="description" content="<?echo $this->lang->line($_SERVER['PATH_INFO']."_desc");?>">
So far this is working great, but how I could make it work for dynamic titles? Regex was the first though that came in my mind, but unfortunately it doesn’t work with language classes like it works for routing as I have already tried it.
The reason i am using language class is because, its huge list, there are many controllers and we are going to change this seo keywords, title again and again. So to avoid that hassle i want all seo details in one place. So in future if we want to make changes, instead of visiting all the controllers i can make changes in one file only.
Can anyone suggest me an idea/solution of how I could make it work with dynamic titles?
2
Answers
Try This
On controller method add this:
And finally call on
header.php
:You should not use any of regex there. All data should be passed by controller, thats how MVC works. So, depending your situation, you should create variables in the controller and pass it to view. You can make it like this:
in your language file (inside english directory for example):
and after you switch languages (by default it was defined in your config file) you can access it via:
so the code in your controller should look like this:
So using codeigniter
$this->session->userdata('lang');
it will return current selected lang. You can change it:and then system will read files from your application/language/germany/ folder. Do not forget to load language first –
$this->lang->load('lang_file', $this->session->userdata('lang'))