CSS、JS包含方法

发布于 2024-12-12 11:05:09 字数 761 浏览 4 评论 0原文

这是一个概念性问题。将 CSS、Javascript 等资源包含到 HTML 页面的最佳方法是什么? 我正在实现我自己的 MVC 框架。应用程序directroy structer是

index.php
controllers
  c1.php
  c2.php
    ...
views
  v1.php
  v2.php
    ...
scripts
  s1.js
  s2.js
    ...
styles
  style1.css
  style2.css
    ...

如您所见,所有请求都通过index.php,然后我找到正确的控件来处理它。控制器处理一些业务逻辑,然后包含视图文件。

在视图文件中,我需要给出绝对路径像这样的所有 css 和 java 脚本;

<link rel="stylesheet" type="text/css" href="<?php echo APPROOT; ?>/styles/master.css" />'
<script type="text/javascript" src="<?php echo APPROOT; ?>/scripts/jquery-1.6.1.min.js"></script>

APPROOT 是一个定义应用程序目录路径的常量:

define("APPROOT", "/project1");

我认为这不是最好的方法,那么我该如何改进它呢?

It's a conceptual question. What's the best way to inlude assets like CSS, Javascript to HTML page..
I'm implementing my own MVC framwork. Application directroy structer is

index.php
controllers
  c1.php
  c2.php
    ...
views
  v1.php
  v2.php
    ...
scripts
  s1.js
  s2.js
    ...
styles
  style1.css
  style2.css
    ...

As you can see, all request come through index.php and then I find the right control to handle it.. Controller process some business logic then include a view file..

In view file i need to give an absolute path to all css and java script like this ;

<link rel="stylesheet" type="text/css" href="<?php echo APPROOT; ?>/styles/master.css" />'
<script type="text/javascript" src="<?php echo APPROOT; ?>/scripts/jquery-1.6.1.min.js"></script>

APPROOT is a constant which defines directory path for application:

define("APPROOT", "/project1");

I think It's not the best way so how can i improve it?

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

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

发布评论

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

评论(2

为你拒绝所有暧昧 2024-12-19 11:05:09

通常,您希望将应用程序保留在公共目录之外,并创建一个指向它的包含路径。这样,您可以编写 url 重写,仅当请求路径中的文件不存在时才重定向到 index.php。在 htaccess 中,看起来像这样。

# redirect any requests for missing files to index.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]

这样,如果所请求的文件确实存在于公共目录中,apache将像往常一样简单地提供该文件,但如果所请求的文件存在,它将重定向到index.php进行MVC处理。

要创建指向应用程序目录的包含路径,请参阅以下文档:

http://php.net/manual/en/function.set-include-path.php

您还可以通过inis设置包含路径。

Typically you would want to keep your application outside of the public directory, and create an include path that leads to it. This way you can write your url rewrite to simply redirect to index.php only if a file at the requested path does not exist. In htaccess, that would look something like this.

# redirect any requests for missing files to index.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]

This way if the file being requested does exist in the public directory, apache will simply serve that file as usual, but if the file being requested does not exist, it will redirect to index.php for MVC handling.

To create an include path that leads to your application directory, see the following documentation:

http://php.net/manual/en/function.set-include-path.php

You can also set include paths via inis.

魂归处 2024-12-19 11:05:09

我认为正确的方法是像 Zend 那样做。它可能与您的代码兼容,也可能不兼容。

$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/style.css');
$this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery.js','text/javascript',array('language'=>'javascript'));

but i doubt your way, @dqhendricks have good opinion and i think you should look at this tutorial and reconsider your framework approach.
http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

In my opinion the right way is to do as Zend does. It may or may not be compatible with your code.

$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/style.css');
$this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery.js','text/javascript',array('language'=>'javascript'));

but i doubt your way, @dqhendricks have good opinion and i think you should look at this tutorial and reconsider your framework approach.
http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

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