CodeIgniter - How to make function that extends helper? - php

First my plan was to make a library that read php code content with html meta content and send it into html header.
And just several day ago, I found that CodeIgniter already working about meta tags and My plan change...
Now how to make my library extends this html helper like this
class Htmlplus extends CI_html {
public function show(){
//show html header
}
}

Did you checked the docs?
http://www.codeigniter.com/user_guide/general/helpers.html#extending-helpers
Or do you looking for some other solution?
edit to comment #1
using helper functions in an extended helper is same way as you would do anywhere else. just use them (make sure, helper is loaded)
e.g. in the file application/helpers/MY_url_helper.php you can use anchor()
function helper_test($str) {
return anchor($str, 'test');
}

Create an instance of CI and load helper!
class Htmlplus extends CI_html {
public function show(){
//show html header
$ci = & get_intance();
$ci->load->helper('myhelper');
myfunction();
}
}

Related

how to retrieve data in view Independent of controller in codeigniter

I have a sidebar in my site that receive some information from db and I can't use controller for retrieve data because I have different controller and same sidebar. How can I print this data in view page.
when I wrote in P.h.P code in the view it shows an error that it cant define variables.
How could I do this?
When you find that you need the same code in many different controllers a "custom library" (class) is the perfect choice. Documentation for creating your own libraries is found HERE.
Controllers should be using models to get data from the database. Custom libraries can also use models just like controllers. Here is a very basic custom library called Sidebar. It depends on a model (sidebar_model) that will not be shown. The purpose of the Sidebar library is to return the variables need by the sidebar_view file.
File: application/libraries/Sidebar.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sidebar
{
protected $CI; // Read the documentation link to see why this is needed.
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->database(); //only needed if not already done
$this->CI->load->model('sidebar_model');
}
public function get_sidebar_data()
{
return $this->CI->sidebar_model->get_sidebar();
}
}
The library method get_sidebar_data() returns the variables for the view.
Here is a controller that uses the custom library. It will use the custom library and a view file (not shown) containing HTML for the sidebar.
File: application/controllers/Main.php
class Main extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('sidebar'); //can also be autoloaded
}
public function index()
{
$data['sidebar'] = $this->sidebar->get_sidebar();
$this->load
->view('banner')
->view('sidebar_view', $data)
->view('main_view')
->view('footer_view');
}
}
Any other controller that needs to show the sidebar would use this same pattern.
This controller loads four different view files and is using "method chaining" which is encouraged. Method chaining executes a tiny bit faster. But the best reason for using it? Less typing.
The method chaning could also be type like this:
$this->load->view('banner')->view('sidebar_view', $data)->view('main_view')->view('footer_view');
But, IMO, putting each ->view() on a separate line makes it easier to read.
You can create a helper for your common tasks. Then create a function for your sidebar and call it where you need it. Check this link for more details about creating helper
You can also create a library for it. Although it will not a very good choice.
create sidebar (view page) and Call model directly inside that sidebar (view page).
secondly call sidebar (view page) directly inside all other view pages.

Codeigniter page cache when using custom _output method

I would like to cache my pages, which could be done by using:
$this->output->cache(n)
As in the documentation, i could use a custom output method in my controller, named _output.
The problem is, when I create this _output method in my controller, it doesn't create any cache file, only displays the output. I looked into the Output core class, and as I see if it's not find any method with _output, it just echo the content.
They have this in the documentation, to use in my _output:
if ($this->output->cache_expiration > 0)
{
$this->output->_write_cache($output);
}
But cache_expiration is not accessible...
You have 2 ways of solving this,
you can edit the core files and make it the cache_expiration to
public instead of protected and mess with the core files (DIRTY WAY)
or you can extend the CI_Output class (BETTER)
Create a file name it MY_Output.php in your application/core the MY_ prefix is found on the $config['subclass_prefix'] option in your application/config/config.php file.,
then put this little piece of code:
class MY_Output Extends CI_Output{
public $the_public_cache_expiration_ = 0;
public function get_cache_expiration()
{
$this->cache_expiration_ = $this->cache_expiration;
}
}
What we are doing is we are extending the CI_Output class and adding our very own method, when the method get_cache_expiration() is called it will assign the cache_expiration to the_public_cache_expiration_, use it on your _output.
test it using this:
public function _output($output)
{
$this->output->get_cache_expiration();
echo '<pre>';
print_r($this->output->the_public_cache_expiration_);//get only our cache expiration
echo '<hr>';
print_r($this->output);//print the whole Output class
}
benefits:
We did no mess with the core files.
we can add more functionality later to our child class.
here is the basic _output method in use
public function _output($output)
{
$this->output->get_cache_expiration();
if($this->output->cache_expiration_ > 0)
{
$this->output->_write_cache($output);
}
echo $output;
}

Load a single module of page in views codeigniter framework

I am developing a web app using codeigniter framework. My application has a fixed header and footer.I want to middle section (ie the body) of my application to load when the user goes to various pages which are available to him and make the header and footer constant.
(hackerrank.com ... I was talking about a website similar to this one ... after login into this website ... the header and the sidebar of this remains constant and they load the remaining page ... how can we implement it using CI framework)
Is there any way through which I can achieve this?
I am performing the following actions which is making me to load the complete website(Its like reloading the entire page :/)
As You can see the following code is present in my template.php
<?php
$this->load->view("templates/header.php");
$this->load->view($main_body);
$this->load->view("templates/footer.php");
?>
and I Controller I write the following piece of code usually
public function load_page(){
$data['main_body'] = 'dashboard_pages/dashboard_view';
$this->load->view('template.php',$data);
}
You were pretty close to the solution: Load the header and the footer as they are, variables:
<?php
$this->load->view($header);
$this->load->view($main_body);
$this->load->view($footer);
?>
The tricky thing, is that you're always writing the method function load_page() in every controller you write, and it would be better having a MY_controller, a class previous to your controller class in which you'll write which footer you're referring:
Check for writing a MY_Controller here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Then, write your MY_Controller:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function load_page( $page )
{
$data['main_body'] = $page;
// Logic for your header or footer deppending on logging or whatever...
if ( 1 ==1 ) {
$data['header'] = "templates/header.php";
$data['footer'] = "templates/footer.php";
}
$this->load->view('template.php',$data);
}
}
and you'll have to make sure your controllers extends MY_Controller class, and add a load_page method to whom you'll pass the argument:
class Custom_page extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load_page( 'dashboard_pages/dashboard_view' );
}
}
I think with that you'll have exactly what you look for, that way you only have to write in one place the logic for the header and footer: In MY_Controller, and just use it in any part.

CodeIgniter adding page doesn't work

I have taken over a project written in CodeIgniter, which I have never used before. I have added a new file to views/pages called features.php, and have read on the internet that to make it accessible, I need to create a function in the controller file that will render the page.
I have tried the following:
public function features()
{
$this->render('template', 'pages/features');
}
However, when I try to open features.php, it gives me 404. How can I fix that?
Update 1 - Class
Here is the controller's class code:
class Pages extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->model('setting_model', 'setting');
$this->load->model('order_model', 'order');
$this->load->model('page_model', 'page');
$this->load->library('form_validation');
$this->load->helper(array('inflector', 'string'));
}
public function index()
{
$settings = $this->setting->get_settings();
$data['document_price'] = $settings->document_price;
$this->render('template', 'pages/index', $data);
}
//This works fine
public function about_us()
{
$this->render('template', 'pages/about_us');
}
//Here is the problem, although it follows the same pattern as about_us()
public function features()
{
$this->render('template', 'pages/features');
}
}
As you are using $this->render I guess you are using the template library. I think you should be using:
public function features()
{
$this->template->set_template('template');
$this->template->write_view('pages/features');
$this->template->render();
}
The php files contained in /views are not directly accessible by typing in some URL. CodeIgniter is an MVC framework. That means that your URLs are mapped to your controllers and the controllers call the views.
What is the name of the class that this function is encapsulated in? Please post the entire class and not just the features() function and we can help you out. If you're working locally, the default mapping to call controllers is: http://localhost/appname/controller/function/param1/param2/etc.
The $this->render() function is not vanilla CodeIgniter syntax, you either inherited a project that is using a templating library, or, there is a sibling render() function inside the controller class.
Check your config/routes.php file as well and consider posting it.
If you want to diagnose the issue, try pinpointing by removing the call to $this->render() and instead using CodeIgniter's native $this->load->view('pages/features') function. If this works, we can be sure it's the library or render() call.

How do I specify to Zend Framework to use a different view file than the action default file?

I have an action, successAction(), it uses the file in my views folder, success.phtml, how do I tell the action that I want it to use the success2.phtml file instead
Use Zend_Controller_Action's render. This would render the view script in controller-name/success2.phtml
class ControllerName_Controller_Action extends Zend_Controller_Action
{
public function successAction()
{
$this->render("success2");
}
}
You should read the docs on Zend Controller's for more.
$this->render('success2');

Categories