When 404 Page Not Found error occurs application/errors/error_404.php template is used in CodeIgniter. I need to access the base_url() function inside that template. Any suggestion on how base_url() or site_url() functions can be accessed inside error_404.php will be really helpful.
you might want to use this simple trick. Just change the code below:
<?php echo base_url() ?>
to:
<?php echo config_item(‘base_url’); ?>
credit for https://www.petanikode.com/codeigniter-base-url-404/
this work for my case
Try following in application/errors/error_404.php
$CI =& get_instance();
if( ! isset($CI))
{
$CI = new CI_Controller();
}
$CI->load->helper('url');
echo $CI->base_url();
Try this in top of your 404 page:
$CI =& get_instance();
if( ! isset($CI))
{
$CI = new CI_Controller();
}
$CI->load->helper('url');
echo base_url();
// below here base_url(), site_url() will work
Work for all pages under views/errors/html folder
you can directly use these functions, because these are global functions.
eg: you are in 404 page template
<html>
<head>
<title> 404 Error </title>
</head>
<body>
<?php echo base_url('/your path'); ?>
<?php echo site_url('/your path'); ?>
</body>
</html>
Edited
goto your application/config/autoload.php file and add url in autoload array
$autoload['helper'] = array('url', 'form');
after this you can directly use <?php echo base_url('/your path'); ?> everywhere
Edit your /application/config/routes.php
$route['404_override'] = 'your_controller/error';
Add function into your_controller.php
function error(){
show_404();
}
You are ready to go.
refer https://stackoverflow.com/a/48033542/7840849
or do followings
$autoload['helper'] = array('url'); in application\config\autoload.php file
Then simply use echo config_item('base_url'); instead of echo base_url(); in codeigniter default error pages
$autoload['helper'] = array('url'); in application\config\autoload.php file
Then simply use echo config_item('base_url'); instead of echo base_url(); in codeigniter default error pages
this work for my case, try this method
Change the function base_url() to config_item('base_url')
<?php echo config_item('base_url'); ?>
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Check if you have something configured inside the config file /application/config/config.php e.g.
$config['base_url'] = 'http://example.com/';
**Then**
u can use like this..
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
Related
I have a problem with dompdf in PHP, in my template file dompdf does not execute my <?php ?> code. Do you have any ideas?
Template.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
BEFORE
<?php echo "PHP CODE"; ?>
AFTER
<?php echo $var1; ?>
PHP code
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/vendor/dompdf/dompdf/lib/html5lib/Parser.php';
require_once dirname(__DIR__) . '/vendor/dompdf/dompdf/src/Autoloader.php';
Dompdf\Autoloader::register();
$var1 = "TEST1";
var_dump($var1);
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isPhpEnabled', TRUE);
$dompdf = new Dompdf($options);
$dompdf->loadHtml( file_get_contents(dirname(__DIR__) . '\template.php') );
$dompdf->render();
$output = $dompdf->output();
file_put_contents(dirname(__DIR__) . '/data/' .'Output.pdf', $output);
PDF output
BEFORE AFTER
....
Info
dompdf/dompdf version ^0.8.2
PHP 7.2.12
Thanks C,
The problem was the file_get_contents it only takes the content of a file, it do not evaluate the content of the file (fo ex: <?php code?>).
I have to add to my code a buffering mecanics:
ob_start();
include $template;
$contents = ob_get_clean();
$dompdf->loadHtml($contents);
C
Can someone tell me if this code is codeigniter 3.0 compatible ?
If not, how should it be formatted?
if ( ! function_exists('get_site_url'))
{
function get_site_url($data){
$CI =& get_instance();
//$data = '';
//echo base_url(); exit;
$data =str_replace('{SITE_URL}',base_url(),$data);
return $data;
}
}
SITE_URL is constant that you define using define('SITE_URL','value').So no need quotes ''.Try like this..
if ( ! function_exists('get_site_url'))
{
function get_site_url($data){
$CI =& get_instance();
//$data = '';
//echo base_url(); exit;
$data =str_replace(SITE_URL,base_url(),$data);
return $data;
}
}
In order to use base_url() don't forget to load url helper in
application/config/autoload.php
As you pass the $data['common_row'] into the view section
$this->load->view('home_view',$data) the data has been converted into a array is not object so when you are trying to get the data in the view you can try something like this <?php echo get_site_url($common_row['services']);?> or <?php echo get_site_url($common_row[0]['services']);?> depending upon the result.
You can debug the code and see the actual value by
print_r($common_row); die(); on the view page.
I am using Gantry 5 template and I want to add php code in the header. I set some variables using php and access them in the script tag. For example
<?php
$id = $user->id;
$name = $user->username;
?>
<script>
var id = '<?php echo $id; ?>';
var name = '<?php echo $name; ?>';
</script>
I want to add the above code in head of Gantry 5 template. In we just add the code in index.php but this mechanism is different in Gantry 5. How this code can be added?
I have a trouble with CodeIgniter.
I would like to get (recoup) data from a helper that I call in a view.
I will explain with the code
Here's my view :
// CALL THE FUNCTION FROM THE HELPER
<?php recup_email(); ?>
// DATA RECOUP FROM THE HELPER
<?php foreach($em as $e): ?>
<?php echo $e->email_membre; ?>
<?php endforeach; ?>
As you can see, I call the function and just after I would like to use data that I had recoup.
Here's my helper :
function recup_email()
{
$CI =& get_instance();
$CI->load->model('unite_model');
$data['em'] = $CI->unite_model->email_membre_model();
}
But I don't know how to recoup data without using
$layout_network['contenu'] = $this->load->view('list_vote_friend', $data, TRUE);
$this->load->view('layout_network',$layout_network);
because the view is already loaded.
I hope you understand, sorry for my bad english.
Many thanks.
In the example you have given, why not just return the data from the helper function?
function recup_email()
{
$CI =& get_instance();
$CI->load->model('unite_model');
$membres = $CI->unite_model->email_membre_model();
return $membres;
}
So that is you view;
// CALL THE FUNCTION FROM THE HELPER
<?php $em = recup_email(); ?>
// DATA RECOUP FROM THE HELPER
<?php foreach($em as $e): ?>
<?php echo $e->email_membre; ?>
<?php endforeach; ?>
Here is my function:
function single_resonator() {
$data['title'] = 'Single Resonator Operating Parameters';
$data['main_content'] = 'products/single_res_view';
$this->load->view('templates/main.php', $data);
}
But the single_res_view doesn't load if it is in the products folder in the views folder. How do I accomplish this? I tried a file called MY_router.php from a post elsewhere on this site and it didn't help. CI will only load the single_res_view if it is in the root of the views folder.
Here is templates/main.php
<?php $this->load->view('includes/header.php'); ?>
<?php $this->load->view('includes/nav.php'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('includes/footer.php'); ?>
Have you tried this:
$data['main_content'] = 'products/single_res_view.php';
If not, can you triple check that the path and file names are correct? Otherwise, can you echo the value of $main_content just in case you're overwriting it somewhere?