Codeigniter URL Overlapping. Not working properly - php

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

Related

how to add index.php in codeigniter with every anchor

I have developed a shopping cart and have removed index.php using .htaccess file but now my client is asking me to show him the site on free web hosting first i have tried 000webhost but they do not support .htaccess file hence i moved to orgfree.com it also dont support .htaccess file than i remove .htaccess file and it created a problem because now i need to put index.php infront of every url but now i am consfuse that how can i add index.php with every achor doing this with every link manually will be a very huge process because the project is so big so there might be a solution for adding index.php to every anchor in codeigniter .
Here are some setting i have done so far please suggest me or help me adding index.php to every anchor
In Config.php
$config['index_page'] = 'index.php';
Before my anchor were like that
<li class="active">Home</li>
But after removing .htaccess file i need to put index.php with every anchor manually which is headace and i cant do it please show me the shortest path to add index.php with every link or anchor.
<li class="active">Home</li>
Both Nicolas and J Young are correct. you should use URL helper to avoid any trouble in the future. But since you want to a quick-fix, how about fixing link urls via JS
i'll give you a jquery example,
$(document).ready(function(e){
$('a').each(function(){
var newUrl = 'index.php/'+$(this).attr('href');
$(this).attr('href', newUrl)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
you link
Include above code into your views
I think you now have to change it mannually, but start using Codeigniter functions in URL Helper.
Take a look at base_url() and site_url() they are very usefull when you need to be prepared for changes like yours.
For example, in your code you can put:
<li class="active">Home</li>
or
<li class="active">Home</li>
In config set your base_url to this
$config['base_url'] = "your_site_url/index.php/";

URL base changing after using any controller with parameters

i'm having an problem with the URL of Codeigniter.
When i use one controller with parameter, example: localhost/ci/products/news (where localhost/ci is the url base), after i click in any element <a />, example: <a href="home"/> Codeigniter redirects for localhost/ci/products/home and no for localhost/ci/home.
Anyone know why this happens?
You must use a base_url for links. Right now your anchor has a relative path in the href="home", you should have full path in your links, when u use mod_rewrite for nice urls.
like
Home

CodeIgniter controller to view to another controller url

Say for example, I loaded a view from controller1
The url should be index.php/controller1/<function where view is ran>
Then from that view I have a link.
<a href='controller2/view'>View</a>
After clicking that link, the url now looks like this.
index.php/controller1/controller2/view
Is there a way to clear previous url_segment, or is there a better way to link to another controller's function.
Do.
<?=base_url(); ?>/controller2/view
Inside your href.
You can write the link as
<a href='<?=base_url(); ?>controller2/view'>View</a>
remember that you have set the base_url in your config/config.php file
$config['base_url'] = 'http://yourSite';

Codeigniter Redirect not working

Let I am in following location
http://localhost/ignitershop/index.php/seller_controller/viewcart
And here in this view I have a link and using it i want to move to another method of the same controller called removeRow() . So I am using href as below
<a href="seller_controller/removeRow" >CLICK TO REMOVE </a>
So I expect the new url to be :
http://localhost/ignitershop/index.php/seller_controller/removeRow
but the url seems to be concatenating. And it is becoming something as below :
http://localhost/ignitershop/index.php/seller_controller/seller_controller/removeRow
That is seller_controller is coming twice. I am facing such type of concatening problem using redirect also. So I need to know what it is the best way for switching method of the same controller. Any good solution ???
Try like
<a href="<?php echo site_url('seller_controller/removeRow');?>" >CLICK TO REMOVE </a>
Or you can also try like
<a href="<?php echo site_url().'seller_controller/removeRow';?>" >CLICK TO REMOVE </a>
You cont use base_url() Because it doesnt included the index page (May be the index.php ) where site_url() has the combination with base_url() and index url.
Use ob_start(); at the begging of the script.
This is happening, because your link is relative - i.e. it is added to the current URI minus the last part. Think of it like directories and files - the last part (viewcart) is a file, and the rest is a directory. A relative link means "Hey, take me to this file (removeRow), but start delving from the current directory".
This means that if you have http://localhost/ignitershop/index.php/seller_controller/viewcart and there you visit a link with content Link, it will send you to http://localhost/ignitershop/index.php/seller_controller/one/two/three
To fix it, use base_url() or site_url() functions from the URL helper accordingly, which will do the job for you. Check out the documentation for more info.
Try to use the absolute path by using either site_url() or base_url()

URL with trailing slashes leads to different pages - MVC CI

I have a routed like the following in my Code Igniter route file.
$route['profile'] = "profile";
The class profile contains the following
$this->load->view('pages/profile.php');
Now I have a link on my profile.php page which looks like the following
<a href = 'logout'>Logout</a>
Now consider a user using it. If he visits the following url
localhost/project/profile
Then now the link on the profile.php page would lead to the following
localhost/project/logout
But if the user uses this url
localhost/project/profile/
That is if there is a trailing slash then the link on the page would lead to
localhost/project/profile/logout
And now my question is what should I do to lead the link in both the cases to
localhost/project/logout
Hope I am clear. Please help me out
I think that you're searching for this, load the url helper on your controller with this code line
$this->load->helper('url');
On your view whenever you want to echo URL's use this code
//this will echo something like this http://(yourdomain).index.php/logout
<a href = '<?=site_url("logout")?>'>Logout</a>
If you want let's say, an url to another controller you'll use something like this
//this will echo something like this http://(yourdomain).index.php/(anothercontroller)/logout
<a href = '<?=site_url("anothercontroller/logout")?>'>Logout</a>
More information about the url helper from codeigniter can be found here:
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

Categories