如何访问CodeIgniter中的静态资产? [英] How to access static assets in CodeIgniter?
问题描述
我正在为一个项目使用CodeIgniter。
I am using CodeIgniter for a Project .
文件夹结构 - htdocs / NewProject / application
Folder Structure - htdocs/NewProject/application
我创建了一个名为Home的控制器 - applications / controllers / home .php
I created a controller called Home - applications/controllers/home.php
我创建了一个视图 - applications / view / index.php
I created a view - applications/view/index.php
我可以访问索引。 php没有问题。
I can access the index.php without issue .
我的路线是
$route['default_controller'] = "home";
我可以访问localhost:8888 / NewProject - 它带我回家#index
I can access localhost:8888/NewProject - which takes me to home#index
我在view文件夹中放了一些css和images文件夹。
如何访问图像和css?
I have put some css and images folders inside views folder . How do i access the images and css ?
尝试访问NewProject / css / xxx.css会导致找不到错误。
Trying to access NewProject/css/xxx.css leads to not found error .
有没有备用文件夹,我应该添加这些静态文件?
Is there any alternate folder where I should add these static files ? Do I need to add anything to the routes file ?
推荐答案
使结构像这样
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/';
自动载入 URL
c> autoload.php
Autoload URL
helper in autoload.php
/*
| -------------------------------------------------------------------
| 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="" />
希望这有帮助。谢谢!
这篇关于如何访问CodeIgniter中的静态资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!