-
wiyono
Newbie
-
Posts: 6
Threads: 1
Joined: Jan 2015
Reputation:
0
Can somone help me how to set different page title for each page? i am new with codeigniter and i make my website with the tutorial from codeigniter, but i am confused how to make title page for each page, maybe from controller or so?
thx for your help
Wiyono
-
wiyono
Newbie
-
Posts: 6
Threads: 1
Joined: Jan 2015
Reputation:
0
ooh, no aswer? please help me ;-)
-
wiyono
Newbie
-
Posts: 6
Threads: 1
Joined: Jan 2015
Reputation:
0
Hey CroNiX,
thx for your answer, that's mean i have to make each controller for each pages?
ohye, can i make only one controller for all page or so?
thx again for your answer?
Regard
Wiyono
-
mwhitney
Posting Freak
-
Posts: 1,101
Threads: 4
Joined: Nov 2014
Reputation:
95
You could handle this any number of ways. What I usually do is set a default page title in my controller's constructor, then change the title as needed in the controller's methods (in most cases, the methods just add something to the title set by the constructor, since all of the pages served by a controller are usually related).
Then I use a function to load my data into my view and call it from all of the controllers, so I can get some defaults if I forget to set something in a given method or don't want to repeat certain things in each method.
For example:
PHP Code: class News extends MY_Controller { private $data = array(); private $pageTitle; public function __construct() { parent::__construct(); $this->pageTitle = lang('news_default_page_title'); } public function index() { // ... do something ... $data['page_title'] = "List : {$this->pageTitle}"; $this->render($data, 'index'); } public function details($id = 0) { // ... do something ... $this->render(); // Load $this->data into the 'News/details' view... } protected function render($data = array(), $view = '') { if (empty($view)) { // Load the view named for the current controller/method. $view = "{$this->router->class}/" . (empty($this->router->method) ? 'index' : $this->router->method); } $data = array_merge($this->data, (array) $data); if (empty($data['page_title'])) { // Set the page title. $data['page_title'] = $this->pageTitle; } // ... other stuff ... $this->load->view($view, $data); } }
-
wiyono
Newbie
-
Posts: 6
Threads: 1
Joined: Jan 2015
Reputation:
0
Hey mwhitney,
thx for your help
i have solution like this
PHP Code: class Pages extends CI_Controller { function index() { $data['title'] = "Startseite"; $this->load->view('templates/header', $data); $this->load->view('pages/startseite', $data); $this->load->view('templates/footer', $data); } function about_us() { $data['title'] = "About us"; $this->load->view('templates/header', $data); $this->load->view('pages/about-us', $data); $this->load->view('templates/footer', $data); } }
some change in routers. and work good, but the url address is "about_us" can you help me how to make the url address "about-us", when i put
i got php error, can you help me please how to make the url address "about-us", with "-" and not "_" (underscore)
Thx again
Regard
Wiyono
-
CroNiX
Senior Member
-
Posts: 428
Threads: 0
Joined: Jan 2015
Reputation:
25
02-02-2015, 03:54 PM
(This post was last modified: 02-02-2015, 03:56 PM by CroNiX.)
You can't have a dash in a function/class/method name. To PHP, that's subtraction.
Basically you create a route, using the dash, that routes to whatever controller/method, but the method name must be legal PHP.
Code: $route['about-us'] = 'controller/about_us'; //Sends 'about-us' request to the controller/about_us controller/method with the underscore
Controller method:
Code: function about_us()
{
//your about-us page
}
Code: <a href="/about-us">About Us</a> //clicking will actually go to "about_us" behind the scenes due to our route
-
wiyono
Newbie
-
Posts: 6
Threads: 1
Joined: Jan 2015
Reputation:
0
Hey CroNiX,
thx for your answer, unfortunately i got error 404 page if i make the router like that, then i call about-us page. Can you help? what i did wrong?
Thx
Regard
Wiyono
-
wiyono
Newbie
-
Posts: 6
Threads: 1
Joined: Jan 2015
Reputation:
0
Hey CroNiX....
yeeeeeeeeezzzz...
thank you so much...
i forget to change the controller with the name of controller...
thank you again.
Regard
wiyono
|