代码点火器 + CSS

发布于 2024-12-26 12:10:31 字数 637 浏览 0 评论 0原文

美好的一天,我正在用 Smarty 学习 CodeIgniter。我的 CSS 文件存储在

/App01/application/views/css/main.css

链接我使用的 CSS:

<link rel="stylesheet" type="text/css" href="http://localhost:88/APP1/application/views/css/layout.css" media="screen" />

但 CSS 未应用在我的页面上。当我打开 CSS URL 时,我收到一条消息:

Forbidden
You don't have permission to access /APP1/application/views/css/layout.css on this server.

请问,我做错了什么?我想将 CSS 与视图放在一起,因为将来我想学习如何创建多个主题,并且我认为 CSS 应该保存在主题文件夹中。

我可以用一些 Smarty 变量替换 CSS 文件的 URL 路径,这样当我移动应用程序时,我不需要手动更改模板中的 CSS URL 路径吗?

先感谢您!沃伊泰克

Good Day, I'm learning CodeIgniter with Smarty. My CSS file is stored in

/App01/application/views/css/main.css

To link my CSS I use:

<link rel="stylesheet" type="text/css" href="http://localhost:88/APP1/application/views/css/layout.css" media="screen" />

But CSS is not applied on my page. When I open CSS URL, I get a message:

Forbidden
You don't have permission to access /APP1/application/views/css/layout.css on this server.

Please, what am I doing wrong? I'd like to keep my CSS together with the view because in future I'd like to learn how to create multiple themes and I thing the CSS should be kept within the theme folder.

Can I replace URL path to CSS file with some Smarty variable so that when I move my application I do not need to change CSS URL path in templates manually?

Thank you in advance! Vojtech

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若言繁花未落 2025-01-02 12:10:31

CodeIgniter 的 /application 文件夹中的任何内容都应被视为越界。为了获得最佳安全性,您实际上应该考虑将 /application 保留在 wwwpublic_html 文件夹之上,结构如下:

– application
    – controllers
    – models
    – views
    – ...
– system
    – core
    – libraries
    – ...
– public_html
    – index.php

这使得您的应用程序代码更安全。

我建议在公共文件夹中创建客户端脚本和 CSS。例如 public_html/csspublic_html/js。或者,如果您想沿着主题路线前进,可以将每个 CSS 文件命名为主题名称,这样您就会有 css/theme1.csscss/theme2.css

如果您的站点始终从域的根目录运行,那么您可以使用:

<link rel="stylesheet" type="text/css" href="/css/layout.css" media="screen" />

但是如果您觉得要移动各种内容,请考虑在将文件发送到之前在控制器中准备好文件位置聪明。

$this->load->helper('url');
$this->smarty->assign('css_file', base_url("css/theme1.css"));

这将返回:

http://localhost/app1/css/theme.css

或者无论您的 CodeIgniter URL 是什么。

Anything in the /application folder of CodeIgniter should be considered out-of-bounds. For the best security, you should actually consider keeping /application above your www or public_html folder in a structure such as this:

– application
    – controllers
    – models
    – views
    – ...
– system
    – core
    – libraries
    – ...
– public_html
    – index.php

This makes your application code safer.

I’d advise creating your client-side scripts and CSS in a public folder. For example public_html/css and public_html/js. Or, if you wanted to go down the theme route, possibly name each CSS file as the name of the theme, so you’d have css/theme1.css and css/theme2.css.

If your site will always work from the root of a domain, then you can just use:

<link rel="stylesheet" type="text/css" href="/css/layout.css" media="screen" />

But if you feel that you’re going to be moving all sorts of things around, then consider preparing the file location in your controller before sending it to Smarty.

$this->load->helper('url');
$this->smarty->assign('css_file', base_url("css/theme1.css"));

That will return:

http://localhost/app1/css/theme.css

Or whatever your CodeIgniter URL is.

动次打次papapa 2025-01-02 12:10:31

这将有助于将 css 链接到 codeigniter。

link_tag 用于链接资源,您可以使用 helper 函数。
例如 html 帮助器、url 帮助器、电子邮件帮助器等。

在您的控制器中,您必须创建一个类似的函数

<?php 
class Home extends CI_Controller{
    public function helper(){
        $this->load->helper('html');    
        $this->load->view('index');
    }
}
?>

,并且您的 view 文件夹中的 index.php 使用 link_tag 关键字。

<html>
<head>
<title></title>
<?php echo link_tag('App01/application/views/css/main.css');?>
</head>
<body>
    <?php 
     .......
    ?>
</body>
</html>

This will help to link css to codeigniter.

The link_tag is used to link resources and you can use helper function.
For example html helper, url helper, email helper, etc.

In your controller you have to create a function something like

<?php 
class Home extends CI_Controller{
    public function helper(){
        $this->load->helper('html');    
        $this->load->view('index');
    }
}
?>

And your index.php in view folder use link_tag keyword.

<html>
<head>
<title></title>
<?php echo link_tag('App01/application/views/css/main.css');?>
</head>
<body>
    <?php 
     .......
    ?>
</body>
</html>
反话 2025-01-02 12:10:31

尝试将符号链接添加到服务器文档根文件夹。 (www/public_html/htdocs)

cd (document root folder)
ln -s (/App01/application/views/css) .

这样您就可以访问 css 文件夹并保留当前结构。

Try adding a symlink to your servers document root folder. (www/public_html/htdocs)

cd (document root folder)
ln -s (/App01/application/views/css) .

This way you can access your css folder and keep the current structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文