CodeIgniter - Simple base_url question

后端 未结 4 1191
有刺的猬
有刺的猬 2020-12-11 13:50

I\'m a bit confused here.

I have a simple controller which loads a view. The view contains a form and links some CSS files. I don\'t really want to do ../../cs

相关标签:
4条回答
  • 2020-12-11 14:19

    You can use the URL helper to ease your URL woes :)

    http://codeigniter.com/user_guide/helpers/url_helper.html

    Usage

    Load it up in your bootstrap

    $this->load->helper('url');
    

    And whenever you need something you can use

    echo site_url("/css/style.css");
    

    Or just assign it as a handy base url so you can use it wherever you want.

    $base_url = site_url('/');
    <link href="{$base_url}css/style.css" rel="stylesheet" type="text/css" />
    <?php echo 'base url is' . $base_url?>
    

    Note

    Remember to define your proper base URL in the config file before using this method.

    0 讨论(0)
  • 2020-12-11 14:20

    I think your problem is that base_url is a function in ci 2+ so try this instead

    <link href="<?php echo base_url() ?>css/style.css" 
    rel="stylesheet" type="text/css" />
    

    It depends how you defined base_url if you did an ending slash otherwise just add a slash so

    /css/style.css
    
    0 讨论(0)
  • 2020-12-11 14:28

    for CI 2+ you can add $this->load->helper('url'); before you load the view and then add <link href="<?php echo base_url().'css/style.css';?>" rel="stylesheet" type="text/css" /> into your view file.

    0 讨论(0)
  • 2020-12-11 14:36

    I put my css files in the root directory and link them like this

    <?php echo link_tag('css/forie.css'); ?>  
    <?php echo link_tag('css/reset.css'); ?>
    <?php echo link_tag('css/main.css'); ?>
    

    Using link_tag allows me to access them easily

    0 讨论(0)
提交回复
热议问题