I want to have different title (in head) for each Controller and action.
How to do this from the controller?
Your Controller
class SiteController {
public function actionIndex() {
$this->pageTitle = 'Home page';
//...
}
//..
}
Layout file
<title><?php echo $this->pageTitle; ?></title>
Maybe you forgot to add reference in your html?
If you want to have a different title in each action
Then simply set the value of CController.pageTitle inside your action:
class MyController extends CController {
public function actionIndex() {
$this->pageTitle = "my title";
// other code here
}
}
If you want to share a specific title between multiple actions
One way would be to simply follow the above approach, possibly by using a class constant as the page title:
class MyController extends CController {
const SHARED_TITLE = "my title";
public function actionIndex() {
$this->pageTitle = self::SHARED_TITLE;
// other code here
}
public function actionFoo() {
$this->pageTitle = self::SHARED_TITLE;
// other code here
}
}
However, this requires you to visit each action separately whenever you want to include or exclude it from the "title sharing" scheme. A solution that does not have this drawback is to use a filter. For example:
class MyController extends CController {
public function filters() {
// set the title when running methods index and foo
return array('setPageTitle + index, foo');
// alternatively: set the title when running any method except foo
return array('setPageTitle - foo');
}
public function filterSetPageTitle($filterChain) {
$filterChain->controller->pageTitle = "my title";
$filterChain->run();
}
public function actionIndex() {
// $this->pageTitle is now set automatically!
}
public function actionFoo() {
// $this->pageTitle is now set automatically!
}
}
If you want to have the same title across all actions
This is obvious, but I mention it for completeness:
class MyController extends CController {
public $pageTitle = "my title";
public function actionIndex() {
// $this->pageTitle is already set
}
public function actionFoo() {
// $this->pageTitle is already set
}
}
You can use the function init or before action or run which call before the actual action call. So In that function you can set the public pageTitle variable for the controller.
Use Like this:
public function init()
{
parent::init();
$this->pageTitle = "My Page Title";
}
Yon can give like this:-
$this->set("title", "Enrollment page");
and use this $title in ur ctp files by giving different name or title..
try this..
In the VIEW page (index.php, view.php, create.php etc)
$this->setPageTitle('custom page title');
Related
I want to create custom 404 page for my site.I mean when i type url as
http://example.com/anydummytext
need to redirect to custom 404page
How to create custom page for this
The simple way is create a controller you can name the error controller to any what you want.
Example
<?php
class Not_found extends CI_Controller {
public function index() {
// some content
$this->load->view('not_found');
}
}
Then on another controller you can redirect it
redirect('not_found');
Example Only
<?php
class Home extends CI_Controller {
public function index() {
$result = $this->some_model->get();
if ($result) {
// content
$this->load->view('home');
} else {
redirect('not_found');
}
}
}
The other option is in config/routes.php you can use codeigniter
$route['404_override'] = 'not_found'; // Note will not work in a subfolder
You can create a new controller or use existing controller to write down a function for loading the VIEW for 404.
class Nopage extends CI_Controller
{
/* Index function
*
* return void
*/
public function index()
{
$this->load->view('nopage_404');
}
}
}
Call the controller function in config->routes as below.
$route['404_override'] = 'nopage';
contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.
I want every controller to have a method _render_page, which loads the master template and passes the data object.
My home controller class looks like this:
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$data['title'] = "Site title";
$data['the_view'] = 'welcome_message';
$this->_render_page($this->layout, $data);
//$this->load->view($this->layout, $data); //This works ok..
}
}
MY_controller class:
class MY_Controller extends CI_Controller {
public $layout;
public $viewdata;
public function __construct() {
parent::__construct();
$this->layout = 'layout/master_template';
$this->viewdata = null;
}
public function _render_page($view, $data=null, $render=false) {
$this->viewdata = $data;
$this->viewdata['the_view'] = $view;
if($this->ion_auth->logged_in()) {
$user_obj = $this->ion_auth->user()->row();
$usr_data['username'] = $user_obj->username;
$user_obj = null;
$this->viewdata['usr_data'] = $usr_data;
}
$this->load->view($this->layout, $this->viewdata); //The code crashes here
}
}
When I browse to home controller I get nothing, just white screen no errors...
Found a solution: I'm calling _render_page in a wrong way.
Instead of this:
$this->_render_page($this->layout, $data);
I should call like this:
$this->_render_page('welcome_message', $data);
Of course, that's what this function is about - to load a master page and pass the view name as member of $data, so the master page will know which view to load.
See, you need to understand the flow going,
when you call your class home , it extends MY_Controller, CI will look for MY_Controller, the constructor of your MY_controller gets executed after which CI starts executing the constructor of your home controller then your default method of home controller,
so in order to make it work you need to call _render_page()-
change your MY_Controller like -
class MY_Controller extends CI_Controller {
public $layout;
public $viewdata;
public function __construct() {
parent::__construct();
$this->layout = 'layout/master_template';
$this->viewdata = null;
$this->_render_page($this->layout, $data=null, $render=false); // call your method
}
public function _render_page($view, $data=null, $render=false) {
$this->viewdata = $data;
$this->viewdata['the_view'] = $view;
if($this->ion_auth->logged_in()) {
$user_obj = $this->ion_auth->user()->row();
$usr_data['username'] = $user_obj->username;
$user_obj = null;
$this->viewdata['usr_data'] = $usr_data;
}
$this->load->view($this->layout, $this->viewdata); //The code crashes here
}
}
I have created two views in CodeIgniter and I have created controller named HelloWorld.php
It contains two views.. but my problem is that the second view never gets called.
http://localhost/CodeIgniter/HelloWorld/Hello
Works fine for me, but second view
http://localhost/CodeIgniter/HelloWorld/Buzz
Doesn't call the second view
Here is my code
<?php
class HelloWorld extends CI_Controller
{
var $name;
var $color;
function __construct()
{
parent:: __construct();
$this->name= 'Suzzu';
$this->color = 'aqua';
}
public function Hello()
{
$this->load->view("hello");
}
public function Buzz()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->view("welcome",$data);
}
}
what's the problem ??
Your code works as expected for me provided that:
there is a view file "views/welcome.php"
there is a view file "views/hello.php"
Can you verify that HelloWorld::Buzz() is being called?
public function Buzz()
{
die('yes, it works');
}
If this function doesn't execute when you go to /localhost/CodeIgniter/HelloWorld/Buzz, can you provide the following:
$config['base_url'] (config/config.php)
$config['index_page']
config/routes.php contents
change $config['base_url']="localhost/codeigniter/contollername"
I have my controller
class Page extends CI_Controller {
public function id() {
$this->load->model('content');
$page = $this->uri->segment(3, 0);
if($page == 0)
$page = $this->content->get_default_page($page);
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
$data['content'] = $this->content->get_content($page);
$this->load->view('main', $data);
}
}
Now I'll try to explain.
Im getting navigation, and navigation text from mysql (id, navName, navText).
then im returning those elements in views/main_view.php in url like: http://abc.com/page/id/1 etc...
Now i need to create other controller like mySuperDuperModule which have some functions not just text.
The problem is that if i create new controller like Gallery(), i need to copy all the stuff from Page() controller to make website show the same.
Is there any way not to do that ?
You can create base controller under /application/core/MY_Controller.php
Here MY_ is the value specified for $config['subclass_prefix'] in /application/config/config.php
class MY_Controller extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
}
public function loadPage($page)
{
$this->load->model('content');
if($page == 0)
$page = $this->content->get_default_page(); // I hope this function will give default page from database table. (A Change from your code)
$this->data['navigation'] = $this->content->getNav();
$this->data['pagename'] = $this->content->get_pagename($page);
$this->data['content'] = $this->content->get_content($page);
}
}
Your modified Page Class in /application/controllers/page.php
class Page extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = $this->uri->segment(3, 0);
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
and your new gallery controller can be in /application/controllers/gallery.php
class Gallery extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = 10; // Assuming gallery page index is 10 in database table OR you can change any logic here.
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
you can create as many controllers as you want by extending MY_Controller.
You can pass these two lines to any controller
and they will display the same template.
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
If you still dont understand try to use this library
https://github.com/philsturgeon/codeigniter-template