CSS/JS/图像未从CakePHP的主webroot以外的目录加载 [英] CSS / JS / Images not loading from a different directory than main webroot in CakePHP

查看:53
本文介绍了CSS/JS/图像未从CakePHP的主webroot以外的目录加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用CakePHP应用程序,大多数CSS/JS/图像(图标)位于webroot目录(/var/www/app/webroot/)中,但有一部分(Croogo)或其他该应用程序的页面,我需要从其他目录(即/var/www/app/views/themed/admin/webroot/)中加载CSS/JS/图像.

I have been working on a CakePHP App, most of the CSS / JS / images (icons) are located in the webroot directory (/var/www/app/webroot/), but for some portion (Croogo) or other pages of the app, I need to load CSS / JS / images from a different directory i.e /var/www/app/views/themed/admin/webroot/.

现在我面临的问题是,当我渲染其他页面时,css/JS/图像请求将返回404页面未找到状态,因为这些请求是针对主webroot而不是我上面提到的其他目录.

Now the issue I am facing is that when I render these other pages the css / JS / images requests return 404 page not found status, as these requests are made to main webroot rather than the different directory i mentioned above.

是否可以使用.htaccess处理此问题,即如果网址包含此模式"/admin/"

Is there a way to handle this with .htaccess, i.e if the url contains this pattern "/admin/"

示例:

http://www.myapp.com/theme/admin/css/mycss.css

应将CSS/JS/img文件查找到/var/www/app/views/themed/admin/webroot/而不是var/www/app/webroot/??

The CSS / JS / img files should be looked up into /var/www/app/views/themed/admin/webroot/ rather than var/www/app/webroot/ ??

如果没有其他解决方法来处理这种情况?

If not is there any other workaround to handle such a scenario?

以下是主应用目录的.htaccess

Following is the .htaccess of main app directory

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]     
</IfModule>

以下是主webroot目录的.htaccess

Following is the .htaccess of main webroot directory

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

CakePHP代码

echo $html->link($html- >image('../theme/admin/img/icons/16/icon16x16.png',array('alt'=>'','class'=>'icon left','id'=>'profile-icon')).'Resize',array('action'=>'edit_photo','01'),array('escape'=>false,'id'=>'profile-photo','class'=>'blue','rel'=>'shadowbox;height=100;width=400','title'=>'Resize'));

生成的HTML

<a href="/edit_photo/01" id="profile-photo" class="blue" rel="shadowbox;height=100;width=400" title="Resize"><img src="/theme/admin/img/icons/16/icon16x16.png?v=1000" alt="" class="icon left" id="profile-icon" />Resize</a></div>

CakePHP 1.3版

CakePHP version 1.3

任何想法和想法将不胜感激!

Any thoughts and ideas would be appreciated!

推荐答案

那不是它的工作方式

在网络根目录之外的

没有任何应该可以通过网络访问.

That's not how it works

Nothing outside the webroot is supposed to be web accessible.

主题文件不会存储在webroot中,但是可以将它们复制/链接到webroot-或通过资产分派过程提供.

Theme files are not stored in the webroot, but they are intended to be either copied/linked to the webroot - or served via the asset dispatching process.

在这种情况下,例如:

app
    views
       themed
           admin
               webroot
    webroot
        theme
            admin -> ../../views/themed/admin/webroot # <- a symlink

请注意,这意味着使用正确的主题资产url-它直接映射到文件(提供服务时没有CakePHP或PHP逻辑).

Note that this means that with the correct theme asset url - it maps directly to a file (There is no CakePHP or PHP logic in serving it).

您也可以使用正确的URL来进行资产分派(在1.3中是自动的,在以后的版本中需要显式启用)-尽管这是

You can also rely on asset dispatching using the correct url (in 1.3 this is automatic, in later versions it needs to be enabled explicitly) - though that is significantly slower.

在问题中使用相对网址:

In the question a relative url is used:

echo $html->image('../theme/admin/img/icons/16/icon16x16.png'
                   ^

这是相当脆弱的,并且是导致结果URL不能指向正确文件的原因-而是使用绝对URL(在您的应用程序中,绝对URL总是相对于其安装位置而言):

This is quite fragile and a possible factor in why the resultant url does not point at the right file - instead use an absolute url (within your application, an absolute url is always relative to wherever it has been installed):

echo $html->image('/theme/admin/img/icons/16/icon16x16.png'
                   ^

这可以确保无论当前的控制器/操作/位置如何,URL都是正确的.

This ensures the right url irrespective of the current controller/action/location.

这篇关于CSS/JS/图像未从CakePHP的主webroot以外的目录加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆