I routed my controller function and suddenly the function name is added to base URL, how can I remove this?
$route['editblog/(:num)']='Code/editblog/$1';
Then all of the sudden edit blogs are added to base URL and my external files are showing an error.
<script src="assests/vendor/slick/slick.min.js">
It shows:
<script src="editblog/assests/vendor/slick/slick.min.js">
Please help me, help will be highly appreciated!
You need to verify 1 things:
application/config.php $config['base_url'] = 'application/path/'; like http://example.com/ or http://localhost/applicationName/
Now you should use urls like
base_url('assets/js/filename/js');
base_url('controller/action/parameters');
In this way, it will generate correct urls.
You should change your URL's to:
<script src="<?php echo base_url('assests/vendor/slick/slick.min.js'); ?>">
Actually base_url is set in your applications/config.php file. -> config['base_url']. It MUST include trailing slash
in your config file:
$config['base_url'] = 'http://test.com';
when you call out the base_url(). You can use this :
<script src="<?= base_url().'/assests/vendor/slick/slick.min.js'; ?>">
In general when we use site_url() in anchor tag in php code it auto insert "index.php" before the controller like
/index.php/controllerName/methodName
But in base_url it doesn't insert "index.php".
I want to add auto index.php before the controller how site_url() works.
Already I searched and understood that the change will be in config file.
But want to know the specific answer what I need to change.
Base URL should be absolute, including the protocol:
$config['base_url'] = "http://somesite.com/somedir/";
If using the URL helper, then base_url() will output the above string.
Passing arguments to base_url() or site_url() will result in the following
$config['index_page'] = "index.php";:
echo base_url('assets/stylesheet.css'); // http://somesite.com/somedir/assets/stylesheet.css
echo site_url('mycontroller/mymethod'); // http://somesite.com/somedir/index.php/mycontroller/mymethod
I have the following code in a view for my codeigniter application:
<link href="<?php echo APPPATH.'assets/css/bootstrap.min.css'?>" rel="stylesheet">
This is giving me a 404.
However, a few lines down, I tried this to troubleshoot:
<?php echo APPPATH.'assets/css/bootstrap.min.css' ?>
That shows the following path on my webpage:
/var/www/html/testapp/application/assets/css/bootstrap.min.css
If I copy that path and paste it into a new browser window, it returns the contents of my css file.
I'm not sure where I've gone wrong.
Tips?
Try
<link href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>" rel="stylesheet">
(In response to the helpful comment)
base_url() requires the URL Helper to be loaded which can be done one of two ways.
Loaded in a controller
$this->load->helper('url');
Or, set to autoload in /application/config/autoload.php
$autoload['helper'] = array('url');
If the path you get printed by the 2nd line you've given is correct, then it might be the slash at the beginning. So instead of /var/... etc. try make sure it's var/... etc. Solution to your problem might be then as follows:
<?php
$str = APPATH.'assets/css/bootstrap.min.css';
echo substr($str, 1);
?>
The second variable at the substr is the startposition of the new string, so that should get rid of the slash at the beginning. Hope this helps!
Why do the paths to my css,js and img files have the CONTROLLER name BEFORE 'application'?
Path from firebug:
localhost/quote-generator/admin/application/assets/bootstrap/css/bootstrap.min.css
My code:
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>" >
This is from a header.php file i have included in one of my views generated in my Admin controller in the 'login' function.
Any suggestions??
Its bad practise to keep assets files, i.e. css/images in the application folder
Your Codeigniter folder structure should be this
root
|
|_______________application
|_______________assets
|_________css
|_________images
considering the root is the admin
setting the above structure, set the base_url like this
$config['base_url'] = 'localhost/quote-generator/admin/';
then put the href like this
href="<?php echo base_url();?>assets/bootstrap/css/bootstrap.min.css"
use :
site_url()
instead of
base_url
it's saver that way
In my web application using codeigniter. I am trying to use base_url() function but it shows empty results. I have also used autoload helper through autoload file, but then too it doesn't seem to work. Also I had defined base constants but all in vain.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
<script type="text/javascript">
//<![CDATA[
base_url = '<?= base_url();?>';
//]]>
</script>
</head>
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67):
$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();
Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:
If this (base_url) is not set then CodeIgniter will guess the protocol, domain and path to your installation.
application/config/config.php, line 13
If you want to use base_url(), so we need to load url helper.
By using autoload $autoload['helper'] = array('url');
Or by manually load in controller or in view $this->load->helper('url');
Then you can user base_url() anywhere in controller or view.
First of all load URL helper. you can load in "config/autoload.php" file and add following code
$autoload['helper'] = array('url');
or in controller add following code
$this->load->helper('url');
then go to config.php in cofig folder and set
$config['base_url'] = 'http://urlbaseurl.com/';
hope this will help
thanks
Check if you have something configured inside the config file /application/config/config.php e.g.
$config['base_url'] = 'http://example.com/';
I think you haven't edited codeigniter files to enable base_url(). you try to assign it in url_helper.php you also can do the same config/autoload.php file. you can add this code in your autoload.php
$autoload['helper'] = array('url');
Than You will be able to ue base_url() like this
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
Apart from making sure you have set config/autoload.php:
$autoload['helper'] = array('url');
Change application/config/config.php from:
$config['base_url'] = 'http://example.com/';
Become a dynamic base url:
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https": "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
And using the host from php to run it on local, below is just an example port.
php -S localhost:2000
If you don't want to use the url helper, you can get the same results by using the following variable:
$this->config->config['base_url']
It will return the base url for you with no extra steps required.
Load url helper in controller
$this->load->helper('url');
Anything if you use directly in the Codeigniter framework directly, like base_url(), uri_string(), or word_limiter(), All of these are coming from some sort of Helper function of framework.
While some of Helpers may be available globally to use just like log_message() which are extremely useful everywhere, rest of the Helpers are optional and use case varies application to application. base_url() is a function defined in url helper of the Framework.
You can learn more about helper in Codeigniter user guide's helper section.
You can use base_url() function once your current class have access to it, for which you needs to load it first.
$this->load->helper('url')
You can use this line anywhere in the application before using the base_url() function.
If you need to use it frequently, I will suggest adding this function in config/autoload.php in the autoload helpers section.
Also, make sure you have well defined base_url value in your config/config.php file.
This will be the first configuration you will see,
$config['base_url'] = 'http://yourdomain.com/';
You can check quickly by
echo base_url();
Reference: https://codeigniter.com/user_guide/helpers/url_helper.html
Question -I wanted to load my css file but it was not working even though i autoload and manual laod why ? i found the solution =>
here is my solution :
application>config>config.php
$config['base_url'] = 'http://localhost/CodeIgniter/'; //paste the link to base url
question explanation:
" >
i had my bootstrap.min.css file inside assets/css folder where assets is root directory which i was created.But it was not working even though when i loaded ?
1. $autoload['helper'] = array('url');
2. $this->load->helper('url'); in my controllar then i go to my
I encountered with this issue spending couple of hours, however solved it in different ways. You can see, I have just created an assets folder outside application folder. Finally I linked my style sheet in the page header section. Folder structure are below images.
Before action this you should include url helper file either in your controller class method/__constructor files or by in autoload.php file. Also change $config['base_url'] = 'http://yoursiteurl'; in the following file application/config/config.php
If you include it in controller class method/__constructor then it look like
public function __construct()
{
$this->load->helper('url');
}
or If you load in autoload file then it would looks like
$autoload['helper'] = array('url');
Finally, add your stylesheet file.
You can link a style sheet by different ways, include it in your inside section
-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />
-> or
<?php
$main = array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
);
echo link_tag($main); ?>
-> or
finally I get more reliable code cleaner concept. Just create a config file, named styles.php in you application/config/styles.php folder.
Then add some links in styles.php file looks like below
<?php
$config['style'] = array(
'main' => array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
)
);
?>
call/add this config to your controller class method looks like below
$this->config->load('styles');
$data['style'] = $this->config->config['style'];
Pass this data in your header template looks like below.
$this->load->view('templates/header', $data);
And finally add or link your css file looks like below.
<?php echo link_tag($style['main']); ?>
This All Directly Access Function Will Be Loaded Through Helper Class Only.
Like URL, Security, File all Are Helpers and You can Also Load Custom Helpers.
config/autoload.php
$autoload['helper'] = array('url', 'file', 'authorization', 'custom');