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';
Related
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 to CodeIgniter, and I don't know whether this is possible or not. How can I link view to view without the help of controller just like PHP.
{ <a href=''>contact.php</a> }
I tried base_url(), site_url() and current_url() but this error displays:
You don't have permission to access /Buildon/User/views/contact.php on this server.
When your using codeIgniter site url or base url etc. You should send them to the controller.
CodeIgniter Doc's http://www.codeigniter.com/docs
URI Routing: http://www.codeigniter.com/user_guide/general/routing.html
URL Helper: http://www.codeigniter.com/user_guide/helpers/url_helper.html
Lets say
Contact Us
The contact_us in the base_url would be controller name.
Or Example
Example
Example
If you try to send the link to view file will not work.
Incorrect
Example
Copy this file in root of this folder parallel to index.php and then you can access it.
I am new to CodeIgniter and having trouble with link formation. In my page, I have a link to another page set to:
<li>Feed Page</li>
Here the feed is one of my controllers, but the link is showing as:
http://localhost/BusinessPad/localhost/BusinessPad/feed,
which doesn't actually exist. I can't understand how this happened; I have made sure: $config['index_page'] = ''; and I added an .htaccess file. If I leave $config['base_url']='', the base URL is still not working for me.
To deal with anchor tag, You can have like this
Feeds
You shall put the base_url inside your anchor tag and then your controller name.
Then you will get the url like localhost/BusinessPad/feed which you expect.
Note : Make sure you have loaded the url helper by $this->load->helper('url'); or loaded in autoload itself
Feed Page
if this not works use this
Feed Page
Explain :
<?php echo base_url();?>//URL
BusinessPad//Controller
feed//Function
in 1. this will give your site URL. http://localhost/BusinessPad/localhost/. if you host this to live server it will come with tour domain name.
then in 2 it will call your controller which Contains your functions.
Note: if you juct call your controller it will call automatically public function index() if you create.
then in 3 it will redirect to your function which you exactly need.
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
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