【问题标题】:How to access static assets in CodeIgniter?如何访问 CodeIgniter 中的静态资产?
【发布时间】:2013-02-09 14:45:01
【问题描述】:

我正在将 CodeIgniter 用于项目。

文件夹结构 - htdocs/NewProject/application

我创建了一个名为 Home - applications/controllers/home.php 的控制器

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

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

我的路线是

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

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

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

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

是否有任何备用文件夹可以添加这些静态文件?我需要在路由文件中添加任何内容吗?

【问题讨论】:

    标签: php codeigniter static routes


    【解决方案1】:

    把结构做成这样的

    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 中自动加载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="" />
    

    希望这会有所帮助。谢谢!!

    【讨论】:

    • 当我 leave $config['base_url'] 作为解决方案的空字符串 Thx 时,这对我有用
    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2014-11-20
    • 2012-02-10
    • 2012-11-05
    • 2019-01-20
    • 1970-01-01
    相关资源
    最近更新 更多