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
Related
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'; ?>">
i'm using Codeigniter for my website. I had add url_suffix in config
$config['url_suffix'] = '.html';
Normal i create $href with same below code
echo "<li>Products</li>";
I must add .html in last. How can i auto add .html in url but i don't need fill it in last
You should use site_url()
Your href should look like this site_url('controller/method');
You should use site_url() function for auto generate of URL in codeigniter
Use this:
echo "<li>Products</li>";
Instead:
echo "<li>Products</li>";
this is my code below.
<li>Why share?</li><li>
But after I apply this i manage to redirect to the page but when i try to press another page the url will be keep adding like this and cause page not found.
The page look like this
But when i try to link to home page, the url won't change back to /home only
Any solution? or another way to link the page?
Need help on this!
Thanks!
Your href should have a slash in front of it so that it goes to "root".
<li>Why share?</li><li>
If not, the browser will think it is relative to the current route. Or use Codeigniter's built-in site_url() function
<li>Why share?</li>
Read about relative/absolute here: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/
You need to set your base_url() in config.php, and call the url_helper so that you can then use it.
Step by Step Instructions:
in application/config/config.php, set your base_url, I prefer to use something like this:
$config['base_url'] = 'http://' . $_SERVER['SERVER_NAME'] . '/';
in application/config/autoload.php, add url helper:
$autoload['helper'] = array('url');
Use it in your views like this:
<a href="<?= base_url('about/why') ?>" > link </a>
Read this: https://www.codeigniter.com/user_guide/helpers/url_helper.html
I am new in CodeIgniter.
I setup CI in my localhost Eg. localhost/MyProjects/CodeIgniter/welcome/index
I can able to remove the index.php from the url & I also completed the .html suffix
So, I can able to visit localhost/MyProjects/CodeIgniter/welcome/index.html & Its working properly.
So I am using my a tag like this:
Register
But, I dont want to use the .html in my link manually.
have any solution to show my visitor .html suffix automatically Eg. localhost/MyProjects/CodeIgniter/welcome/index.html Or localhost/MyProjects/CodeIgniter/welcome/about.html Etc
Here is my htaccess http://pastebin.com/cXUFjvbp
Just edit url_suffix in config.php .....
$config['url_suffix'] = '.html';
Use site_url()
<!-- No need .html -->
Register
<!-- Register -->
Or anchor()
// No need .html
echo anchor('welcome/register', 'Register');
// Register
Note : Dont forget to load URL helper with $this->load->helper('url');.
Change your config.php file
$config['url_suffix'] = '.html';
Just redefine the base_url function or make a new one. Look at the source of base_url method
function _base_url($path = NULL) {
return base_url() . $path . '.html';
}
so basically when i echo the codeigniter function base_url() in the href attribute of an anchor tag, it appears to echo it out twice. Example:
somelink
and the above, if you inspect it your chrome browser shows this:
somelink
"mysitedomainname.com" is just a name i made up for this example. Any reason why this is happening?
There are three reasons I'm aware about that can cause this.
The first one is when something wrong is written in config.php on line 17 $config['base_url'] = ''; - it better be left empty, just like when you download CI.
The second one is if you have set $config['base_url'] value to something without prefixing it with http:// or other protocol.
The third one is if you have set base href somewhere:
<base href="http://www.mysitedomainname.com/" />
When you need to link to some other page, you should use site_url(), base_url() can be used to link stylesheets, js, img src attributes and other real URL's. The reason is pretty simple, base_url() does not include the index_page value set in config.php.
try this
make this
$config['base_url'] = "http://www.mysitedomainname.com"
into this
$config['base_url'] = ""
in config.php
It will work fine if you use
somelink