Krish
Krish

Reputation: 421

Issue with Codeigniter url

I'm new to codeigniter and I'm going through the tutorials. I created a new controller and when I try to create a link as shown below its throws an error.

<p><a href="dashboard">Dashboard</a></p>

However when I change the link as shown below everything works fine.

<p><a href="<?php echo base_url(); ?>dashboard">Dashboard</a></p>

Is this how things work in Codeigniter? Is there a way I can build links without using base_url everytime?

P.S. For now I don't care about the index.php in the link.

Upvotes: 0

Views: 44

Answers (2)

Tpojka
Tpojka

Reputation: 7111

Most likely, issue is because of using relative URLs. For example, those won't work when you are using deeper level URI segments. Consider next few examples:

If base of your application is http://www.example.com/ which you should set in config file too by setting $config['base_url'] = 'http://www.example.com/';, when you are on that URL with your browser, and when you click on some relative URL you will be pointed/redirected to URL that is current URL appended by relative link.

http://www.example.com/ => http://www.example.com/dashboard

but if you are in some deeper segment, let's say http://www.example.com/index/15, dashboard will be extending that one although you would like it points to http://www.example.com/dashboard

http://www.example.com/index/15 => http://www.example.com/index/15/dashboard

As other said, you should set and use base_url() CodeIgniter inbuilt function (use appendix as parameter of function base_url('dashboard') ). If you would like to use it without base_url('some-page') function, you should use either absolute URL either relative to server route one. The latter one would look like as with prepended slash that is telling web server to use root location defined in .htacces file or virtual host environment settings /dashboard. In your former example

<p><a href="/dashboard">Dashboard</a></p>

should work just well. If it is not working you need to set RewriteBase attribute in .htaccess file. Check this comment here and check some articles that describe differences, pro and cons between relative and absolute URLs

Upvotes: 0

user4419336
user4419336

Reputation:

You could try like in your head area use base https://www.w3schools.com/tags/tag_base.asp

<head>
   <base href="<?php echo base_url();?>">
</head>

Then you should be able to use like

<p><a href="dashboard">Dashboard</a></p>

Make sure you have set your base url like below and end with /

$config['base_url'] = 'http://localhost/yourproject/';

Or

$config['base_url'] = 'http://www.yourdomain.com/;

And autoload the url helper config/autoload.php

$autoload['helper'] = array('url');

Tip: Make sure your controller files start with first letter only uppercase same applies for class name

https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming

Example:

Filename: Dashboard.php

<?php

class Dashboard extends CI_Controller {
    // code
}

Upvotes: 1

Related Questions