0

I am using CodeIgniter for my web app and i want to avoid on keep using base_url() function in all my urls in the views.

Now this code is what i want :

<a href="/user/create">Click me to create user</a>

instead of

<a href="<?php echo base_url(); ?>user/create">Click me to create user</a>


However, specifying "/user/create" will redirect me to below url which is incorrect

http://localhost/user/create


What i want is that "/user/create" url in href should automatically redirect me to

 http://localhost/myapp/user/create 

without specifying base_url()

Is this possible?

This is my current .htaccess file

<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On

# If your website begins from a folder e.g localhost/my_project then 
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /myapp/

# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)

# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]

# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)

# No rewriting
RewriteRule ^(.*)$ - [PT,L]

# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>

Any help would greatly be appreciated.

Thanks

Bogz
  • 565
  • 1
  • 10
  • 30

2 Answers2

0

you can use form helper library

//Controller

function __construct()
{
    parent:: __construct();
    $this->load->helper('url');
    $this->load->helper('form');
}

//View

<?php echo anchor('user/create','Click me to create user');?>

This will create similar result as

<a href="/user/create">Click me to create user</a>

They are way much better than using base_url() for links.

Wayne Tun
  • 590
  • 7
  • 24
  • thanks for your suggestion. I dont want to use any of the functions for the url i.e base_url(), site_url(), anchor(). I just want to avoid that since i dont want to always call that function in every url. Specifying like "/user/create" will be easier and faster for me and for the urls i will put in my views – Bogz Nov 06 '13 at 23:16
  • i think you should , what is the point of using Framework if you do not use libraries,helpers . Just my two cents. – Wayne Tun Nov 07 '13 at 20:24
  • hi Wayne, yes i understand that helpers are available but it is much faster to code if i eliminate using base_url(). if i have many links in one page (including local css and js), it would be time-consuming for me if i use use base_url() in all links thats why i am asking if what i want is possible – Bogz Nov 11 '13 at 07:06
  • I sincerely believe that "echo anchor('user/create','Create') " is a lot better than "> Create – Wayne Tun Nov 11 '13 at 17:04
  • Hi Wayne, thanks for your replies. But i think you are not getting my point here. My point is i dont want to use any of the functions for url i.e anchor() or base_url().. what i want is in my post where i dont need to specify url functions in CI.. just specify relative url and the page will understand – Bogz Nov 12 '13 at 08:03
0

Try this:
href="user/create"
Just remove the first slash (/) before user and try.

user2936213
  • 1,021
  • 1
  • 8
  • 19
  • actually this is what i am using now. this will work in forms or other links inside html page but doesnt work in javascript i.e window.location.href = "user/create" any idea how to let it work in javascript? my javascript files location are in CIProject/assets/js/jsfile.js – Bogz Nov 11 '13 at 07:08