首页 文章

如何在CodeIgniter中访问静态资产?

提问于
浏览
3

我正在使用CodeIgniter进行项目 .

文件夹结构 - htdocs / NewProject / application

我创建了一个名为Home的应用程序 - applications / controllers / home.php

我创建了一个视图 - applications / view / index.php

我可以毫无问题地访问index.php .

我的路线是

$route['default_controller'] = "home";

我可以访问localhost:8888 / NewProject - 它带我回家#index

我在视图文件夹中放了一些css和images文件夹 . 我如何访问图像和CSS?

试图访问NewProject / css / xxx.css导致找不到错误 .

是否有任何备用文件夹,我应该添加这些静态文件?我是否需要在路线文件中添加任何内容?

1 回答

  • 15

    使结构像这样

    root (is your root directory)
        /application
        /system
        /static
            /images
            /css
            /js
    

    config.php 中更改基本网址

    /*
      |--------------------------------------------------------------------------
      | Base Site URL
      |--------------------------------------------------------------------------
      |
      | URL to your CodeIgniter root. Typically this will be your base URL,
      | WITH a trailing slash:
      |
      | http://example.com/
      |
      | If this is not set then CodeIgniter will guess the protocol, domain and
      | path to your installation.
      |
     */
     // use this if your project is in root
    $config['base_url'] = 'http://example.com/';
    
    // use this if your project is in folder/subfolders
    //$config['base_url'] = 'http://example.com/newproject/liveproject/';
    

    autoload.php 中的Autoload URL 帮手

    /*
      | -------------------------------------------------------------------
      |  Auto-load Helper Files
      | -------------------------------------------------------------------
      | Prototype:
      |
      | $autoload['helper'] = array('url', 'file');
     */
    
    $autoload['helper'] = array('url');
    

    在您的视图文件中

    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>static/style.css" />
    
    <img src="<?php echo base_url(); ?>static/images/myimage.jpg" alt="" />
    

    希望这可以帮助 . 谢谢!!

相关问题