I am trying to use baseurl() in my application, but it's not working. I've attached two photos, and . Can anybody help me solve this?
$config['base_url'] = 'http://localhost/class-19/';
hiii...Please replace baseurl() to 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();
Related
I did same as described in Verify email with codeigniter and all work seems ok from my side, but it only works when I give all the direction like: http://localhost/site_name/controller/function_name/parameter the parameter is my random code, but when I learn codeigniter it says that the links to a function we put in like this:
echo site_url('controller/function_name/parameter')
When I do this, the link doesn't work.
It looks like you need to load the url helper in your code.
Either add it system wide in the file:
application/config/autoload.php
$autoload['helper'] = array('url');
or you can define it in your controller:
$this->load->helper('url');
But just in case please ensure that you have the base_url parameter set up in your appliation/config/config.php
$config['base_url'] = 'http://localhost/site_name/';
I want to have nice good looking error pages. For this I need to get some CSS and JS files. But for some odd reason using base_url() does not work on the error_pages. I could of course just use href="/css/style.css" and tell it to get it from the root folder. But the website could very well be put in a different folder than the root folder. So using the / is not an option.
So my question now is why doesn't base_url() work on an error page? I have autoloaded it so shouldn't it be working?
This is what I tried when I was trying to get the base_url() from the error_404 page in the view.
In my autoload.php I have included the helper url
$autoload['helper'] = array('url', 'form');
And in my error_404 page (application/views/errors/html/error_404.php) I am echoing the base_url like so:
<?php echo base_url(); ?>
This is the error I'm getting:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined function base_url()
Filename: /usr/local/www/example.com/application/views/errors/html/error_404.php
Line Number: 37
Backtrace:
File: /usr/local/www/example.com/index.php
Line: 315
Function: require_once
I'd rather not make any changes in the system folder since I don't want to redo the process every time I update Codeigniter folder.
System:
Codeigniter 3.0.6
PHP 7.0.8
Nginx 1.11.1
UPDATE:
I'd like to call base_url on every error page (db, 404, general, php, etc.).
The autoloader does not work because the autoloader loads the libraries after the error checks are finished. If there are errors like a 404 not found it will view the custom error page without loading the autoloader.
To get your base_url on a custom page you would need to add the following in your custom error page:
$base_url = load_class('Config')->config['base_url'];
Taken from:
http://forum.codeigniter.com/thread-65968-post-335822.html#pid335822
You must load the helper class to get access url functions. So, try to do first:
$this->load->helper('url');
Then you can echo it using
echo base_url();
alternatively, you can get the same result with
$this->config->config['base_url'];
In order to do this ,You must override the error pages in your routes.php file something like
$route['404_override'] = 'controller/method';
And inside method() function,You can access base_url() after loading uri helper as same as normal codeigniter controller.
$this->load->helper('uri');
echo base_url();
if we need to change the design of other php/db error pages.we can edit the following files
I am using XAMPP, MySQL and CodeIgniter.
I already included,
$autoload['helper'] = array('url');
in autoload.php.
<?php echo base_url(); ?>
returns http://::1/mysite/ instead of http://localhost/mysite/.
Can anyone help me with this issue?
You need to make sure you have set your base_url in. Most likely because you left it blank. This is a common thing that people do not know about or forget to do.
application > config > config.php
$config['base_url'] = 'http://localhost/mysite/';
One of the issues when you try to submit form with out base url not set is will not go to correct url, when you try to submit a form so that why in codeigniter 3 all ways best to set your base_url even though not a requirement.
Also here are some htaccess for codeigniter xampp & wamp
Htaccess For Codeigniter
I found it is unusual with the latest stable version of CI (3.03),
I tried echo base_url() and it returns some unusual thing,
**Note: ** I have loaded the URL helper .
The following code
$this->load->helper('url');
echo base_url();
returns http://127.0.0.1/ (I have used http://localhost/ to interpret the application) in Firefox.
And:
http://::1/ (Same I have used http://localhost/ ) in Chrome ?
Help me guys in fixing this.
Please remove base_url form the config file if you set there,
$config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/";
In your config, use define. like below:
define('base_url', 'http://localhost/your_project name/');
And then try
echo base_url() ;
Make sure you have set a base url
$config['base_url'] = 'http://localhost/project/';
Some times for me that is issue.
In older versions of CI you use to be able to leave that blank but.
Now it is recommended that you do not leave that base_url blank.
And I would auto load the url helper as it is the most common one.
I've an application working since one year, now my client is asking to make a folder outside the App to place the contracts (PDFs) in.
The point is that the app is fully working and has many calls to the base_url() function, so I cannot change the config file $config['base_url'] variable or my app will stop working.
So basically I need to go to the parent folder of my current app.
Let's say the base url is at www.mysite.com/a/app/ I need to go into the www.mysite.com/b/ folder.
My first idea was:
<?= base_url('../b') ?>
But of course does not work. Any idea?
Try this code:
echo base_url(array('.', '..', 'b'));
OR
echo base_url('./../b');
Try this:
echo base_url() . '../../b/';
work for me
back to one folder;
dirname(base_url());
back to two folder ;
dirname(dirname(base_url()));