cakePhp - 访问webroot

时间:2017-08-25 06:23:18

标签: javascript php css cakephp

这是我想要的结构:

-webroot/
    -css/
    -font/
    -img/
    -js/
    -new_folder/
        -file.css
        -another_new_folder/
             -file.js

如何在webroot/new_folder/file.css文件中访问webroot/new_folder/another_new_folder/file.js.ctp,以便生成这两个标记:

<link href="/new_folder/file.css">
<script src="/new_folder/another_new_folder/file.js">
如果可能,

不生成控制器。

谢谢你们。

2 个答案:

答案 0 :(得分:3)

您可以使用

echo $this->Html->css('/new_folder/file');
    // webroot/new_folder/file.css
echo $this->Html->script('/new_folder/another_new_folder/file');
    // webroot/new_folder/another_new_folder/file.js

对于css和脚本,您可以使用以下方式

echo $this->Html->script('custom');
    // webroot/js/custom.js

echo $this->Html->script('otherdir/custom');
    // webroot/js/otherdir/custom.js


echo $this->Html->script('/otherdir/custom');
// webroot/otherdir/custom.js

有关详情,请查看Linking to Javascript FilesLinking to CSS Files

答案 1 :(得分:0)

的CSS:

# create dummy files under Folder "scriptFld"
ifelse(!dir.exists(file.path("scriptFld")), dir.create(file.path("scriptFld")), FALSE)
strTime = format(Sys.time(), "%H%M")
file.create(NA, paste0("scriptFld/mySource1_", strTime,".R")); Sys.sleep(1)
file.create(NA, paste0("scriptFld/mySource2_", strTime,".R")); Sys.sleep(1)
file.create(NA, paste0("scriptFld/notMySource3_", strTime,".R"))


# read source R files
setwd("scriptFld")
fInfo = file.info(list.files()) # find all files under the folder "scriptFld"
iCandidate = which(substr(rownames(fInfo),1,8) == "mySource") # focus on file names starting with "source"
iCandidateMax = iCandidate[ which.max(fInfo$mtime[iCandidate]) ] # choose the most recent file
fSourceName = rownames(fInfo)[iCandidateMax]
source(file = fSourceName) # This is what I want, except the script is too long.
setwd("..")
(fSourceName)

JS:

<?php echo $this -> Html -> css(array('/new_folder/file.css'));?>