My controller code is :
class Tconfig extends CI_Controller {
public $config;
public function __construct($schoolId = 1) {
parent::__construct();
$this->load->database();
$this->load->model('Tconfig_model');
$this->config = $this->Tconfig_model->load_config($schoolId);
}
public function config() {
$data['config'] = $this->config;
$this->load->view('templates/user_header', $data);
$this->load->view('templates/user_menu', $data);
// printf("%s", base_url());
//$this->load->view('config', $data);
//$this->load_view('templates/user_footer', $data);
}
}
I have autoloaded url_helper
At this point, I am just trying to load a HTML file which includes a call to base_url()
The exact line where this function is called is :
<link href=<?php echo base_url();?>"assets/bootstrap337/css/bootstrap.min.css" rel="stylesheet">
When I run this code through a debugger, I get this
What I cannot figure out is why I get a call to an undefined method stdClass::base_url() in url_helper.php
I am pretty sure that it's something very obvious, but it's been a long day .. TIA!
You have to load url helper in application->Config->autoload.php file
Like this:-
$autoload['helper'] = array('url');
OR
Add this in construct function in controller
$this->load->helper('url');
The problem is - you overwrite the CI intern Variable config with your own - you can avoid this by renaming your variable
something like the following should work
class Tconfig extends CI_Controller
{
public $tcConfig;
public function __construct($schoolId = 1)
{
parent::__construct();
$this->load->database();
$this->load->model('Tconfig_model');
$this->tcConfig = $this->Tconfig_model->load_config($schoolId);
}
public function config()
{
$data['config'] = $this->tcConfig;
$this->load->view('templates/user_header', $data);
$this->load->view('templates/user_menu', $data);
}
}
use this in your header file.
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
check whether you defined your base url in your config.php file. If not, define it like-
$config['base_url'] = 'your-base-url';
in your config.php file in application/config folder.
Related
<?php
class MyModel extends CI_Model {
public function loadData()
{
$CI =& get_instance();
$CI->load->helper('data_helper');
print_r($CI->data_helper); //this is printing nothing
$CI->data_helper->loaditems(); // method is not calling
}
}
function loaditems()
{
echo "hello from load of helper";
}
?>
helper filename is data_helper.php
give me you thought about this why it not working and in which case it will work
Put the file data_helper.php in the /application/helpers directory.
In /application/config/autoload.php load the helper using just the word 'data'. (line 92).
$autoload['helper'] = array('data');
Or you can load it before you need it with $this->load->helper('data');
Then you can use loaditems() from anywhere like a normal function.
You don't need the $CI magic at all.
According to the documentation
$this->load->helper('name');
Where name is the file name of the helper, without the .php file extension or the “helper” part.
which means the following code should work
class MyModel extends CI_Model
{
public function loadData()
{
$this->load->helper('data');
loaditems();
}
}
you can read more about it here
Try data_helper() to call helper function.
data_helper();
I have an app using CI 1.7.2.
In controller i have $data['title'] = 'Sample Title';
In view
<?php echo $title; ?>
Should print it.
But not working.
I have another app with latest CI, in that passing like this works fine.
Anybody know reason for this strange issue?
You should pass the data to view like,
$this->load->view('view_file', $data);
There is another way to pass data to view use the library parser
class home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('parser');
}
public function index() {
$this->sssssssss();
}
function sssssssss($data = '') {
$header = $this->parser->parse('interface', array(), TRUE);
$data['interface'] = $header;
//bind all together and parse to template view
$this->parser->parse('template_view', $data);
}
}
I am using HMVC with CodeIgniter.
I have this in my testmodule controller:
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
And this in my view template.php of that controller that is loaded by this controller:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>
but, when I var_dump($main_content) in the view and die() it shows null instead of frontpage
How, come? I don't get it at all.
If you want to use $this->view_data you have to declare $view_data as a property first (at the top of your controller):
class TestModule extends CI_Controller
{
public $view_data = array();
public function index()
{
// Now you can use $this->view_data in this function:
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}
i try to use custom 404 page from this tutorial http://maestric.com/doc/php/codeigniter_404
my controller error.php :
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
echo "error bro";
}
}
when i open link localhost/mading/admin/blablabla , where there is not function blablabla() in controoler admin. the output : “error bro”
i try change line in method error_404() become the code below,
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
$data['title'] = "404 Page Not Found";
$data['body'] = $CI->load->view('web/404','',true);
$CI->load->view('web/web_page',$data);
}
}
but, when i open link localhost/mading/admin/blablabla , where there is not function blablabla in controller admin. the output : blank page.
the function error_404 not load the view . why the 404 page not load when i open controller admin/blablabla ??
thanks
Reading the comments of that tutorial, many people are having the same problem with that code. Did you change parent::CI_Router() on line 10 to parent::__construct()?
But why not just set the $route['404_override'] variable?
Just set a proper route in your config/routes.php:
$route['404_override'] = 'your_controller/show_404';
And the corresponding controller would look like that:
class Your_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
function show_404() {
$this->output->set_header("HTTP/1.1 404 Not Found");
$this->load->view ('common/errors/404'); //wherever your view is
}
}
That should do the trick for you.
I'm a codeigniter beginner. I'm using version 2.0.2 with a local server (php 5.3) and I have a start controller with code like this (just for testing obviously):
<?php
class Start extends CI_Controller
{
var $base;
function _construct()
{
parent::_construct();
$this->base = $this->config->item('base_url');
}
function hello()
{
$data['base'] = $this->base;
print_r ($data);
}
}
When I navigate to the hello function the $data['base'] array element is empty. Why should that be when the construct function has populated it with 'base_url' from the config file?
Seems that the variable $base is not available outside the construct function but I can't understand why or how to fix. Can anyone advise please?
Your constructor should be __construct() (2 underscores).
function __construct()
{
parent::__construct();
$this->base = $this->config->item('base_url');
}
Also, as other people have mentioned, if you load the 'url_helper', you can get the base_url by calling base_url().
$this->load->helper('url');
$this->base = base_url();
Did you know you can do
$this->load->helper('url');
$base = base_url();
Or even in the view:
<?php echo base_url(); ?>
Use it like this:
class Start extends CI_Controller
{
private $base = '';
function _construct()
{
parent::_construct();
$this->base = $this->config->item('base_url');
}
function hello()
{
$data['base'] = $this->base;
print_r ($data);
}
}
Or in the autoload.php configure:
$autoload['helper'] = array('url');
and then you can use base_url(); everywhere in your code.