我正在为一个项目使用 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/