php - 如何在 CodeIgniter 中访问静态资源?

标签 php codeigniter static routes

我正在为一个项目使用 CodeIgniter。

文件夹结构 - htdocs/NewProject/application

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

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

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

我的路线是

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

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

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

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

有没有我应该添加这些静态文件的备用文件夹?我需要向路由文件添加任何内容吗?

最佳答案

把结构改成这样

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

config.php 中更改您的基本 URL>

/*
  |--------------------------------------------------------------------------
  | 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');

在你的 View 文件中

<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="" />

希望这对您有所帮助。谢谢!!

关于php - 如何在 CodeIgniter 中访问静态资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15050332/

相关文章:

php - Paypal Pro 不在 magento 中显示运输方式

javascript - 如何使用 ajax 和 codeigniter 加载查看更多评论

c++ - 在类模板中定义一个静态数组

java - 在静态上下文中定义的本地类中使用 this 和 super

php - 如何在 Azure 上启用 Zend Guard Loader 扩展 (ZendLoader.dll)?

php - Eclipse PDT插件安装

mysql - codeigniter 购物车和多个表

php - CodeIgniter 存储过程的多个结果

javascript - REST API 与直接访问代码

php - 了解php错误处理和代码执行