What I'm trying to attempt is to display a template on a custom tab.
This is the code of controllers/admin/AdminTController.php:
<?php
class AdminTController extends ModuleAdminController {
public function __construct() {
parent::__construct();
}
public function display() {
//echo $this->l('This is admin my module tab !');
// the echo works.
$this->createTemplate('initial.tpl');
}
}
This is code of /views/templates/admin/initial.tpl:
<p>This is just a test</p>
The result of this code is a blank page.This means the template is not loaded. What am I doing wrong?
Try using the below mentioned code.
<?php
class AdminTController extends ModuleAdminController {
public function __construct() {
parent::__construct();
}
public function display() {
$tpl = $this->custom_smarty->createTemplate('initial.tpl');
return $tpl->fetch();
}
}
?>
Related
This is my code:
class Show
{
public function error()
{
add_action('admin_notices', [$this, 'pip1']);
}
public function pip1() {
echo '
<div class="notice notice-success"><p>test</p></div>
';
}
}
(new Show())->error();
Top code works fine and print errors.
But if I use this code in another class, it doesn't work.
Example:
class Manager extends Controller
{
public function add()
{
(new Show())->error();
}
}
(new Manager())->add();
You should include class file first before call it.
class Manager extends Controller
{
public function add()
{
require_once('path/to/class_Show/Show.php');
(new Show())->error();
}
}
(new Manager())->add();
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 am using hierarchical MVC model in codeigniter. I create a controller called template and inside it a function called sample_template. Then a view called sample_template_v created and call it inside the template controller. I create another controller called Admin and called Template->sample_template($data); inside its 2 funtions.
MY_Controlle.php
<?php
class MY_Controller extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->module('Template');
}
}
Admin.php
<?php
class Admin extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data['content_view'] = 'admin/admin_v';
$this->Template->sample_template($data);
}
function about()
{
$data['content_view'] = 'admin/about_v';
$this->Template->sample_template($data);
}
}
Template.php
<?php
class Template extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function sample_template($data = NULL)
{
$this->load->view('Template/sample_template_v', $data);
}
}
sample_template_v.php file---->
<h5>This is the main Template.</h5>
<?php $this->load->view($content_view); ?>
Error:
If you want to call a method from the object you need to initiaze the object and then call a method. Make sure $this->template is set in your case it isn't.
$this->template = new Template();
$this->template->sample_template($data);
I'm sure this is a simple mistake on my part. I'm trying to demo ci to a friend and we created a simple controller that looks like this:
<?php
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}
And here's what the model looks like:
<?php
class Customerlookup_model extends CI_Model
{
public function __construct()
{
parent::Model();
$this->load->database();
}
public function get_customers()
{
$query = $this->db->get('customer');
return $query->result_array();
}
}
If I try to test this out by doing either:
localhost/myapp/index.php/customerlookup/loadcustomers
or
localhost/myapp/index.php/customerlookup/
nothing happens and no errors appear and no data or messages either. We are using the latest CodeIgniter (2.1.3).
Any suggestion?
i found the problem. php errors were not turned on. there were some syntax problems. checked the apache error log and figured out that the errors were just not being displayed
thanks for the help guys
you are missing with your url helper
$this->load->helper('url');
into your
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
$this->load->helper('url');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}
On Ci you have the possibility to load a view directly from the constructor of your controller, I'm loading the header and footer of my page (since it's the same for each function)
class Add extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->view('header_view');
$this->load->view('footer_view');
}
function whatever()
{
//do stuff
}
}
But this will load the footer view before loading my function, so is there any way to do it without "manually" loading the view at the end of each function ?
I would add the header/footer in the main view with the data, or use a template library (I use this one).
If in main view for function;
// in view for html page
<?php $this->load->view('header'); ?>
<h1>My Page</h1>
<?php $this->load->view('footer'); ?>
You shouldn't be rendering any views in the constructor. CI Controllers should look more something like this:
class Add extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
function index()
{
$this->load->view('header_view');
$this->load->view('home_page');
$this->load->view('footer_view');
}
function whatever()
{
/*
* Some logic stuff
*/
$data_for_view = array(
'product' => 'thing',
'foo' => 'bar'
);
$this->load->view('header_view');
$this->load->view('show_other_stuff', $data_for_view);
$this->load->view('footer_view');
}
}
I've come up with this approach:
class Add extends CI_Controller{
public function __construct()
{
parent::__construct();
// load some static
$this->data['page_footer'] = $this->common_model->get_footer();
}
private function view_loader () {
//decide what to load based on local environment
if(isset($_SESSION['user'])){
$this->load->view('profile_view', $this->data);
} else {
$this->load->view('unlogged_view', $this->data);
}
}
function index()
{
$this->data['page_content'] = $this->profile_model->do_stuff();
// call once in every function. this is the only thing to repeat.
$this->view_loader();
}
}