简体   繁体   English

Serving.js 和 .css 文件位于 PHP 中的公用文件夹之外

[英]Serving .js and .css files that are outside of public folder in PHP

I've got this structure in my projects:我的项目中有这样的结构:

[]public []上市

  • index.php index.php

  • style.css样式.css

  • script.js脚本.js

[]controller []控制器

[]model []模型

[]view []看法

  • base.html.php基地.html.php

  • []view1 []视图1

    • view1.html.php视图1.html.php
    • view1.css查看1.css
    • view1.js view1.js

.htaccess .htaccess

In public folder, which is the root's server, there's.js and.css files which must be accessed by the browser.在根服务器的public文件夹中,有.js和.css文件,必须通过浏览器访问。 However, I've more and more oftenly css and javascript files particular to one view.但是,我越来越频繁地使用 css 和 javascript 文件,特别是一个视图。 As css interact with html and js with css (via selectors), It is easiest for me to have all theses files in one folder (like the view1 folder in the example above).由于 css 与 html 和 js 与 css 交互(通过选择器),对我来说,将所有这些文件放在一个文件夹中是最简单的(如上面示例中的 view1 文件夹)。

Is there a way to do this?有没有办法做到这一点? I guess my best bet would be to act in the main controller.我想我最好的选择是在主要的 controller 中行动。 When it serves a view depending of the url, it should move the js and css files (or duplicate them) from the view folder to the public folder.当它根据 url 提供视图时,它应该将 js 和 css 文件(或复制它们)从视图文件夹移动到公用文件夹。 But can it be moved temporary instead of permanent?但它可以临时移动而不是永久移动吗? And does it costs lots of server resources?并且它会花费大量的服务器资源吗?

thaks a lot!非常感谢!

Static files (css, js, html) should be served together with your public directory. Static 文件(css、js、html)应与您的公共目录一起提供。 Why?为什么? Because they're what you want to serve to the client, like you said.因为他们是你想为客户服务的,就像你说的。

To serve a client a file, the server must allow the client to request the file, putting it outside of the client's access points defeats the point.为了向客户端提供文件,服务器必须允许客户端请求文件,将其放在客户端的访问点之外会破坏该点。

But regardless, to give you an answer to your actual question, yes, you can do this by adding a rewrite rule for an non-existant path, such as view1 in public to use the view1 path in your view folder:但是无论如何,为了给您一个实际问题的答案,是的,您可以通过为不存在的路径添加重写规则来做到这一点,例如 public 中的view1以使用view文件夹中的view1路径:

RewriteRule ^view1/(.*)$ ../view/view1/$1 [L,NC]

This will rewrite any request view1/* to view/view1/* rather than public/view1/* .这会将任何请求view1/*重写为view/view1/*而不是public/view1/*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM